2

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

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'));
        }
    }
}
6
  • It seems that get_current_screen() can't be called from __construct as it is not available until AFTER admin_init. Any thoughts on how to work around that? – edeneye Oct 15 '13 at 12:47
  • @user955664 In the __construct add an action for admin_init and add an associated method to the class (which will be called on admin_init) then do your get_current_screen handling there. Updated answer for Admin init. – Twifty Oct 15 '13 at 13:35
  • Thank you again for your help. It seems that get_current_screen() can't be called in admin_init though. From WP: 'The function (get_current_screen()) returns null if called from the admin_init hook. It should be OK to use in a later hook.' Perhaps, I'm overly complicating things by trying to save some repetitive code. – edeneye Oct 15 '13 at 14:55
  • @user955664 Try hooking admin_head instead. It's a little difficult to advise without knowing exactly what you're trying to accomplish. – Twifty Oct 15 '13 at 15:02
  • The way you've outlined it is exactly what I'm trying to do. I only want to call hooks based on whether or not they are actually needed according to the post_type. I'm not sure if this saves overhead, but it does save repetitive code within all the functions to test for the post_type. I'll give admin_head a shot. Thank you. – edeneye Oct 15 '13 at 17:04
1
add_Action('init','call_it_to_hook');
function call_it_to_hook() {
    global $post;
    if($post->post_type == 'custom_post_type') {
        add_Action('hook name', 'callback_function');
    }
}

callback_function() {
echo "hey";
// thats my place , can do whatever i want here
}
4
  • why double-nest the eventual callback? – Chip Bennett Oct 9 '13 at 19:24
  • how else we can do it ? – Prince Singh Oct 9 '13 at 19:27
  • Grr... I misread the original question. Nevermind. :) – Chip Bennett Oct 9 '13 at 19:47
  • It doesn't seem that post_type is available this early. After thinking about it, it does make sense as a custom post type has yet to be registered until somewhere within 'init'. – edeneye Oct 15 '13 at 13:32

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.