I am trying to use the above two functions within a plugin I am trying to build and obviously they don't work because plugins are loaded before a page is ready and before a post is loaded to know what post type it is.

Which that said, how can I work around that? I am just trying to do something simple like so:

if ( is_single() && get_post_type() == 'boxy' ) {
$setting = 200;

But this statement never becomes true per above reasons. Thanks for the help!

1 Answer 1


You need to hook into an appropriate place within the WordPress load sequence.

For example,

add_action('init', 'my_function'); //is too early and won't work!

Where as,

add_action('wp', 'my_function'); //will work!

And so too will,

add_action('template_redirect', 'my_function'); //this works too!

In fact template_redirect fires just before rendering the page/post to the viewer so its an appropriate place to hook into to perform your action.

Take the following example, if it were placed within your plugin file, will successfully pass the is_single conditional check, then proceed to add a filter which replaces the_content with a completely custom result, in this case a simple string which outputs new content will be displayed in place of what is held in the database.

function plugin_function(){
    if(is_single()) {
        add_filter('the_content', 'filter_content');
        function filter_content(){
            echo 'new content';

add_action('template_redirect', 'plugin_function');

As an example, try changing the last line above to read,

add_action('init', 'plugin_function'); 

Using the init hook is to early, instead of seeing our filtered content as in the function above, you'll notice the regular post content from the database is shown instead.

Hope this helps.


Since you mentioned in your comments you're using a CLASS, are you using a constructor for your add_action/add_filter?


add_action( 'template_redirect', array( 'YourClassNmae', 'init' ) );

class YourClassNmae {

    public static function init() {
        $class = __CLASS__;
        new $class;

    public function __construct() {
         //your actions/filters are to be added here... 
         add_filter('the_content', array( $this, 'filter_content') );

         public function filter_content(){
             echo 'new content';

Codex Reference: Using add_action with a class

  • Thanks for the reply. I think I have tried template_redirect but it didn't work. Maybe I am not applying it in the right place? I am writing this plugin in an object style with a class and I am using this to instantiate my classes add_action( 'plugins_loaded', 'bbe_run_plugin' ); and I have tried changing that to 'template_redirect' and didn't seem to work. Thanks for the help
    – Rick
    Sep 16, 2012 at 1:26
  • @Rick Can you please post your full code, if its excessively large you can trim it down to relevant context, or use pastebin. Also while you're at it, try is_singular('boxy') instead of get_post_type which by the way means you can drop the is_single because is_singular will be running that check for you.
    – Adam
    Sep 16, 2012 at 12:03
  • @Rick See above for an example CLASS structure in your plugin that should work for you.
    – Adam
    Sep 16, 2012 at 16:47
  • thanks again for the patience and reply. Here is a sample code of how I currently have it and of course I trimmed out most of it and left only necessary items. Please see -> pastebin.com/JYXvxa3u
    – Rick
    Sep 17, 2012 at 0:17
  • might be nice to edit that pastebin into your original post Dec 13, 2012 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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