I follow up the sitepoint tutorial to create logo uploader for my option page but the image uploader not working correctly.
here the functions.php:
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"]))
{
$urls = wp_handle_upload($_FILES["logo"], array('test_form' => FALSE));
$temp = $urls["url"];
return $temp;
}
return $option;
}
function display_theme_panel_fields()
{
add_settings_section("section", "All Settings", null, "theme-options");
add_settings_field("logo", "Logo", "logo_display", "theme-options", "section");
register_setting("section", "logo", "handle_logo_upload");
}
add_action("admin_init", "display_theme_panel_fields");
if you view source, you will prob see the enctype is not set to enctype="multipart/form-data". You could do something like below to change the enctype
function change_form_enc() {
echo "<script type='text/javascript'>
jQuery(document).ready(function(){
jQuery('form').attr('enctype','multipart/form-data');
});
</script>";
}
if ( is_admin() ) {
add_action('admin_head', 'change_form_enc');
}
function logo_display()
{
?>
<input type="file" id="logo" name="logo" />
<?php echo get_option('logo'); ?>
<?php
}
function handle_logo_upload()
{
if(!empty($_FILES["logo"]["tmp_name"]))
{
$urls = wp_handle_upload($_FILES["logo"], array('test_form' => FALSE));
if ($urls["error"])
{
return $urls["error"];
}
$temp = $urls["url"];
return $temp;
}
return get_option('logo');
}