Created
September 5, 2017 20:40
-
-
Save malcalevak/ba05b4fbed0c6e33ac8c18c1451bd857 to your computer and use it in GitHub Desktop.
WordPress: Set default category for a new post using links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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