• Resolved IM_natascha

    (@im_natascha)


    Hello guys,

    i try to make a new posttype (success) with one registered taxonomy (success). This taxonomy is called Gender and should therefore contain only “male” and “female” which i add via PHP instantly after the taxonomy-registration.
    To avoid any changes of my taxonomy (additional terms) I set all capabilities to “noone” so not even an administrator may change or add any term in this taxonomy.

    I experienced in my following tests that anyone who can edit/write a post may insert new terms into my taxonomy – maybe by accident or on purpose, doesn’t matter.

    So all i want is to eliminate this functionallity with this posttype. I am searching for a hook where i can prevent inserting a new taxonomy or one which is called when the term was inserted (i don’t have any problems with removing it after it was inserted if it doesn’t match my conditions).

    Yet i tried out following hooks: create_category, save_post, edit_post. but none will work. For testing reasons i tried debugging in my function with adding
    add_action(‘admin_notices’, ‘echo “….”;’);
    to it. But after saving a post, nothing is printed out. So i guess i picked the wrong hooks.

    May anyone know a solution?

    Another question which would solve my primary problem would be how to provide a drop-down menu for picking this specific taxonomy? Would prevent user-generated-input and therefore new terms in my taxonomy.

    If anyone wants to know why i need this: my plugin should allow too many (in my opinion) people to create posts. So i try to minimize possible errors, as always – do not allow anything not specifically needed by the DAU.

    Thanks a lot.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi,

    Try this:

    add_filter('pre_insert_term', 'check_right_term');
    function check_right_term($term, $taxonomy)
    {
      if($taxonomy == 'gender' && !in_array($term, array('male', 'female'))
        return new WP_Error('invalid_term', __('Invalid term'));
    
      return $term;
    
    }

    Thread Starter IM_natascha

    (@im_natascha)

    Thank you! Sounds pretty fine but i challenge an error with your code (beside the ‘)’ which is missing :P)

    Missing argument 2 for NK_Association::check_right_term() […]

    You call two parameters, but it seems like the filter isn’t passing them correctly. I didn’t work a lot with filters yet so i can’t see the error at first sight, might you?

    P.S.: Impressed by answering-speed ??

    This code works only if terms others than male/female dont exists.
    Try to change new WP_Error('invalid_term', __('Invalid term'));
    to new WP_Error();

    Oh, thats add_filter chnge it to:
    add_filter('pre_insert_term', 'check_right_term', 10, 2);

    About terms edit box.
    Thats quite tricky. You should remove default tags metabox using remove_meta_box on ‘add_meta_boxes’ action and then add your metabox with strict select term selection.
    You can find default tags metabox render function in wp-admin/includes/meta-boxes.php it called post_tags_meta_box
    You can use it like prototype for your metabox.

    Thread Starter IM_natascha

    (@im_natascha)

    From what i can see now, your code works fine (if i do not forget to change “gender” to my own taxonomy-name *gg*) after adding the 2 optional parameters.

    at last but not least: is there a trick how i can forbid to insert the post into the database if the term is wrong?
    otherwise i will have a look at your suggested add_meta_boxes-Action tomorrow.

    Thread Starter IM_natascha

    (@im_natascha)

    So, does anyone know a way how to stop the process of inserting a new post if the inserted term was not correct?

    Well, you can try to check $_POST[‘tax_input[gender]’] on init action but this will be kind of dirty hack.

    Why not use own meta box with fixed terms?
    If you use custom posts you will work with custom metaboxes anyway.
    Your metabox content will be very simple, something like this:

    <?php $tax_name = 'gender';?>
    <div class="tagsdiv" id="<?php echo $tax_name; ?>">
    	<div class="jaxtag">
    	<div>
    	<p>Description</p>
            <select name="tax_input[<?php echo $tax_name;?>]" id="tax-input-<?php echo $tax_name; ?>">
              <?php foreach(array('male', 'female') as $g):?>
                <option <?php echo ($g == get_terms_to_edit( $post->ID, $tax_name ))?'selected':''><?php echo $g; ?></option>
              <?php endforeach; ?>
            </select>
            </div>
    </div>
    </div>

    Thread Starter IM_natascha

    (@im_natascha)

    Yes, this might work.

    As i do not have any further questions on this topic, i mark it as resolved, thanks to the wpua ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Disable any modification of taxonomies’ is closed to new replies.