Advertisement
Guest User

Wordpress Multi-network switch_to_blog

a guest
Dec 6th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.98 KB | None | 0 0
  1. function switch_to_network_blog($network_prefix, $blog_id)
  2. {
  3.     global $wpdb, $wp_object_cache, $last_network_prefix;
  4.  
  5.     //save prefix for restoring
  6.     $last_network_prefix = $wpdb->base_prefix;
  7.  
  8.     if($network_prefix == $last_network_prefix)
  9.     {
  10.         //if on the same network, use wp functionality
  11.         switch_to_blog($blog_id);
  12.     }
  13.     else
  14.     {
  15.         //Change prefix
  16.         $wpdb->base_prefix = $network_prefix;
  17.         $wpdb->prefix = $wpdb->get_blog_prefix();
  18.  
  19.         //Switch over
  20.         switch_to_blog($blog_id);
  21.        
  22.         //Clear cache
  23.         $wp_object_cache->flush();
  24.     }
  25. }
  26.  
  27. function restore_current_network_blog()
  28. {
  29.     global $wpdb, $wp_object_cache, $last_network_prefix;
  30.  
  31.     if($last_network_prefix == $wpdb->base_prefix)
  32.     {
  33.         //if on the same network, use wp functionality
  34.         restore_current_blog();
  35.     }
  36.     else
  37.     {
  38.         //Change prefix
  39.         $wpdb->base_prefix = $last_network_prefix;
  40.         $wpdb->prefix = $wpdb->get_blog_prefix();
  41.  
  42.         //Restore
  43.         restore_current_blog();
  44.  
  45.         //Clear cache
  46.         $wp_object_cache->flush();
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement