• Resolved Dave

    (@dcutri500)


    Hi,

    I’m trying to create a function that fires when a new post type is created and uses the new post type slug in the function. Is there an action hook for this?

    I this what I need?

    cptui_post_register_post_types

    If so, how exactly would I pass the post type to the function? I need my function to fire as soon as the post type is created but only when the post type is created (just once).

    Thanks for any help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Hey @dcutri500

    Just to be certain, are you talking about when registering a post type itself? Or do you mean posts within a post type?

    For example say that I register a movie post type, and then start publishing horror movies in the post type, and this functionality is supposed to happen per movie, only on initial publish?

    Thread Starter Dave

    (@dcutri500)

    Yes, only when the post type itself is created, not for any single post operation.

    When the movie post type is created, do something, but only that one time.

    Thread Starter Dave

    (@dcutri500)

    So, not per post. I mean not when creating or saving a single post. Just when creating the actual post type on the Add New Post Type screen.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I know we have this action hook:

    /**
     * Fires after a post type is updated to our saved options.
     *
     * @since 1.0.0
     *
     * @param array $data Array of post type data that was updated.
     */
    do_action( 'cptui_after_update_post_type', $data );
    

    This runs after saving of a new or existing post type. $data would be an array of all the settings, from the submitted form. For example you could get the post type slug from $data['cpt_custom_post_type']['name']

    The only part that we don’t have would be a way to limit to just once. That could be easily coded out though for your usecase. Perhaps a quick single row option that stores slugs for which post types have already been run, and if the slug is NOT in the option, your code runs. If it is in there already, return early.

    $ran_types = get_option('my-ran-slugs', [] );
    if ( in_array( $data['cpt_custom_post_type']['name'], $ran_types ) ) {
    	return;
    }
    
    // Run your code here.
    
    $ran_types[] = $data['cpt_custom_post_type']['name'];
    update_option('my-ran-slugs', $ran_types );
    
    Thread Starter Dave

    (@dcutri500)

    Thank you so much! That looks really promising. I will let you know how it turns out.

    Thread Starter Dave

    (@dcutri500)

    Thanks again, Michael. This is working perfectly for me.

    So, whenever a new post type is registered, check a box on the theme options page:

    // Check the box for a new post type on theme options page
    function check_post_type_box_initially($data) {
    
    	// Get the post types that are already checked
    	$checked = get_checked_post_types();
    
    	// Get the post types that have already been registered
    	$already_registered = get_option('already_registered', []);
    
    	// Don't do anything for those post types that are not new
    	if ( in_array( $data['cpt_custom_post_type']['name'], $already_registered ) ) {
    		return;
    	}
    
    	// This is a new post type: add it to the checked and already registered arrays
    	$checked[] = $data['cpt_custom_post_type']['name'];
    	$already_registered[] = $data['cpt_custom_post_type']['name'];
    
    	// Update the database
    	update_option('options_post_type_options_post_types', $checked);
    	update_option('already_registered', $already_registered, 'yes');
    
    }
    add_action( 'cptui_after_update_post_type', 'check_post_type_box_initially' );
    • This reply was modified 2 years, 4 months ago by Dave.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Looks like a winner to me.

    Glad something was able to be done here.

    Let us know if you need anything else.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Action hook for post type creation?’ is closed to new replies.