自定义元框保存值问题-已弃用:函数Ereg()等

时间:2015-02-05 作者:Damir

我是WP主题开发人员nobie,在保存自定义元框值方面有问题。

我找到了一些教程,其中解释了如何做到这一点。

我包含了一个名为custom meta Box save的文件。php在我的函数中。php,代码如下:

<?php 
function save_postdata( $post_id ) {
    global $post, $new_meta_boxes, $page_settings, $blogroll, $post_settings, $team_info, $slide_info, $meta_box_groups;


    if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) {
        return $post_id;
    }

    if( defined(\'DOING_AJAX\') && DOING_AJAX ) { //Prevents the metaboxes from being overwritten while quick editing.
        return $post_id;
    }

    if( ereg(\'/\\edit\\.php\', $_SERVER[\'REQUEST_URI\']) ) { //Detects if the save action is coming from a quick edit/batch edit.
        return $post_id;
    }
    foreach($meta_box_groups as $group) {
        foreach($group as $meta_box) {

            if(isset($_POST[$meta_box[\'name\'].\'_noncename\'])){
                if ( !wp_verify_nonce( $_POST[$meta_box[\'name\'].\'_noncename\'], $meta_box[\'name\'].\'-meta\' )) {
                    error_log(print_r($meta_box, TRUE).\'{err_end}\');
                    return $post_id;
                }
            }

            if ( isset($_POST[\'post_type\']) && \'page\' == $_POST[\'post_type\'] ) {
                if ( !current_user_can( \'edit_page\', $post_id ))
                    return $post_id;
            } else {
                if ( !current_user_can( \'edit_post\', $post_id ))
                    return $post_id;
            }

            if(isset($_POST[$meta_box[\'name\'].\'_value\'])){
                $data = $_POST[$meta_box[\'name\'].\'_value\'];
            }

            if(get_post_meta($post_id, $meta_box[\'name\'].\'_value\') == "") 
                add_post_meta($post_id, $meta_box[\'name\'].\'_value\', $data, true);
            elseif($data != get_post_meta($post_id, $meta_box[\'name\'].\'_value\', true))
                update_post_meta($post_id, $meta_box[\'name\'].\'_value\', $data);
            elseif($data == "" || $data == $meta_box[\'std\'] )
                delete_post_meta($post_id, $meta_box[\'name\'].\'_value\', get_post_meta($post_id, $meta_box[\'name\'].\'_value\', true));

        } // end foreach
    } // end foreach
} // end save_postdata

add_action(\'save_post\', \'save_postdata\');
?>
在点击发布帖子和页面后,当打开wp\\U调试时,我会出现一些错误:

Deprecated: Function ereg() is deprecated in /home/creat/domains/tester.net/public_html/re/wp-content/themes/rev2/includes/custom-meta-boxes-save.php on line 14

Notice: Undefined variable: data in /home/creat/domains/tester.net/public_html/re/wp-content/themes/rev2/includes/custom-meta-boxes-save.php on line 41

Notice: Undefined variable: data in /home/creat/domains/tester.net/public_html/re/wp-content/themes/rev2/includes/custom-meta-boxes-save.php on line 43

Warning: Cannot modify header information - headers already sent by (output started at /home/creat/domains/tester.net/public_html/re/wp-content/themes/rev2/includes/custom-meta-boxes-save.php:41) in /home/creative/domains/ctproject.net/public_html/sa2/wp-admin/post.php on line 233

Warning: Cannot modify header information - headers already sent by (output started at /home/creat/domains/tester.net/public_html/re/wp-content/themes/rev2/includes/custom-meta-boxes-save.php:41) in /home/creative/domains/ctproject.net/public_html/sa2/wp-includes/pluggable.php on line 1178
非常需要帮助,因为我的最后期限很近。

谢谢

1 个回复
SO网友:czerspalace

请尝试以下操作。代替

ereg(\'/\\edit\\.php\', $_SERVER[\'REQUEST_URI\'])
使用

preg_match(\'/edit.php/\',  $_SERVER[\'REQUEST_URI\'])
还有变化

if(isset($_POST[$meta_box[\'name\'].\'_value\'])){
    $data = $_POST[$meta_box[\'name\'].\'_value\'];
}

if(isset($_POST[$meta_box[\'name\'].\'_value\'])){
    $data = $_POST[$meta_box[\'name\'].\'_value\'];
}
else
    $data = "";

结束

相关推荐

如何在常规页面(Page-Example.php)显示类别图片(来自插件)?

我正在尝试使用Category Images Plugin 使用自定义分类法(区域)类别/术语显示图像。以下是我目前为止在页面示例中使用的代码。列出所有类别/术语及其描述的php:$siteurl = home_url(\'/\'); $tax = \'region\'; // slug of taxonomy $terms = get_terms($tax); foreach ($terms as $term) { $slug = $term->