• I tried to use a few functions into the top of my plugin file but it always returns an empty string or even becomes to display the blank page anywhere on the site. For example:
    print_r(is_category('9'));
    returns nothing.

    I need to use some WP function to know the current category ID in plugin, so that I could enable it for specific category.

    Anybody knows why the functions doesn’t work in plugin?

    I apologies for my English.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Are you just doing that print_r() or are you trying to do that within function for add_filter() or add_action() within a loop or not?

    Can you provide more of a code sample via pastebin.com? The output is often dependent upon how, when, and what you are doing…

    Thread Starter artnik7

    (@artnik7)

    Yes I’m trying to use functions without add_filter() or add_action(). This could be the problem?

    Thread Starter artnik7

    (@artnik7)

    I’m trying to do something like this:

    if (is_category('9') {
        // plugin content
    })

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Yes I’m trying to use functions without add_filter() or add_action(). This could be the problem?

    Well it could be. It depends on what you want to do.

    For example, if you want to modify the_content so that posts within category ID 9 get an added styled div wrapped around the content then it’s

    add_filter( 'the_content' , 'ar_category_9' );
    function ar_category_9( $content ) {
       if ( is_category( '9' ) {
          $content = '<div class="special-css">' . $content . '</div>';
       }
       return $content;
    }

    That will modify the_content so that any post in that category will get wrapped in an additional div.

    It’s just an example, but you see what I mean. Depending on what you want to do depends on how you do it. ??

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Note: I’m missing a closing ) above, but still you get the idea. ??

    if ( is_category( '9' ) )  {
    Thread Starter artnik7

    (@artnik7)

    Thanks, Jan. I have tried a lot of various WP functions to get categ. id, but seems like only one of them works in plugin files – get_the_category().

    So, if someone interesting in, here is the code, that helps to find current category:

    foreach(get_the_category() as $category)  $catid = $category->cat_ID;
    if ($catid == 9) {
        // plugin content
    }

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Any WP function is not working in the top of the plugin file’ is closed to new replies.