这段代码有什么问题吗?

时间:2010-04-04 13:04:53

标签: php wordpress

在下面的代码中,我是否真的必须在每种情况下返回$ 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);
}
}

2 个答案:

答案 0 :(得分:1)

操作永远不会期望返回值。它们在{strong> wp-includes / plugin.php 中由do_action('name');调用,但不会返回任何内容。 所以,不,你不必返回$postID

答案 1 :(得分:0)

据我所知,您受到启发的代码不会返回$postID - 我在法典中看不到有关必要的内容。那么,不是吗?

相关问题