php – WordPress add_meta_box()奇怪

下面的代码工作几乎完美无缺,但是我的一个页面上的页面标题的价值在几页刷新后仍然空白…它坚持了一段时间,然后似乎重置为空.我想我下面的代码中必须有一个冲突,但我不太清楚.

我允许用户通过自定义的“帖子/页面标题输入字段”为帖子和页面设置自定义页面标题.任何人都可以看到一个明显的问题,可能是将页面标题重置为空白?

// ===================
// = POST OPTION Box =
// ===================

add_action('admin_menu','my_post_options_Box');

function my_post_options_Box() {
    if ( function_exists('add_Meta_Box') ) { 
      //add_Meta_Box( $id,$title,$callback,$page,$context,$priority );
        add_Meta_Box('post_header','Custom Post Header Code (optional)','custom_post_images','post','normal','low');
        add_Meta_Box('post_title','Custom Post Title','custom_post_title','high');
        add_Meta_Box('post_title_page','page','high');
        add_Meta_Box('postexcerpt',__('Excerpt'),'post_excerpt_Meta_Box','core');
        add_Meta_Box('categorydiv',__('Page Options'),'post_categories_Meta_Box','side','core');
    }
}

//Adds the custom images Box
function custom_post_images() {
    global $post;
    ?>
    <div class="inside">
        <textarea style="height:70px; width:100%;margin-left:-5px;" name="customHeader" id="customHeader"><?PHP echo get_post_meta($post->ID,'customHeader',true); ?></textarea>
        <p>Enter your custom html code here for the post page header/image area. Whatever you enter here will override the default post header or image listing <b>for this post only</b>. You can enter image references like so &lt;img src='wp-content/uploads/product1.jpg' /&gt;. To show default images,just leave this field empty</p>
    </div>
<?PHP
}

//Adds the custom post title Box
function custom_post_title() {
    global $post;
    ?>
    <div class="inside">
        <p><input style="height:25px;width:100%;margin-left:-10px;" type="text" name="customTitle" id="customTitle" value="<?PHP echo get_post_meta($post->ID,'customTitle',true); ?>"></p>
        <p>Enter your custom post/page title here and it will be used for the html &lt;title&gt; for this post page and the Google link text used for this page.</p>
    </div>
<?PHP
}


add_action('save_post','custom_add_save');

function custom_add_save($postID){
    if (!defined('DOING_AUTOSAVE') && !DOING_AUTOSAVE) {
        return $postID;
    }
    else
    {
        // called after a post or page is saved and not on autosave
        if($parent_id = wp_is_post_revision($postID))
        {
        $postID = $parent_id;
        }

        if ($_POST['customHeader']) 
        {
            update_custom_Meta($postID,$_POST['customHeader'],'customHeader');
        }
        else
        {
            update_custom_Meta($postID,'','customHeader');
        }
        if ($_POST['customTitle']) 
        {
            update_custom_Meta($postID,$_POST['customTitle'],'customTitle');
        }
        else
        {
            update_custom_Meta($postID,'customTitle');
        }
    }

  }
    function update_custom_Meta($postID,$newvalue,$field_name) {
    // To create new Meta
    if(!get_post_meta($postID,$field_name)){
    add_post_Meta($postID,$field_name,$newvalue);
    }else{
    // or to update existing Meta
    update_post_Meta($postID,$newvalue);
    }
}
?>
wordpress自动保存系统可能是您的问题,因为我认为自定义字段不会传递给自动保存(所以您的customHeader和customTitle post变量在自动保存期间将为空).

在您的保存功能中,您应该检查是否设置了DOING_AUTOSAVE常量(这似乎比检查帖子操作更合适),如果是这样,返回.这样的事情

if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;

更多信息请看这张票:http://core.trac.wordpress.org/ticket/10744

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...