• Hi

    I was just wondering how I could make my parent category automatically checked (or even forced checked would be better)

    I have a large number of posters who must check that base category box in addition to others.

    Any suggestions?

    ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • Can’t see any hooks available for forcing or selecting a category automatically.

    You could do it with jQuery however, it’s just not guaranteed to work for all users (they may have JS disabled as one example).

    It’s still an option all the same.

    add_action( 'admin_print_scripts-post.php', 'load_jquery_if_necessary' );
    add_action( 'admin_print_scripts-post-new.php', 'load_jquery_if_necessary' );
    add_action( 'admin_head-post.php', 'disable_and_check_cat' );
    add_action( 'admin_head-post-new.php', 'disable_and_check_cat' );
    function load_jquery_if_necessary() {
    	wp_enqueue_script( 'jquery' );
    }
    function disable_and_check_cat() {
    	?>
    	<script type="text/javascript">
    	jQuery(document).ready(function($){
    		var required_cat = $('input#in-category-3');
    		if( required_cat )
    			required_cat.attr('disabled','disabled');
    		if( !required_cat.attr('checked')  )
    			required_cat.attr('checked','checked');
    	});
    	</script>
    	<?php
    }

    Change the 3 for the category ID of the category you want selected and disabled.

    For reference, this is the line that requires adjustment.

    var required_cat = $('input#in-category-3');

    Thread Starter SamBarker

    (@sambarker)

    ok thanks a million for that.

    So should I replace that line or write the top code in after it?

    And which file should I do all this in?

    ??

    You can set the default category in the Settings > Writing section. This will select the one you want by default, however it will allow them to uncheck it.

    Thread Starter SamBarker

    (@sambarker)

    hmmm i have my desired category set as default but it doesn’t seem to want to play the game when it comes to autochecking

    Would there be a reason for this?

    Copy all the code (into your themes funcitons file, or a plugin, after the opeing PHP tag <?php on a new line).

    Update the the line i mentioned before to reflect the correct category ID (currently 3).

    I have the same problem. I added the code above with the appropriate category id, but the category remains unchecked. This is a site using wp 2.7 (I know I need to upgrade.), so here is a link to my functions.php, which is very short.

    https://wp.pastebin.com/PhfErR61

    Did I do something wrong?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Auto-Check Category’ is closed to new replies.