个性化阅读
专注于IT技术分析

主题面板中的WordPress logo上传选项

大家好,我面临一个问题,在我的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");

问题是它的不保存logo,也不显示在管理以及。我已经尝试了10次不同的方法,但这段代码不工作。请查看这个代码并尝试一下。


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

#2


你应该试试这个。希望这对你有帮助。

https://kwight.ca/2012/12/02/adding-a-logo-uploader-to-your-wordpress-site-with-the-theme-customizer/

赞(0) 打赏
未经允许不得转载:srcmini » 主题面板中的WordPress logo上传选项
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

微信扫一扫打赏