自定义POST类型-UPDATE_META不起作用

时间:2012-01-18 作者:turbonerd

我相信这一定很简单,但我一辈子都想不出为什么这行不通。

if (!class_exists(\'FeaturedItem\')) :

class FeaturedItem {

    var $plugin_url;

    function FeaturedItem()
    {
        $this->plugin_url = trailingslashit(plugins_url(\'sf-featured-items\'));
    }

    function enqueue_scripts() {
        /* start of replacement tinyMCE instance
        wp_register_script(\'cleditor\', $this->plugin_url .\'cleditor/jquery.cleditor.min.js\', array(\'jquery\'));
        wp_enqueue_script(\'cleditor\'); */
    }

    function enqueue_styles() {
        /* Image upload styles
        wp_enqueue_style(\'thickbox\'); */
    }

    function register_featured_item(){

        $labels = array(
          \'name\' => _x(\'Featured Item\', \'post type general name\'),
          \'singular_name\' => _x(\'Featured Item\', \'post type singular name\'),
          \'add_new\' => _x(\'Add New\', \'profile\'),
          \'add_new_item\' => __(\'Add New Featured Item\'),
          \'edit_item\' => __(\'Edit Featured Item\'),
          \'new_item\' => __(\'New Featured Item\'),
          \'view_item\' => __(\'View Featured Item\'),
          \'search_items\' => __(\'Search Featured Items\'),
          \'not_found\' =>  __(\'No featured items found\'),
          \'not_found_in_trash\' => __(\'No featured items found in Trash\'),
          \'parent_item_colon\' => \'\',
          \'menu_name\' => \'Featured\'

        );
        $args = array(
          \'labels\' => $labels,
          \'public\' => true,
          \'publicly_queryable\' => false,
          \'exclude_from_search\' => true,    
          \'show_ui\' => true,
          \'show_in_menu\' => true,
          \'query_var\' => true,
          \'rewrite\' => false,
          \'capability_type\' => \'post\',
          \'has_archive\' => false,
          \'hierarchical\' => false,
          \'menu_position\' => 50,
          \'menu_icon\' => $this->plugin_url . \'images/star.png\',
          \'supports\' => array(\'title\',\'editor\',\'thumbnail\')
        );

        register_post_type(\'featured-item\', $args);
    }

    function meta_link(){
        global $post;

        if ($post->ID) {
            $link_to_post = get_post_meta( $post->ID , \'link_to_post\', TRUE );
        }

        ?>
    <div class="meta_control">
        <p>Make sure you use the permalink (i.e. written link) rather than the short link (numeric).</p>
        <input type="text" name="link_to_post" class="range" value="<?php if (isset($link_to_post)) { echo esc_attr($link_to_post); } ?>" />
    </div>
        <?php
    }

    function register_featured_meta(){
        add_meta_box("link_to_post", "Link to Post", array($this, \'meta_link\'), "featured-item", "normal", "core");
    }

    function update_featured_meta(){
        global $post;

        if ($post) {
            /* when Trashing a custom post type, it fires the save_post mechanism.
               running a hidden nonce allows the Trash mechanism to ignore all of the
               update_post_meta functions */
            if (isset($_POST[\'featured-item_trash_nonce\'])) {
                $nonce = $_POST[\'featured-item_trash_nonce\'];
            } else {
                $nonce = NULL;
            }

            if (!wp_verify_nonce($nonce, plugin_basename(__FILE__).$post->ID)) {
                return $post->ID;
            }

            // Taxonomy meta
            update_post_meta( $post->ID, "link_to_post", $_POST["link_to_post"] );
        }
    }

}

else :
    exit("Class FeaturedItem already exists.");
endif ;

/* INSTANCE CLASS */
if (!isset($FeaturedItem)) {
    $FeaturedItem = new FeaturedItem();
}

if (isset($FeaturedItem)) {
    add_action( \'init\' , array (&$FeaturedItem, \'register_featured_item\' ));
    add_action( \'admin_enqueue_scripts\', array (&$FeaturedItem, \'enqueue_scripts\' ));
    add_action( \'admin_enqueue_scripts\', array (&$FeaturedItem, \'enqueue_styles\' ));
    add_action( \'admin_init\' , array( &$FeaturedItem, \'register_featured_meta\' ));
    add_action( \'save_post\' , array( &$FeaturedItem, \'update_featured_meta\' ));
}
我还有其他CPT,它们使用非常相似的代码工作。这次我做错了什么?

1 个回复
最合适的回答,由SO网友:turbonerd 整理而成

好的,这实际上是另一个插件的问题。

使用多个自定义帖子类型时save_post 行动,awp_nonce 应用于验证操作实际来自哪个插件。

没有这个,我所有的CPT都想解雇这个CPTsave_post 这一行动最终破坏了所有这些。哦!

结束

相关推荐