1

If the custom post type is known (e.g., 'reference') then the add_action is

add_action( 'publish_reference' , 'run_new_post_code' );

Great. But what is the proper code for an unknown custom post type? I tried to add a text box so the custom_post_type could be added by the end-user. This is then called in $custom_post_type =. A wp_die shows the $custom_post_type is returned correctly. But the publish_{$custom_post_type} must be the wrong way to fire the add_action because this fails.

$custom_post_type = $options['custom_post_type'];
add_action( 'publish_{$custom_post_type}' , 'run_new_post_code' );

What is a good way to get an add_action written so it'll work for any customer's custom_post_type?

Update: This works but is still only for one custom post type. What about multiple custom post types?

$custom_post_type = $options['custom_post_type'];
add_action( 'publish_' . $custom_post_type , 'run_new_post_code' );
1

The first one does not work because to use that format you need double quotes.

add_action( "publish_{$custom_post_type}" , 'run_new_post_code' );

That will let the wrapped variable name be processed properly.

To do something for all custom post types you probably just need to grab the list of custom post types and cycle through them adding an action for each one.

If you only want to do it for some of them, then you will have to give the users the ability to set an option somewhere to list which types to do each thing for.

<?php
$post_type_names = get_post_types( array(), 'names' );
foreach ( $post_type_names as $name ) {
   add_action( "publish_{$name}", 'run_new_post_code' );
}
?>

The above will cycle through each found post type.

See get_post_types for the list of arguments and how to use them.

  • Thank you for the reply. I wasn't able to use the double quotes (no idea why) but decided to build a multi-select in the plugin options page that grabs all post types then loop through the chosen options in order to get the code to run. – LPH Feb 15 '15 at 2:03
  • Creating a option and loop through the chosen post types is not the same that what you asked for: "add action for all custom post types". For that, birgire answer is much better that this one. – cybmeta Feb 15 '15 at 9:46
1

You might want to use the transition_post_status hook instead, for example:

add_action( 'transition_post_status', 
    function( $new_status, $old_status, $post )
    {
        if(     'cpt' === $post->post_type 
            &&  'publish' === $new_status
            &&  'publish' !== $old_status
        )
        {
            // do stuff
        } 
    }
, 10, 3 );

to get a better control into the post status changes and easily check for your post types.

To execute it for all custom post types (as requested in the answer) we can check the _built-in property of the post type object:

add_action( 'transition_post_status', 
    function( $new_status, $old_status, $post ) {

        // get post type object
        $post_type_object = get_post_type_object( $post->post_type );

        // Check is the post type object is not built-in (that is, it is a custom post type)
        // and check that the transition is from "some status" to "publish"
        if(     ! $post_type_object->_builtin
            &&  'publish' === $new_status
            &&  'publish' !== $old_status
        ) {
            // do stuff
        } 
    }
, 10, 3 );
  • I think the answer should include the checking of the _built-in property of the object type, so it can match the "add the action to all custom post types" requested in the answer. – cybmeta Feb 15 '15 at 9:55
  • Thanks for the edit @cybmeta, sounds like a good idea. I must admit I'm not absolutely sure what the OP is looking for here ;-) – birgire Feb 15 '15 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.