Forums

global scope not working in 2.6 - how do I... (6 posts)

  1. ulfben
    Member
    Posted 3 years ago #

    I'm updating a plugin from WP 2.3 to 2.6. Trying to get the current $post_id when running a function as the "upload_dir"-filter. This is what I've tried so far:

    global $post_id;
    global $post; $post_id = $post->ID;
    global $wp_query; $post_id = $wp_query->post->ID;

    None of these produce the post id. Is the global scope somehow disabled in WP 2.6? How can I get the $post_id?

  2. Otto
    Tech Ninja
    Posted 3 years ago #

    The $post does not get set until the Loop has begun. You can't get a post ID when there isn't one, and the upload_dir is not used when there is one.

  3. ulfben
    Member
    Posted 3 years ago #

    Thanks Otto42. Just to be clear: the plugin has worked well up until to 2.5. I've always had a global $post_id; to play with.

    File uploads are handled on the Write Post screen and they are attached to the active post; obviously there must be a post_id somewhere...

  4. Otto
    Tech Ninja
    Posted 3 years ago #

    Well... A post doesn't have an ID until you have saved it. So I don't see how a post_id could be set anywhere.

    I'll look at it, see what I can figure out.

  5. ulfben
    Member
    Posted 3 years ago #

    Any luck?

  6. ulfben
    Member
    Posted 3 years ago #

    Finally realized today that I could just check the $_REQUEST variable and see what was being passed around. Sure enough - the current post ID was there.
    $post_id = $_REQUEST['post_id'];

Topic Closed

This topic has been closed to new replies.

About this Topic