• danhgilmore

    (@danhgilmore)


    I am currently writing a plugin for Multisite, and in it, I need to switch to the blog that is using the plugin.

    $blog_id    = $_POST['blog_id'];
    switch_to_blog($blog_id);
    //  PLUGIN DOES STUFF
    restore_current_blog();

    In the above code, I’ve verified that $blog_id is the id of the blog I need (in this case, it’s 37). The value returned from the switch_to_blog function is ALWAYS 1, and the plugin does stuff to blog id 1.

    $wpdb->set_blog_id($blog_id);
    //  PLUGIN DOES STUFF
    restore_current_blog();

    This code switches to the desired blog id.

    http://pastebin.com/GDBdiqYU

Viewing 4 replies - 1 through 4 (of 4 total)
  • Yeah, switch_to_blog() will always return 1/true:

    Return Values
    (bool)
    true is returned even when no blog exists with a Blog ID of $new_blog, or when “switching” to the current blog.

    From https://codex.wordpress.org/Function_Reference/switch_to_blog

    Thread Starter danhgilmore

    (@danhgilmore)

    Ok, I get that, but I still need to figure out why switch_to_blog() isn’t switching to that blog.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    Are you 100% sure that $_POST['blog_id']; is set right?

    You said it worked if you put in 37 manually (on Twitter).

    (And why aren’t you sanitizing and validating your post input!?!?! DAN! You know Users are daft!)

    Thread Starter danhgilmore

    (@danhgilmore)

    Yep, I’ve verified that the $_POST[‘blog_id’] is the correct number.

    I’m still in development! I’ll add the fancy schmancy stuff before it goes live! 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘switch_to_blog() not working’ is closed to new replies.