0

I am developing a WordPress plugin and can't conditionally add filters based on the conditional tags.

This is my code:

add_Action('init', 'determine_location');

function determine_location() {
$d = get_option('display_locations'); //this is a checkbox field in the plugins settings with 7 options
    if (isset($d)) {
        if ($d[0] == 'home') {
            if (is_home()) {
                add_filter('the_excerpt', 'insert_buttons_to_post_top');
                add_filter('the_content', 'insert_buttons_to_post_top');
            }
        } else if ($d == 'post' || $d == 'post') {
            if (is_single()) {
                add_filter('the_content', 'insert_buttons_to_post_top');
            }
        } else if ($d[0] == 'page' || $d == 'page' || $d == 'page') {
            if (is_page()) {
                add_filter('the_content', 'insert_buttons_to_post_top');
            }
        } else if ($d[0] == 'category' || $d[1] == 'category' || $d[2] == 'category' || $d[3] == 'category') {
            if (is_category()) {
                add_filter('the_excerpt', 'insert_buttons_to_post_top');
                add_filter('the_content', 'insert_buttons_to_post_top');
            }
        } else if ($d[0] == 'tag' || $d[1] == 'tag' || $d[2] == 'tag' || $d[3] == 'tag' || $d[4] == 'tag') {
            if (is_tag()) {
                add_filter('the_excerpt', 'insert_buttons_to_post_top');
                add_filter('the_content', 'insert_buttons_to_post_top');
            }
        } else if ($d[0] == 'archive' || $d[1] == 'archive' || $d[2] == 'archive' || $d[3] == 'archive' || $d[4] == 'archive' || $d[5] == 'archive') {
            if (is_archive()) {
                add_filter('the_excerpt', 'insert_buttons_to_post_top');
                add_filter('the_content', 'insert_buttons_to_post_top');
            }
        }
    }

}

function insert_buttons_to_post_top(){
   return "<div>Output</div>"
}

Currently this code shows no output. What is wrong here?

3
  • Can you show us the scope where you are using the conditional tags? Are they inside an action hook? If so, which one?
    – cybmeta
    Sep 11 '14 at 9:55
  • Just updated the question. It is in the init hook Sep 11 '14 at 11:44
  • 1
    It should be add_action, not add_Action Sep 11 '14 at 11:45
-1

The conditional tags must be run after WP object is set up. The wp action hook is the first one where you can use these conditionals safely. See the warning message in the conditional tags documentation.

So, you should hook into wp action or later event; also, the correct function name is add_action, not add_Action:

add_action('wp', 'determine_location');

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.