WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Calling wp_get_attachment_url() after get_blog_details() makes a plobelm. (10 posts)

  1. wolfkang
    Member
    Posted 8 months ago #

    Hi.

    When I call the function get_blog_details() before the wp_get_attachment_url() function to get the url of the featured image of a post, the url contains 'sites/$blog_id' string.

    $blog = get_blog_details($blog_id);
    $url= wp_get_attachment_url( get_post_thumbnail_id($post_id) );
    echo $url;

    The url contains 'sites/$blog_id' and makes 404 not found.

    Please check it out.
    Thanks.

  2. The url contains 'sites/$blog_id' and makes 404 not found.

    Well the url should be example.com/wp-content/uploads/sites/siteid/2013/... and so on, so are you saying that it's actually echoing $site_id or that the images are not in that location?

    Mine are domain.com/wp-content/uploads/sites/2/2013/10/image.jpg

  3. wolfkang
    Member
    Posted 8 months ago #

    I uploaded a file in the main site and Its path is /wp-content/2013/11/image.jpg.

    $url= wp_get_attachment_url( get_post_thumbnail_id($post_id) );
    $blog = get_blog_details($blog_id);
    echo $url;

    The path of $url is /wp-content/2013/11/image.jpg. However,

    $blog = get_blog_details($blog_id);
    $url= wp_get_attachment_url( get_post_thumbnail_id($post_id) );
    echo $url;

    The path is changed as /wp-content/sites/$blog_id/2013/10/image.jpg and makes 404 not found.

    I think there's something wrong in the restore_current_blog() function that the get_blog_detail() invokes.
    It doesn't restore all the settings of the current blog.

  4. mistari
    Member
    Posted 8 months ago #

    restore_current_blog()
    dose NOT restore current blog. just previous blog.

  5. Just to confirm, you're getting a LITERAL $blog_id in there?

    How are you defining $blog_id? What happens if you echo that?

  6. wolfkang
    Member
    Posted 8 months ago #

    $blog_id is just the blog id like 42.

    $blog = get_blog_details($blog_id);
    $url= wp_get_attachment_url( get_post_thumbnail_id($post_id) );
    echo $url;

    prints /wp-content/sites/42/2013/10/image.jpg.

  7. Okay... So that's what's SUPPOSED to happen.

    Site #1: /wp-content/2013/11/image.jpg

    Site #11: /wp-content/sites/11/2013/11/image.jpg

    Site #42: /wp-content/sites/42/2013/11/image.jpg

    That's how they're supposed to look. Why do you think this is wrong? What were you expecting the site ID of the SUBsite to be and what should the URL look like?

  8. wolfkang
    Member
    Posted 7 months ago #

    Hi.
    get_blog_details() and wp_get_attachment_ur() are independent functions and should be independent.
    But get_blog_details() affects another function.

    $post_id = 10;
    $url= wp_get_attachment_url(get_post_thumbnail_id($post_id));
    echo $url;

    prints wp-content/2013/10/image.jpg because the current site id is 1 and $post_id 10 belongs to the site 1.
    wp-content/2013/10/image.jpg is correct.

    $url= wp_get_attachment_url( get_post_thumbnail_id($post_id) );
    $blog_id = 42
    $blog = get_blog_details($blog_id);
    echo $url;

    prints wp-content/2013/10/image.jpg correctly, too.

    But

    $blog = get_blog_details($blog_id);
    $url= wp_get_attachment_url( get_post_thumbnail_id($post_id) );
    echo $url;

    prints /wp-content/sites/42/2013/10/image.jpg.

    get_blog_details() changed the current site id to $blog_id 42 and makes the wrong attachment url.

    get_blog_details() affects wp_get_attachment_url() indeed. Do you understand?

  9. Well. Yes. It's supposed to.

    Do you actually understand you told WP "Okay, now I'm blog 42, so everything I call is now relative to THAT blog ID"?

    That's what it sounds like you're saying, so I'm confused why you're confused :)

    I mean... I may really be missing something, but if get_blog_details($blog_id) returns 42, then OF COURSE wp_get_attachment_url is going to show that attachment link. It's supposed to.

    The correct attachment URL for any post in Blog 42 is /wp-content/sites/42/....

    Why would it not?

    get_blog_details() and wp_get_attachment_ur() are independent functions and should be independent.

    They are. But you'll notice you can't define blogID in the latter, right? So how do you think it gets that ID?

    1) Where/HOW are you defining $blog_id?

    2) Are you remembering to reset any switch_to_blog() calls when done?

  10. wolfkang
    Member
    Posted 7 months ago #

    I found what the problem is.
    $blog_id is the global variable of WP.
    It has the current blog id.
    So, when I set $blog_id as something, I would change the current blog id without knowing it.
    There are a lot of statements $GLOBALS['blog_id'] in the wp-includes/ms-blogs.php file.
    It's not safe to use $blog_id as a local variable.
    I'll change all the variable $blog_id to $blogid or something.

    I dont't think it's a good idea for the WP developers to take the blog_id as a global variable name, because it's the name anyone can take.

    Anyway I'll close this topic.

    I appreciate your concern, Ipstenu.

Reply

You must log in to post.

About this Topic

Tags

No tags yet.