WordPress.org

Ready to get started?Download WordPress

Forums

Earliest Actions Plugin Hook Within The Loop (9 posts)

  1. thejoker101
    Member
    Posted 9 years ago #

    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: http://codex.wordpress.org/User:Skippy/Plugin_Hooks. The only ones that have worked so far for me are wp_meta and wp_footer.

    Thanks again.

  2. skippy
    Member
    Posted 9 years ago #

    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;
    }

  3. thejoker101
    Member
    Posted 9 years ago #

    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.

  4. skippy
    Member
    Posted 9 years ago #

    use plugins_loaded.

  5. thejoker101
    Member
    Posted 9 years ago #

    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;
    get_currentuserinfo();
    // 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');

  6. thejoker101
    Member
    Posted 9 years ago #

    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.

    Thoughts...?

  7. skippy
    Member
    Posted 9 years ago #

    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?

  8. thejoker101
    Member
    Posted 9 years ago #

    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.

  9. thejoker101
    Member
    Posted 9 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic