• Resolved applegateian

    (@applegateian)


    I’ve seen a few plugins that allow you to force at leaste one category for every user. However, what I want to do is ALWAYS force the default category on the user without them being able to remove it.

    They can select other categories additionally, but the default category must always be selected. Is there a way to do this?

    I am using the Restrict Categories plugin to limit the categories to my different user types, but I want them all to be forced to use the default…I cannot find a way to do this.

    Thanks,

    Ian

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter applegateian

    (@applegateian)

    Any thoughts on this guys?

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this in your theme’s functions.php:

    function set_my_categories( $post_ID ) {
    
    	$default_cat_id = 1; // change this to the category ID you want
    
    	if ( wp_is_post_autosave( $post_ID ) || wp_is_post_revision( $post_ID ) ) {
    		return $post_ID;
    	}
    
    	$term_list = wp_get_post_terms( $post_ID, 'category', array( "fields" => "ids" ) );
    	if ( $term_list ) {
    
    		$term_list[] = $default_cat_id ;
    		wp_set_post_categories( $post_ID, array_unique( $term_list ) );
    
    	} else {
    		wp_set_post_categories( $post_ID, array( $default_cat_id ) );
    	}
    
    }
    add_action( 'save_post', 'set_my_categories' );

    This will automatically assign the category with ID 1 when a post is published or updated

    Thread Starter applegateian

    (@applegateian)

    Thanks for the suggestion, currently getting this error with this code:

    Parse error: syntax error, unexpected T_IF in /nfs/c08/h03/mnt/126187/domains/architectsrepublic.com/html/community/wp-content/themes/reverie/functions.php on line 176

    Moderator keesiemeijer

    (@keesiemeijer)

    I just copy pasted it in my theme’s functions.php and get no errors.

    Can you post the full code of your functions.php file with my code in it. See: https://codex.www.ads-software.com/Forum_Welcome#Posting_Code

    Thread Starter applegateian

    (@applegateian)

    My mistake – pasted incorrectly and it works PERFECTLY. The checkbox is there by default, that will do the job. Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Always force one specific category’ is closed to new replies.