【问题标题】:Wordpress - creating meta boxes and savingWordpress - 创建元框并保存
【发布时间】:2013-06-18 11:24:29
【问题描述】:

我正在尝试创建一个元框,它应该很简单,但我弄错了。它与旅游自定义帖子有关,并添加了该旅游的价格:

所以最初我创建了这个自定义帖子类型(这有效):

register_post_type( 'tours',
            array(
                'labels' => array(
                    'name' => __( 'Tours' ),
                    'singular_name' => __( 'Tour' ),
                    'add_new' => 'Add New Tour Instance',
                    'add_new_item' => 'Add New Tour Instance',
                    'edit' => 'Edit',
                    'edit_item' => 'Edit Tour Instance',
                    'new_item' => 'New Tour Instance',
                    'view' => 'View',
                    'view_item' => 'View Tour Instance',
                    'search_items' => 'Search Tour Instances',
                    'not_found' => 'No Tour Instances found',
                    'not_found_in_trash' => 'No Tour Instances found in Rubbish',
                    'parent' => 'Parent Tour Instance'
                ),
            'public' => true,
            'supports' => array( 'title', 'editor', 'thumbnail'),
            'taxonomies' => array( '' ),
            //'menu_icon' => plugins_url( 'images/image.png', __FILE__ ),
            'capability_type' => 'post',
            'rewrite' => array("slug" => "tours") // Permalinks format
            )
        );

然后我自己创建盒子(这也有效):

add_action( 'admin_init', 'tour_meta' );
        function tour_meta() {
            add_meta_box(
                'tours_meta_box',
                'Tour Price',
                'display_tours_price_meta_box',
                'tours',
                'side',
                'high'
            );
        }

现在,在整个函数中,我尝试获取细节并保存它:

function display_tours_price_meta_box() {
            global $post;
            // Noncename needed to verify where the data originated
            echo '<input type="hidden" name="tourmeta_noncename" id="tourmeta_noncename" value="' . 
            wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

            // Get the price data if its already been entered
            $price = get_post_meta($post->ID, '_price', true);
            echo 'Add the total cost for this tour here, do not include monetary characters like £ or $';
            echo '<input type="text" name="_price" ' . $price  . '" class="widefat" />';
        }
        function save_tours_meta($tour_id, $tour) {
            if ( !wp_verify_nonce( $_POST['tourmeta_noncename'], plugin_basename(__FILE__) )) {
            return $post->ID;
            }
            // Is the user allowed to edit the post or page?
            if ( !current_user_can( 'edit_post', $post->ID ))
                return $post->ID;

            $tour_meta['_price'] = $_POST['_price'];

            // Add values of $tour_meta as custom fields
            foreach ($tour_meta as $key => $value) {
                if( $post->post_type == 'revision' ) return;
                $value = implode(',', (array)$value);
                if(get_post_meta($post->ID, $key, FALSE)) {
                    update_post_meta($post->ID, $key, $value);
                } else {
                    add_post_meta($post->ID, $key, $value);
                }
                if(!$value) delete_post_meta($post->ID, $key);
            }
        }
        add_action('save_post', 'save_tours_meta', 1, 2);

实际的框出现,信息被回显。但是它不保存。我没有最模糊的原因。它必须与我的最后一个函数有关,但我不明白出了什么问题。

谢谢

【问题讨论】:

    标签: wordpress custom-post-type meta-boxes


    【解决方案1】:

    您最好像这样简化您的保存过程:

        function save_tours_meta($post_id) {
            if ( !wp_verify_nonce( $_POST['tourmeta_noncename'], plugin_basename(__FILE__) ))       {
            // You don't need to return anything
            return;
            }
            // Is the user allowed to edit the post or page?
            if ( !current_user_can( 'edit_post', $post_id ) && !isset($_POST['_price']))
                return;
    
            // If you just have 1 value
            update_post_meta($post_id, '_price', $_POST['_price']);
        }
    

    请注意update_post_meta 函数将更新 post meta 或创建 如果它不存在,所以不要打扰使用add_post_meta

    【讨论】:

      【解决方案2】:

      我认为这是你的论点。您将函数声明为

      function save_tours_meta($tour_id, $tour) {
      

      但您正在使用(例如)$post-&gt;ID$post 在这种情况下不是全局变量,而是函数参数。尝试使用$tour-&gt;ID 或仅使用$tour_id

      其他一些(不相关的)事情:

      1. 您可以调用update_post_meta(),如果它不存在,它会添加密钥,因此您可以删除您的if 语句(只是减少代码行数)。
      2. 您的“修订”检查应返回帖子 ID(并且可以移到循环之外)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多