Comments

  1. hello there,
    first of thank you for your great plugin i’m using shiba Mlib and it works a treat. I am currently looking at a way to set the page attributes metabox on the gallery post page so i can assign different templates to different galleries.
    Could you please point me in the right direction, i have purchased the hermes theme from themeforest but the DEV hasn’t been able to help me.
    kind regards
    Chris

  2. Vince Kurzawa says:

    In your last screenshot I noticed your “Excerpt” box has been renamed to “Gallery Description.” I’ve been looking all over Google and can’t find a way to do this.
    Would you let me know, or point me in the right direction of how to do this?

    • That is actually a custom metabox that I added with -
      add_meta_box( ‘post-content-div’, __(‘Gallery Description’), ‘gallery_description_metabox’, ‘gallery’, ‘normal’, ‘high’);

      • Vince Kurzawa says:

        Oh cool. Thanks for the code snippet, it helps a bunch. There is just so much to learn about the WordPress API!

  3. Ups! Sorry, delete my earlier post. Forgot to write my question.
    Cant get the code to work, have tried everything. Can you se whats wrong?

  4. this would have been a great tut had you not left out the part about retrieving the information from the database. How do I get the values from the radio buttons?

    • I am not sure I understand your question.

      In the save_gallery_data function, values from the radio buttons ($_POST['gallery_type']) are retrieved and stored (line 22).

  5. I’ve made a blank example for custom meta boxes on posts, and give explanation on how to insert them into custom post types as well here. I’d love to know what you think.

  6. Hi
    How i can restrict meta box category with arg child-of ?

    I created a plugin with
    add_action(‘load-post.php’, array(‘Myplugin’,'loadpost’));
    And
    function loadpost() {
    remove_meta_box(‘categorydiv’, ‘post’, ‘normal’);
    add_meta_box(‘categorydiv’, _(‘Category’), ‘post_categories_meta_box’, ‘post’, ‘side’, ‘core’, array( ‘taxonomy’ => ‘category’, ‘child_of’ => 80));
    }
    Have any idea ?
    Thanks for great blog

    • I find with this function
      wp_terms_checklist($post->ID, array( ‘taxonomy’ => $taxonomy, ‘descendants_and_self’ => $wp_cat_id_mother, ‘selected_cats’ => $idCatSelected ) )
      So
      1) remove_meta_box(‘categorydiv’, ‘post’, ‘normal’);
      2) add_meta_box(‘categorydiv1′, _(‘Categories’), array( ‘MyPluginClass’, ‘display_restrict_category’), ‘post’, ‘side’, ‘core’);
      3)
      function display_restrict_category( ) {

      wp_terms_checklist($post->ID, array( ‘taxonomy’ => $taxonomy, ‘descendants_and_self’ => $wp_cat_id_mother, ‘selected_cats’ => $idCatSelected ) )

      }

      Thanks

  7. Here’s a good plugin that handles all the custom metaboxes for you as well as custom post types. http://wordpress.org/extend/plugins/custom-metaboxes/

  8. SOLVED IT !!!

    Its necesary to add this action

    add_action(‘admin_menu’, ‘architecture_admin_menu’);

    • Where do you add the action? beginning or end of the code?

      Would be great to see the code in full!

      -J

      • I usually add the admin_menu hook at the beginning of my theme functions.php file or at the beginning of my main plugin file. I put it together with the init and admin_init hooks. When I use class wrappers, I put it within the class constructor function. If you want to look at an example plugin with these function calls then check out the Shiba Example plugin.

        The register_post_type function call I execute from my init function.

        Both the Shiba Media Library plugin and Shiba Custom Background plugin use custom post type metaboxes. Full working code can be found there but the plugins also contain a whole bunch of other stuff.

  9. Ey men! your last posts about custom post types are amazing!! I just created my first custom type, but when trying to add a meta box gives me this error.

    Fatal error: Call to undefined function add_meta_box() in /home/xxx/public_html/wp-content/themes/xxx/custom-types.php on line 43

    custom-types is an include in functions.php

    I am using wordpress 3.0

    This is the code in custom-types.php

    	$labels = array(
    		'name' => _x('Architecture', 'post type general name'),
    		'singular_name' => _x('Architecture', 'post type singular name'),
    		'add_new' => _x('Add New', 'work'),
    		'add_new_item' => __("Add New work"),
    		'edit_item' => __("Edit work"),
    		'new_item' => __("New work"),
    		'view_item' => __("View work"),
    		'search_items' => __("Search Works"),
    		'not_found' =>  __('No work found'),
    		'not_found_in_trash' => __('No work found in Trash'), 
    		'parent_item_colon' => ''
    	  );
    	  $args = array(
    		'labels' => $labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true, 
    		'query_var' => true,
    		'rewrite' => true,
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'menu_position' => null,
    		'supports' => array('title','editor')
    	  ); 
    	  register_post_type('architecture',$args);
    	  
    	  function architecture_type_metabox($post) {
    		
    	}
    	
    	add_meta_box('architecture-type-div', __('Datos tecnicos'),  'architecture_type_metabox', 'architecture', 'normal', 'low');
    

    Please! help, wp is correctly updated to 3.0, i can find the function add_meta_box and cant find why this happens!

    ++Thanks++

    • FYI, this part is not “out of the box” so you have to wrap it into a function and use add_action such as


      add_action( 'admin_init', 'news_meta_boxes', 1 );

      // Add meta box goes into our admin_init function
      function news_meta_boxes() {
      add_meta_box('gallery-type-div', __('Gallery Type'), 'gallery_type_metabox', 'news', 'normal', 'high');
      }

      This also lets you add all your meta boxes in one add_action as referenced in the codex at http://codex.wordpress.org/Function_Reference/add_meta_box

  10. Thanks very much, i’ve been searching for how to do meta box stuff on the new custom post types and this article has finally helped me to understand.

  11. Thanks for this, very useful!

    I created a checkbox to flag featured “Best Practices” on a site I am putting together. Perhaps this will be helpful to your readers:

    add_action('admin_menu', 'featured_add_custom_box');
    add_action('save_post', 'featured_save_postdata');
    
    /* Adds a custom section to the "advanced" Best Practices */
    function featured_add_custom_box() {
    	add_meta_box( 'featured_sectionid', __( 'Featured', 'featured_textdomain' ), 'featured_inner_custom_box', 'bestpractices', 'advanced' );
    }
       
    /* Prints the inner fields for the custom post/page section */
    function featured_inner_custom_box( $post ) {
    	
    	$featured = get_post_meta($post->ID, '_featured', true);
    	if ($featured == 1) $checked = 'checked="checked"'; else $checked = '';
    	
      echo 'ID) . '" />';
      echo '' . __("Featured?", 'featured_textdomain' ) . ' ';
      echo '';
    }
    
    /* When the post is saved, saves our custom data */
    function featured_save_postdata( $post_id ) {
    
    	if ( !wp_verify_nonce( $_POST['featured_noncename'], 'featured'.$post_id ))
    		return $post_id;
    
      if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return $post_id;
    
      if ( !current_user_can( 'edit_page', $post_id ) )
        return $post_id;
    
    	$post = get_post($post_id);
    	if ($post->post_type == 'bestpractices') {
    		if(esc_attr($_POST['featured'] == "on")) $val = 1; else $val = 0;
    		update_post_meta($post_id, '_featured', $val );
    		return(esc_attr($_POST['featured']));
    	}
    }
    
  12. Oh, my gosh, thank you thank you thank you. I have been dealing with this stupid nonce thing looking for help. I stumbled onto your site and this method works great. Thank you so much!

Trackbacks

  1. [...] Read the rest here: Add a Metabox to Your Custom Post Type Screen [...]

Speak Your Mind