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 trying to write a function to make certain terms of default category always selected.

When the user is writing a new post, the term checkbox should be checked and he shouldn't be able to unckeck it.

Is it possible?

share|improve this question
1  
Hi and welcome! Can you post what you've already tried? –  brasofilo Nov 14 '12 at 21:17

1 Answer 1

up vote 1 down vote accepted

This can be done with jQuery.

In this example, the modifier is printed when editing an existing post (admin_head-post.php) and when writing a new one (admin_head-post-new.php).

There's a condition to check for the correct post type, as this hooks work with posts, pages and custom post types.

The "Most used" tab is being hidden. To address the categories in that tab, use the identifiers #in-popular-category-CAT_ID.

add_action( 'admin_head-post-new.php', 'wpse_72603_default_categories' );
add_action( 'admin_head-post.php', 'wpse_72603_default_categories' );

function wpse_72603_default_categories()
{
    global $current_screen;

    // If not our post type, do nothing
    if( 'post' != $current_screen->post_type )
        return;
    ?>
    <script language="javascript" type="text/javascript">
        jQuery(document).ready(function($) 
        {
            // Hide the "Most used" tab
            $('#category-tabs .hide-if-no-js').remove();

            // Tick the checkboxes of categories 3 and 9
            $('#in-category-3, #in-category-9').attr('checked', true);

            // Disable the clicks in categories 3 and 9
            $('#in-category-3, #in-category-9, #in-popular-category-9')
            .click(function() { return false; });
        });
    </script>
    <?php
}

Plugin of interest: Category Checklist Tree.

On the post editing screen, after saving a post, you will notice that the checked categories are displayed on top, breaking the category hierarchy. This plugin removes that "feature".

share|improve this answer
    
This is great, it works perfectly, thanks! Just another question: what if I want to do this for every different author? (This is what I was looking for in the beginning: a function to disable authors capability to select certain terms) I tried to retrieve author_meta_id and then add an if statement just before "global" (if id = 1, then do this), but it doesn't work. Too complicated? –  She Hulk Nov 14 '12 at 22:49
    
@SheHulk, if you are talking about different behaviors for different users/user-roles, then you need current_user_can. Couple it with the post type check, or print different scripts based on it. –  brasofilo Nov 15 '12 at 0:00
    
I'll try, thanks again! –  She Hulk Nov 15 '12 at 0:15

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.