Conditionally call add_action depending on post_type?

When building a plugin, I know that you can hook into a function to run some code like

add_action('admin_footer', array($this, 'custom_function'));

And within that function, test for post_type like so:

public function custom_function() {
            $screen = get_current_screen();
            if (self::CUSTOM_POST_TYPE == $screen->post_type) {
                // Do something
            }
        }
}

Over the entire plugin, I’m testing the post_type of the screen in many different functions.

In the spirit of D.R.Y., my question is, is there a way to conditionally add the actions in a group based on the current post_type? Instead of having many functions that make the same test for the post_type like:

    public function custom_function_1() {
                $screen = get_current_screen();
                if (self::CUSTOM_POST_TYPE == $screen->post_type) {
                    // Do something
                }
            }
    }
    public function custom_function_2() {
                $screen = get_current_screen();
                if (self::CUSTOM_POST_TYPE == $screen->post_type) {
                    // Do something
                }
            }
    }

is it possible to test the post_type and then call the actions needed for that post type, something like:

$screen = get_current_screen();
if (self::CUSTOM_POST_TYPE == $screen->post_type) {
    add_action('admin_footer', array($this, 'custom_footer_function'));
    add_action('save_post', array($this, 'custom_save_function'));
    add_action('delete_post', array($this, 'custom_delete_function'));
    add_filter('enter_title_here', array($this, 'custom_title_placeholder'));
}

where the conditional test can be removed from all the functions?

Is there a particular hook where a test and calls like this could take place so many of the plugin’s functions don’t have to have the same code?

Thank you for any advice.

2 Answers
2

Using OOP PHP, add your initializer actions in the __construct and create appropriate methods for handling them. From your handler you can initialize any class variable (once) you want and reference it later without having to do any further checks. Here is an example:

class my_plugin {
    private $post_type;
    private $isCustom = false;

    public function __construct() {
        add_action( 'admin_init', array( $this, 'admin_init' ) );
    }

    public function admin_init() {
        // This method is called only once, so do any initialization here.
        $this->post_type = get_current_screen()->post_type;
        $this->isCustom = self::CUSTOM_POST_TYPE == $this->post_type;
        if ( $this->isCustom ) {
            add_action('admin_footer', array($this, 'custom_footer_function'));
            add_action('save_post', array($this, 'custom_save_function'));
            add_action('delete_post', array($this, 'custom_delete_function'));
            add_filter('enter_title_here', array($this, 'custom_title_placeholder'));
        }
    }
}

Leave a Comment