【问题标题】:Anything wrong with this code?这段代码有什么问题吗?
【发布时间】:2010-04-04 13:04:53
【问题描述】:

在下面的代码中,我真的必须在每种情况下都返回 $postID 吗?

这是捕获我添加到 WP 帖子和页面编辑器的自定义字段值所需的代码。从这里得到想法:http://apartmentonesix.com/2009/03/creating-user-friendly-custom-fields-by-modifying-the-post-page/

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
    if($parent_id = wp_is_post_revision($postID))
    {
    $postID = $parent_id;
    }

    if ($_POST['my_customHeader']) 
    {
        update_custom_meta($postID, $_POST['my_customHeader'], 'my_customHeader');
    }
    else
    {
        update_custom_meta($postID, '', 'my_customHeader');
    }
    if ($_POST['my_customTitle']) 
    {
        update_custom_meta($postID, $_POST['my_customTitle'], 'my_customTitle');
    }
    else
    {
        update_custom_meta($postID, '', 'my_customTitle');
    }
}
        return $postID; //IS THIS EVEN NECESSARY?
}

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

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    动作从不期望返回值。它们由 wp-includes/plugin.php 中的do_action('name'); 调用,不返回任何内容。 所以,不,您不必返回 $postID。

    【讨论】:

      【解决方案2】:

      据我所知,您受到启发的代码不会返回$postID——而且我在 Codex 中看不到任何关于它的必要性。所以,不是吗?

      【讨论】:

        猜你喜欢
        • 2015-04-25
        • 2018-09-09
        相关资源
        最近更新 更多