Support » Plugins » Hacks » Create term upon Page Creation

  • Resolved jonboldendesign


    Hey all,

    I’m trying to automate something for a client so they’re less likely to forget an important step. Is there way to automate a term creation upon page creation. In other words, if I create a page called “New Page” with slug “new-page” can I generate a new term called “new-page” if it doesn’t exists behind-the-scenes?


Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz


    You could hook an action that fires when a new page is inserted. You could extract the slug from get_permalink(), though there may be a better way. Then use wp_insert_term().

    One action to hook would be ‘{$new_status}_{$post->post_type}’ which may resolve to something like ‘publish_page’, but you’ll have to investigate to get the exact terms.

    Thanks, Bcworkz

    Would I just add this to the functions.php of my theme? I’m new to hooks but I think I can figure this out.

    Moderator bcworkz


    Adding to functions.php should work. There’s a few things that only work from a plugin, mostly related to admin panels, so this should work fine from a theme.

    It’s well worth the time to learn about using hooks, it’s really the only good way to modify how WP works. Then the only problem is trying to find the right hook for the job.

    Variable hook names like the one I suggested are a bit tricky as you need to do some testing or digging to determine the correct variable values. You could use the action ‘transition_post_status’ to test for the needed values, or just make your code work for this action instead, though it’d be less efficient because it’ll fire on any transition, instead of just the one you’re interested in.

    “I think I can figure this out.” — that’s the attitude I like to see! Happy coding/learning.

    Thanks! I actually think I figured it out. I left out some of the details as to not confuse you. I’m actually using only with a custom post type called “home-blog-project”

    Here’s what I came up with

    // Creates a new category automatically for new Home Build posts
    function create_home_build_category_on_new_page($post_id) {
    	$post = get_post($post_id);
    	if ($post->post_type == 'home-blog-project') {
    		$post->post_name, // the term
    		'home_blog_category', // the taxonomy
    			'description'=> 'Home Build category for '.$post->post_name,
    			'slug' => $post->slug
    	add_action('edit_post', 'create_home_build_category_on_new_page');

    So when you add a new page with custom post type “Home Blog Project” it creates a category automatically with the same slug as the page so my page template will filter the loop with that category. I created the new post type using a plugin called “More Types”.

    The only thing that seems weird to me is that the action tag is “edit_post” but it works when you add a post. I assume the only complication will arise if I ever change the slugname for the page, it will create a new category instead of renaming the existing one. One step at a time!

    Moderator bcworkz


    Looks good! There’s many examples of functions and filters in WP (and PHP) that have name elements like edit, update, change, etc. that will actually work on new objects or create them as needed. One those lovely quirks you learn to live with.

    When you feel ambitious, do poke around with the parameters of ‘transition_post_status’ action, you’ll find some combination that only exists for brand new posts, avoiding the erroneous new category potential.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Create term upon Page Creation’ is closed to new replies.