Facebook Icon

WordPress Multisite: Functions and Methods

Multisite may be a nice choice for freelancers, businesses and organizations that manage multiple WordPress websites. Whether or not you’re a freelancer who wants to provide website hosting and maintenance to clients, or an organization trying to concentrate the management of your websites, multisite is the answer.

Managing multiple websites with a single installation of WordPress permits you to simply upgrade the core, plugins and themes for each web site in a network. You can share functionality across multiple websites with network plugins, also as standardize design elements across multiple websites using a parent theme.

Working with Multisite Functions.

Multisite-enabled WordPress installations contain extra functions and options that wordpress website developer will use to enhance the experience of a website development. If you discover yourself developing themes and plugins for WordPress Multisite, take into account the following tips to customize and improve the connectivity of the network.

Build a Network Navigation Menu.

Many networks have consistent dynamic navigation that appears on all websites, making it easy for visitors to browse the network. Using the $wpdb database class, along with the get_site_url(), home_url(), get_current_blog_id(), switch_to_blog() and restore_current_blog() functions, we can create a fully dynamic network menu, including a class (.current-site-item) to highlight the current website.

The SQL query we’ve created in this example has the potential to become very large, possibly causing performance issues. For this reason, we’ll use the Transients API, which enables us to temporarily store a cached version of the results as network website “transients” in the sitemeta table using the set_site_transient() and get_site_transient() functions.

Transients provide a simple and standardized way to store cached data in the database for a set period of time, after which the data expires and is deleted. It’s very similar to storing information with the Options API, except that it has the added value of an expiration time. Transients are also sped up by caching plugins, whereas normal options aren’t. Due to the nature of the expiration process, never assume that a transient is in the database when writing code.

The SQL query will run every two hours, and the actual data will be returned from the transient, making things much more efficient. Including two parameters, $size and $expires, allowing you to control the number of posts returned and the expiration time for the transient.

One of the most powerful elements of this example is the use of switch_to_blog() and restore_current_blog(). These two Multisite functions enable us to temporarily switch to another website (by ID), gather information or content, and then switch back to the original website.

Set Up Global Variables across a Network.

Starting any WordPress project in a solid local development environment is always important. You might find it handy to have a global variable that determines whether a website is “live” or “staging.” In Multisite, you can achieve this using a network-activated plugin that contains the following handy function, assuming that your local host contains local host in the URL:

/**
* Define network globals
*/
function ms_define_globals() {
global $blog_id;
$GLOBALS[‘staging’] = ( strstr( $_SERVER[‘SERVER_NAME’], ‘localhost’ ) ) ? true : false;
}
add_action( ‘init’, ‘ms_define_globals’, 1 );

When would you use this $staging variable? Use it to display development-related messages, notifications and information to improve my workflow.

Display the Page Request Information in a Local Environment.

Use the $staging global variable to display the number of queries and page-request speed for every page across a network in my local environment.

/**
*Displaypage request info
*
* @requires $staging
*/
function wp_page_request_info() {
global $staging;
if ( $staging ){
echo "queries in seconds."
 } 
}
add_action( ‘wp_footer’, ‘wp_page_request_info’, 1000 ); 

This is only one of the many ways that you’ll be able to use the ms_define_globals() functions. Use it to outline, realize and replace URLs within the content delivery network, to detect mobile devices and user agents, and to filter local attachment URLs.

Share this article

Leave a Comment

Your email address and phone number will not be published. Required fields are marked *

Your Comment / Feedback*

Full Name*

Are you human?2 + 5?*

Copyright 2017. junespringmultimedia.com All Rights Reserved.
We only use the most reputable and trusted brands for making and delivering your products.
Copyright 2017. junespringmultimedia.com All Rights Reserved.
Inverted V iconOrange inverted V icon