Support » Networking WordPress » Network Wide Menu

  • Resolved edtiley


    About two years ago someone asked about creating a menu that would appear on all sites on a MultiSite network. The answer was:

    just before the nav bar code put
    <?php switch_to_blog(1); ?>
    right after the navbar code put
    <?php restore_current_blog(); ?>
    then it will always show the pages from Blog #1.

    And that works beautifully!

    I’ve heard that the switch_to_blog() is expensive in terms of resources and site speed, and seen multiple posts about avoiding using it if at all possible.

    My question is simply, how expensive is it?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator Ipstenu (Mika Epstein)


    🏳️‍🌈 Halfelf Rogue & Plugin Review Team Rep

    Far less expensive than it was two years ago 🙂 It’s safer to use right now, but it’s still something I wouldn’t use, say, seven times on one page.

    but it’s still something I wouldn’t use, say, seven times on one page.

    Oh, no. I’m crazy not stupid!!

    I’ve written a plugin that has some similarities to “sitewide tags” but in mine a table gets created in the main blog (id=1) and every post gets a record that stores the blog id, post id, author nicename, guid, a list of the categories, a list of the tags, how many times the post has been viewed, reader rating info, etc.

    When a post is created or updated:

    add_action('save_post',array($this,'new_post_added') );
    	function new_post_added($postinfo) {
    	global $wpdb , $post , $blog_id;
    	$postid = $postinfo;
    	$user_blog = $blog_id ;
    	$sql =  "SELECT * FROM " . $wpdb->get_blog_prefix( $blog_id ) . "posts WHERE id=" . $postid ;
     	$newpost = $wpdb->get_row($sql);
     	if ( $newpost->post_type == 'post' && $newpost->post_status == 'publish' ) {
    		$title = $newpost->post_title ;
    		$author = get_the_author_meta( 'display_name', $newpost->post_author );
    		$pageurl = get_permalink() ;
    		$catnams ='';
    			foreach (get_the_category() as $cat)
      	     	$catnams .= ' | ' . $cat->cat_name ;
    		$catnams = ltrim(substr($catnams,2));
    		$tagnams =  strip_tags(get_the_tag_list('',' | ',''));
    //		$tagnams = ltrim(substr($tagnams,2));
    		$table_name = $wpdb->prefix . "ipubratings";
    		$sql =  "SELECT * FROM " . $table_name . " WHERE post_id =" . $postid . " AND blog_id = " . $user_blog   ;
     		$starpost = $wpdb->get_row($sql);
    		if ( !empty( $starpost->rating_id ) ) {
    			 $wpdb->update( $table_name , array(
    						'post_title' => $title,
    						'author' => $author,
    						'pageurl' => $pageurl,
    						'cat_list' => $catnams,
    						'tag_list' => $tagnams),
    						array('rating_id' => $starpost->rating_id)
    		} else {
    			 $wpdb->insert( $table_name , array(
    						'blog_id' => $user_blog,
    						'rating_date' => current_time( 'mysql',1),
    						'post_id' => $postid,
    						'post_title' => $title,
    						'author' => $author,
    						'pageurl' => $pageurl,
    						'cat_list' => $catnams,
    						'tag_list' => $tagnams,
    						'voters' => '|')
    		} // end ! empty
    	restore_current_blog() ;
        } // end if  published post
    	} // end new post added

    I’m just worried that if the site gets really busy that the switch might be too resource hungry.

    Actually, I’m wondering if I globalize or DEFINE the wpdb->prefix for the main blog id if I can create a method in the plugin’s class that would be able to access and maintain the ipubratings table without switching blogs. Seems I tried that, but it didn’t work out too well, but then I’m not nearly as good a coder as I was twenty years ago. Sigh…

    My goal is to come to understand MultiSite half as well as you and Andrea! You two are an MU-Treasure. (not fawning) Thanks.

    Moderator Ipstenu (Mika Epstein)


    🏳️‍🌈 Halfelf Rogue & Plugin Review Team Rep

    I think that should be okay. I mean, it shouldn’t be much more expensive that sitewide tags, if that. 🙂

    OK, good to know.

    Actually, I’m wondering if I globalize or DEFINE the wpdb->prefix

    But can it be avoided altogether by specifying the table overtly?


    Moderator Ipstenu (Mika Epstein)


    🏳️‍🌈 Halfelf Rogue & Plugin Review Team Rep

    Probably, but then it begs the question “Why not use WP to query posts?” 🙂

    Direct DB calls are generally frowned on.

    Oh, I think you misunderstand my question.

    I’m not talking about going outside wpdb-> to make the calls. The question is if I have the proper wpdb->prefix for the main blog, does the routine even need to use switch_to_blog() to update that table.

    Moderator Ipstenu (Mika Epstein)


    🏳️‍🌈 Halfelf Rogue & Plugin Review Team Rep

    No, I got it, but I wonder why you’re using wpdb at all for it 🙂

    The simplest answer, I suppose is because when I read the code for sitewide tags, I thought it was perhaps a bit of an overkill for what I wanted to do.

    I do mostly understand the logic behind the code for sitewide, but I come from the olden days (WordStar’s DataStar) of CPM when databases were stored in CSV format. So I understand well how to wring every bit of value out of a single (flat file) table.

    Rather than create a blog to act as a library of sorts, my plugin uses a single table to store a fraction of the data, rather than use a large portion of the database to replicate data.

    The blog id and post id, title, a list of categories and tags, author nicename, and the GUID are all I store from the orginal post. Just enough to find and embellish a post in order to put a link to it on the main portal page if warranted. The other fields in the table register the results of user evaluations. I decided that I would prefer to have that information centrally located rather than spread it in metadata throught the database, which is what makes MultiSite hard to aggregate in the first place.

    This way I can pick off sitewide posts by several criteria, and link from the portal back to the author’s home blog with very little switch_to_blog activity on the portal’s front page. This design also makes it possible to aggregate trending keywords from a single table, and for cron jobs (not WP_cron) running every few minutes to create textual (JSON, XML ?) files that can be polled by custom front page widgets to display posts by the critera needed, thus making life easier for both MySQL and whatever page cache plugin I end up with.

    Sorry, didn’t mean to be so long winded, but is my logic off? God knows I don’t want to be reported to the House committee on Anti-WordPress Activities for not using the existing data structures.

    Moderator Ipstenu (Mika Epstein)


    🏳️‍🌈 Halfelf Rogue & Plugin Review Team Rep

    Well. No. The logic is fine. it’s just one of those things that falls under ‘reinventing the wheel.’

    I’m nominally certain than using wp_insert_post and other functions similar (since you already have to switch to blog) is going to be as fast as direct DB inserts. And personally I’m a huge proponent for using the WP functions since it means I learn more about WP, but more importantly, it means when they add in new features to WP, I can quickly take advantage of them as I need to, rather than re-read all the new code.

    Nothing WRONG with what you’re doing that I can see, just abnormal 🙂

    ROFL! I never have claimed to be normal.

    The db training and experience voices in my head (two of the four) say not to waste disk space duplicating every post.

    And hey, I learned a lot about the save_post hook, so it’s not a total loss in WP education. <grin>

    Thanks for your help.


Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Network Wide Menu’ is closed to new replies.