WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
Hi and welcome! Can you post what you've already tried? – brasofilo Nov 14 '12 at 21:17
up vote 2 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 )
    <script language="javascript" type="text/javascript">
            // 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; });

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

I used this script to select a category using jQuery.

Inside wp-admin/post-new.php

$category_name_check = "My Category Name";
echo "<script type=\"text/javascript\">";
echo "jQuery( \"label:contains('$category_name_check') > input\" ).attr('checked', true)";
echo "</script>";

where $category_name_check is the name of the category to be checked.

share|improve this answer

Your Answer


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.