Image upload in custom theme settings page

I need to upload a logo image and set other options on a custom settings page.
I can’t retrieve the URL and upload the custom image, what is wrong with my code?

<?php

/*** New menu item ***/

function my_admin_menu () {
$page_title = ‘Theme Settings Page’;
$menu_title = ‘Theme Settings’;
$capability = ‘edit_posts’;
$menu_slug = ‘theme_options_page’;
$function = ‘my_theme_settings_page’;
$icon_url = ”;
$position = 110;

add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
}

add_action(‘admin_menu’, ‘my_admin_menu’);

function my_theme_settings_page(){

/*** New section ***/

?>
<h1>Theme Settings Page</h1>
<?php settings_errors(); ?>

<form method=”post” action=”options.php”>
<?php settings_fields(“ff_theme_options”);?>
<?php do_settings_sections(‘theme_options’)?>
<?php submit_button();?>
</form>

<?php
}

/*** Options fields ***/

add_action(‘admin_init’,’ff_custom_setting’);
function ff_custom_setting(){
register_setting(‘ff_theme_options’, ‘phone_field’);
register_setting(“ff_theme_options”, “logo”, “handle_logo_upload”);
add_settings_section(‘ff_theme_options’,’Theme Options’, null, ‘theme_options’);
add_settings_field(‘theme-phone’,’Phone Number’,’theme_phone_func’, ‘theme_options’,’ff_theme_options’);
add_settings_field(‘logo’,’Website Logo’,’logo_display’, ‘theme_options’,’ff_theme_options’);
}

function ff_theme_options(){
echo ‘Add your theme options’;
}
function theme_phone_func(){
$phone = esc_attr(get_option( ‘phone_field’ ));
echo ‘<input type=”text” name=”phone_field” value=”‘.$phone.'” placeholder=”Your Phone Number here”>’;
}
function logo_display()
{
?>
<input type=”file” name=”logo” />
<?php echo get_option(‘logo’); ?>
<?php
}

function handle_logo_upload()
{
if(!empty($_FILES[“demo-file”][“tmp_name”]))
{
require_once( ABSPATH . ‘wp-admin/includes/file.php’ );
$urls = wp_handle_upload($_FILES[“logo”], array(‘test_form’ => false));
$temp = $urls[‘url’];
return $temp;
}
return $option;
}

Read more here:: Image upload in custom theme settings page

Leave a Reply

Your email address will not be published. Required fields are marked *