Skip to content

Instantly share code, notes, and snippets.

@malcalevak
Created September 5, 2017 20:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malcalevak/ba05b4fbed0c6e33ac8c18c1451bd857 to your computer and use it in GitHub Desktop.
Save malcalevak/ba05b4fbed0c6e33ac8c18c1451bd857 to your computer and use it in GitHub Desktop.
WordPress: Set default category for a new post using links
function set_category () {
global $post;
//Check for a category parameter in our URL, and sanitize it as a string
$category_slug = filter_input(INPUT_GET, 'category', FILTER_SANITIZE_STRING, array("options" => array("default" => 0)));
//If we've got a category by that name, set the post terms for it
if ( $category = get_category_by_slug($category_slug) ) {
wp_set_post_terms( $post->ID, array($category->term_id), 'category' );
}
}
//hook it into our post-new.php specific action hook
add_action( 'admin_head-post-new.php', 'set_category', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment