Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm sure this must be something simple, but I can't for the life of me work out why this isn't working.

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

I have other CPTs which work using very similar code. What have I done wrong this time?

share|improve this question
    
I'm still having problems with this. Does nobody have any ideas? It has to be something simple surely...! –  dunc Jan 20 '12 at 0:17
add comment

1 Answer 1

up vote 0 down vote accepted

OK, this was actually a problem with another plugin.

When using more than one Custom Post Type and the save_post action, a wp_nonce should be used to verify which plugin the action is actually coming from.

Without this, all of my CPTs were trying to fire this CPT's save_post action which consequently broke all of them. D'oh!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.