• Resolved Magnus P White

    (@magnuspwhite)


    Are plugins inside of the loop?

    I ask this because I am developing a plugin trying to get the page ID, however, I am unable to call any of the functions from inside of the loop (for example the_ID();).

    I have also be unable to run the equivalent from outside of the loop.

    Many thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • No, they are not. You can pass the ID of the post you are processing to your plugin or make a new loop inside it, depending on what you are trying to do.

    Thread Starter Magnus P White

    (@magnuspwhite)

    Thank you for confirming that.

    I am trying to get the page ID in my plugin. I seem unable to access the the_ID() function whether I set global $wp_query; or not.

    I have tried a number of methods suggested on different wordpress pages but cannot get a definitive answer.

    I use “pretty” permalinks and am told that ‘the_ID();’ does not work with them. A solution suggested was:

    global $wp_query;
    $page_object = $wp_query->get_queried_object();
    $page_id     = $wp_query->get_queried_object_id();

    I got the error:
    Fatal error: Call to a member function get_queried_object() on a non-object in /var/www/...

    Further I have tried the following for inside and outside of the loop, respectively:

    function function_name() {
    global $post;
    $thePostID = $post->ID;
    }
    
    function function_name() {
    global $wp_query;
    $thePostID = $wp_query->post->ID;
    }

    Please can anyone help or provide a reason as to why there is an issue. Alternatively, how to get the page id under any circumstance when called from a plugin function.

    Many thanks.

    Where is your code located ? In a hook ?
    You should at least have access to the WP_Query object, except if you’re in a very early state.

    What’s the output of var_dump( $wp_query ); ?

    As loic.etibe pointed out, you need hook into WordPress core at the right point.

    Here is the list of core hooks the happen during a page load:
    http://codex.wordpress.org/Plugin_API/Action_Reference
    For simplicity, all your plugin code is run by step 2 ‘plugins_loaded’.

    What you need to do is define some actions or filters inside The Loop using these:
    http://codex.wordpress.org/Function_Reference/add_action
    http://codex.wordpress.org/Function_Reference/add_filter

    Then when WordPress core comes to that point in the code (in your case, inside the loop), then you functions will be triggered.

    You starting point for learning this from the codex is probably:
    http://codex.wordpress.org/Plugin_API

    Thread Starter Magnus P White

    (@magnuspwhite)

    Thanks guys that really helped. I used add_action('the_post', 'add_toolbar'); to load the function at the beginning of the post to allow be to use get_the_ID();

    I then posted JS in the footer rather than the header as the header had already been loaded.

    Thanks again for the help!

    P.S. If you were wondering what it was for here is my plugin (http://wordpress.org/extend/plugins/atbar/). It is an accessibility toolbar (ATbar).

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Are plugins inside of the loop?’ is closed to new replies.