Support » Fixing WordPress » Earliest Actions Plugin Hook Within The Loop

  • I need to get the post ID as soon as possible for a plugin I’m writing, however I have to “wait” until the loop starts (right?). So, I need to know the earliest plugin hook within the loop. There doesn’t really seem to be anything on this list that works right: The only ones that have worked so far for me are wp_meta and wp_footer.

    Thanks again.

Viewing 8 replies - 1 through 8 (of 8 total)
  • skippy



    You don’t need to wait until you’re in The Loop to fetch the post ID.

    I use the following snippet in my sidebar, which runs before the Loop:
    if (is_single()) {
    // we're looking at a single post
    $this_id = $wp_query->post->ID;

    I am still using this in a plugin format, so I still need an earlier hook. I can’t use ‘init’ because that is too early. Also, I’m only looking at pages, not posts.




    use plugins_loaded.

    That won’t allow me to get the post ID your way. I’m not really sure the best way to achieve this. Useful comments are in my code.

    Here is the code I’m putting together:

    function check_for_logged_in_user() {
    global $user_ID;
    // is user already logged in?
    if (” == $user_ID) {
    // is he on a page only?
    if(is_page()) {
    global $id, $wpdb;
    $isrestricted = $wpdb->get_var(“select restricted from $wpdb->posts where ID=$id”);
    // is this a restricted page?
    if ($isrestricted == 1) {
    // auth_redirect(); // better way of redirecting?
    } // else { echo “not a restricted page”; }
    } // else { echo “not a single post”; }
    } // else { echo “user id present”; }
    add_action(‘plugins_loaded’, ‘check_for_logged_in_user’);

    The more I think about this, the more I don’t think it’s quite possible in this form. The crux of the problem is this:

    It seems I have to wait a small amount of time into the creation of the page to be able to determine the post ID. However, in doing that, I won’t be able to redirect if the person isn’t logged in.

    The only alternitive I can think of is to just write a filter plugin that just gobble’s up all the content and writes the login form there perhaps.





    You cannot redirect using the location("Header: ...") method once content has been sent to the browser.

    A plugin registered against wp_head() should have be able to use is_single() in the way that you need. Does that not work?

    is_single will return false when trying to detect pages, so I have to use is_page().

    And when I try to get the ID via your method, it doesn’t return anything.

    Ah ha! Because ‘$wp_query’ wasn’t ‘global’-ized, it wasn’t returning anything. Now it works like a charm! Thank again!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Earliest Actions Plugin Hook Within The Loop’ is closed to new replies.