0

Hello All!

I got this puzzling beginner issue. It's my first Multisite install, latest versions of all, no plugins, and when I am trying to use in a page template the switch_to_blog function, I get this :

Fatal error: Call to undefined function switch_to_blog() in /SITES/Sitename/wp-content/themes/themename/page.php on line 28

with line 28 : switch_to_blog(1);

I must do something wrong, but it looks the function itself is not installed, though the function's file is there (as it should be in any normal install).

Is there a way to cleanly create the function, declare it or activate it ? I tried copy the function switch_to_blog to functions.php, and there is does not complain but is not working (i got the article from the CURRENT blog). but then error make it stop to restore_current_blog(); with the same fatal error...

I don't know where to begin... what might help you understand the issue ? THANKS in advance.

G.

5
  • It should be in wp-includes/ms-blogs.php, do you see it there?
    – Tdelang
    Commented Oct 9, 2016 at 7:09
  • Yes there it is...
    – Gesatzs
    Commented Oct 9, 2016 at 7:16
  • I am using MAMP 3 and 4, same result.
    – Gesatzs
    Commented Oct 9, 2016 at 20:12
  • Please edit your question to add in the relevant code. Are you calling this in an action or a filter? If so, which one?
    – Pat J
    Commented Oct 15, 2016 at 3:28
  • I got this error when I used switch_to_blog() inside the functions.php file of my theme, in a single site installation of WordPress 4.8.9. In a multisite installation, with the same WP version, the error did not occur! About the page.php file, I did not have any problem using that function inside it. Commented Jan 8, 2020 at 22:59

1 Answer 1

0

Some time ago, but i ran into exactly the same misbehavior.

Have your multisite activated and installed correctly.

Because WordPress is just loading the functions when a multisite is really active. Here the code from wp-settings.php file around line 137 on WP 6.1.1

if ( is_multisite() ) {
    require ABSPATH . WPINC . '/class-wp-site-query.php';
    require ABSPATH . WPINC . '/class-wp-network-query.php';
    require ABSPATH . WPINC . '/ms-blogs.php'; // <--- switch_to_blog() is in here!!
    require ABSPATH . WPINC . '/ms-settings.php';
} elseif ( ! defined( 'MULTISITE' ) ) {
    define( 'MULTISITE', false );
}

Here i found a long read tutorial which helped me a lot: https://multilingualpress.org/docs/how-to-install-wordpress-multisite/

But there should be alternatives findable anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.