doutan5337 2015-02-05 21:31
浏览 46
已采纳

自定义元框保存值问题 - 不推荐使用:函数ereg()等

I'm a WP theme dev nobie and I have problems with saving custom meta boxes values.

I found some tutorial where its explained how to do this.

I have included a file called custom-meta-boxes-save.php within my functions.php with the following code:

<?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');
?>

After hitting publish for posts and pages I get a number of errors when my wp_debug is turned on:

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

Help much needed as my deadline is VERY CLOSE.

Thanks!

  • 写回答

1条回答 默认 最新

  • dongxiongshi9952 2015-02-05 22:06
    关注

    Use preg_match instead of ereg

    eg. preg_match('/edit.php/', $_SERVER['SCRIPT_NAME'])

    Do you need to define $data using global at the top of the function?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示