Wordpress add_meta_box() 奇怪之处 [英] Wordpress add_meta_box() weirdness

查看:32
本文介绍了Wordpress add_meta_box() 奇怪之处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码几乎完美地工作,但是我的一个页面上的页面标题值在几次页面刷新后一直为空......它坚持了一段时间,然后它似乎重置为空.我想我一定在下面的代码中有冲突,但我不太明白.

我允许用户通过自定义帖子/页面标题输入字段"为帖子和页面设置自定义页面标题.有人能在这里看到一个明显的问题,可能是将页面标题重置为空白吗?

//====================//= 发布选项框 =//====================add_action('admin_menu', 'my_post_options_box');函数 my_post_options_box() {如果(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', 'post', 'normal', 'high');add_meta_box('post_title_page', '自定义帖子标题', 'custom_post_title', 'page', 'normal', 'high');add_meta_box('postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', 'page', 'normal', 'core');add_meta_box('categorydiv', __('Page Options'), 'post_categories_meta_box', 'page', 'side', 'core');}}//添加自定义图片框函数 custom_post_images() {全球 $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>在此处为帖子页面标题/图像区域输入您的自定义 html 代码.您在此处输入的任何内容都将覆盖默认的帖子标题或图片列表仅适用于该帖子.您可以像这样输入图像参考 &lt;img src='wp-content/uploads/product1.jpg'/>.要显示默认图像,只需将此字段留空即可</p>

<?php}//添加自定义帖子标题框函数 custom_post_title() {全球 $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>在此处输入您的自定义帖子/页面标题,它将用于 html &lt;title>用于此帖子页面和用于此页面的 Google 链接文本.</p>

<?php}add_action('save_post', 'custom_add_save');函数 custom_add_save($postID){如果 (!defined('DOING_AUTOSAVE') && !DOING_AUTOSAVE) {返回 $postID;}别的{//在帖子或页面被保存后调用,而不是在自动保存时调用如果($parent_id = wp_is_post_revision($postID)){$postID = $parent_id;}如果 ($_POST['customHeader']){update_custom_meta($postID, $_POST['customHeader'], 'customHeader');}别的{update_custom_meta($postID, '', 'customHeader');}如果 ($_POST['customTitle']){update_custom_meta($postID, $_POST['customTitle'], 'customTitle');}别的{update_custom_meta($postID, '', 'customTitle');}}}函数 update_custom_meta($postID, $newvalue, $field_name) {//创建新元if(!get_post_meta($postID, $field_name)){add_post_meta($postID, $field_name, $newvalue);}别的{//或者更新现有的元数据update_post_meta($postID, $field_name, $newvalue);}}?>

解决方案

Wordpress 的自动保存系统很可能是您的问题,因为我认为自动保存时不会传递自定义字段(因此您的 customHeader并且 customTitle 帖子变量在自动保存期间将为空).

在您的保存函数中,您应该检查是否设置了 DOING_AUTOSAVE 常量(这似乎比检查 post 操作更可取),如果设置则返回.像这样:

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

查看此票以了解更多信息:http://core.trac.wordpress.org/ticket/10744

The code below is working nearly flawlessly, however my value for page title on one of my pages keeps coming up empty after a few page refreshes... It sticks for awhile, then it appears to reset to empty. I'm thinking I must have a conflict in the code below, but I can't quite figure it.

I'm allowing the user to set a custom page title for posts as well as pages via a custom "post/page title input field). Can anyone see an obvious issue here that might be resetting the page title to blank?

// ===================
// = 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', 'post', 'normal', 'high');
        add_meta_box('post_title_page', 'Custom Post Title', 'custom_post_title', 'page', 'normal', 'high');
        add_meta_box('postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', 'page', 'normal', 'core');
        add_meta_box('categorydiv', __('Page Options'), 'post_categories_meta_box', 'page', '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, $field_name, $newvalue);
    }
}
?>

解决方案

Wordpress's auto save system may well be your problem, as I think custom fields are not passed along for auto saves (so your customHeader and customTitle post variables will be empty during an auto save).

In your save function you should check if the DOING_AUTOSAVE constant is set (this seems to be preferable to checking the post action) and return if so. Something like this:

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

See this ticket for more info: http://core.trac.wordpress.org/ticket/10744

这篇关于Wordpress add_meta_box() 奇怪之处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆