主题面板中的Wordpress徽标上传选项

时间:2016-08-29 12:04:18

标签: php wordpress wordpress-theming

大家好,我在wordpress的主题面板中添加logo选项时遇到一个问题我正在使用此代码

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");

问题是它没有保存徽标,也没有在管理员中显示它。 我用不同的方式尝试了10次,但这段代码不起作用。请查看此代码并尝试一下。

2 个答案:

答案 0 :(得分:1)

您忘记在“handle_logo_upload”函数中添加else条件。请尝试以下代码:

function logo_display()
{
    ?>
        <input type="hidden" name="ologo" value="<?php echo get_option('logo'); ?>" readonly /><input type="file" name="logo" id="imgupload" style="display: none;" />
  <a id="OpenImgUpload" class="button button-primary">Image Upload</a>
        <?php echo get_option('logo'); ?>
   <?php
}
function handle_logo_upload()
{
    if(isset($_FILES["logo"]) && !empty($_FILES['logo']['name']))
    {
        $urls = wp_handle_upload($_FILES["logo"], array('test_form' => FALSE));
        $temp = $urls["url"];
       return $temp;
    }
 elseif(isset($_FILES["logo"]) && empty($_FILES['logo']['name'])){
  $urls = $_POST["ologo"];
  return $urls;
 }
   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");

答案 1 :(得分:0)