WordPress.org

Ready to get started?Download WordPress

Forums

Set Context/Priority of Meta_Box Based on Registered Taxonomy (8 posts)

  1. moatmtn
    Member
    Posted 3 years ago #

    Hello, Talented WP Folks!

    I've created a custom post type and attendant taxonomies in WP 3.0.1. All I want to do now is set the context/priority for the meta box generated for one of the new taxonomies. By default, an hierarchical taxonomy is placed on the 'side', 'low' of the admin menu. I'd like for it to sit right under the 'title' field without my users having to manipulate the GUI.

    I believe the solution lies in register_meta_box_cb, and I was able to remove the default meta box and add a new one in the right position ('normal', 'high'); however, I couldn't get the taxonomy to display in this new meta box--in other words, I'm not trying to add a custom meta box...I just want to reposition the registered taxonomy's meta box, and I have read that to do so one has to use the remove_meta_box and add_meta_box functions.

    So, my question is: is that the right/only way? Or is there a way to set the context/priority the meta_box of a registered taxonomy much more elegantly?

    Thanks in advance for any help you can provide!

    Cheers,
    Steve

  2. Mark / t31os
    Moderator
    Posted 3 years ago #

    Can't see any easy way other than removing and re-adding the metabox in a different position.

    All you need do is remove the existing meta box, then add it back with the same parameters, with exception to setting the context and priorities to your liking.

    The code that registers the taxonomy metaboxes looks like this (for easy reference).

    foreach ( get_object_taxonomies($post_type) as $tax_name ) {
    	$taxonomy = get_taxonomy($tax_name);
    	if ( ! $taxonomy->show_ui )
    		continue;
    
    	$label = $taxonomy->labels->name;
    
    	if ( !is_taxonomy_hierarchical($tax_name) )
    		add_meta_box('tagsdiv-' . $tax_name, $label, 'post_tags_meta_box', $post_type, 'side', 'core', array( 'taxonomy' => $tax_name ));
    	else
    		add_meta_box($tax_name . 'div', $label, 'post_categories_meta_box', $post_type, 'side', 'core', array( 'taxonomy' => $tax_name ));
    }

    Hope that helps... :)

  3. moatmtn
    Member
    Posted 3 years ago #

    Hi, Mark.

    Very helpful indeed! Thank you.

    As an aside, do you know if setting 'label' => false, when registering a new taxonomy would eliminate the need to remove a box (thus requiring that one only add a new meta box)?

    No need to answer that one. I had just read that somewhere and wasn't sure if it was an option.

    Again, thank you!

    Cheers,
    Steve

  4. moatmtn
    Member
    Posted 3 years ago #

    I jumped the gun! Can't get the re-added meta_box to recognize the taxonomy. Here's my code:

    // Register deeds (Activity) post_type
    add_action('init', 'deeds_register');
    
    function deeds_register() {
    
    $labels = array(
    	'name' => _x('Activities', 'post type general name'),
    	'singular_name' => _x('Activity', 'post type singular name'),
    	'add_new' => _x("Post Today's Activity", 'add new post type name'),
    	'add_new_item' => __("Post Today's Activity...They're Waiting"),
    	'edit_item' => __('Edit Activity'),
    	'new_item' => __('New Activity'),
    	'view_item' => __('View Activity'),
    	'search_items' => __('Search Activities'),
    	'not_found' =>  __('Nothing found'),
    	'not_found_in_trash' => __('Nothing found in Trash'),
    	'parent_item_colon' => ''
    );
    
    $args = array(
    	'labels' => $labels,
    	'public' => true,
    	'publicly_queryable' => false,
    	'show_ui' => true,
    	'query_var' => true,
    	'rewrite' => true,
    	'capability_type' => 'post',
    	'hierarchical' => false,
    	'menu_position' => 8,
    	'register_meta_box_cb' => 'move_activitypost_box',
    	'supports' => array('title',)
      ); 
    
    register_post_type( 'deeds' , $args );
    // Register deeds (Activity) taxonomies
    register_taxonomy("activity-type", array("deeds"), array("hierarchical" => true, "label" => "Activity", "rewrite" => true,));
    register_taxonomy("activity-route", array("deeds"), array("hierarchical" => true, "label" => "Route", "labels" => array('all_items' => __('All Routes'),'add_new_item' => __('Add New Route'),), "rewrite" => true));
    
    add_action('do_meta_boxes', 'move_activitypost_box');
    
    function move_activitypost_box() {
    	remove_meta_box( 'activity-typediv', 'deeds', 'side' );
    	add_meta_box( 'activity-typediv', "Activity", 'move_activitypost_box', 'deeds', 'normal', 'high',array( 'taxonomy' => 'activity-type' ));
    }

    Thanks in advance for your help, all!
    -Steve

  5. Mark / t31os
    Moderator
    Posted 3 years ago #

    There's a few errors in your code, but i assume that's due to copy and pasting the relevant parts of your code (no problem).

    As i said before, you need to re-register the same parameters, just changing the position and label (although the variables need to be replaced to), but only a small modification is needed to move_activitypost_box() and the code will work (it's tested so i know it works).

    function move_activitypost_box() {
    	remove_meta_box( 'activity-typediv', 'deeds', 'side' );
    	$t_name = 'activity-type';
    	if ( !is_taxonomy_hierarchical($t_name) )
    		add_meta_box('tagsdiv-' . $t_name, "Activity", 'post_tags_meta_box', 'deeds', 'normal', 'high', array( 'taxonomy' => $t_name ));
    	else
    		add_meta_box($t_name . 'div', "Activity", 'post_categories_meta_box', 'deeds', 'normal', 'high', array( 'taxonomy' => $t_name ));
    
    }

    Hope that helps... ;)

  6. moatmtn
    Member
    Posted 3 years ago #

    Mark,

    Thank you! That worked like a charm!!

    You're far too kind, by the way. And while I appreciate the delicacy with which you point out errors in my code, I am enthusiastic about learning, so please feel free to point them out if you have time...I have NO ego when it comes to this stuff. I just want to learn.

    Again, many, many thanks! Hope things are good in England.

    Cheers,
    Steve

  7. Mark / t31os
    Moderator
    Posted 3 years ago #

    You're welcome.. :)

    The errors are only minor, not really worth noting if they're due to copying and pasting the code, but i'll detail them below for clarity..

    The first function is missing a closing curly bracket (so all the code sits inside an unclosed function), and there's an extra comma on the end of a couple of your arrays (minor errors).

  8. moatmtn
    Member
    Posted 3 years ago #

    Hi again, Mark.

    Once more, thanks for pointing those errors out. I despise bad code, so I went in and cleaned everything up.

    Cheers!
    -Steve

Topic Closed

This topic has been closed to new replies.

About this Topic