Skip to content

Instantly share code, notes, and snippets.

@MartyThornley
Last active December 23, 2015 16:48
Show Gist options
  • Save MartyThornley/6664082 to your computer and use it in GitHub Desktop.
Save MartyThornley/6664082 to your computer and use it in GitHub Desktop.
Code snippets from #wcla talk "Developing for MultiSite"
/*
* Knowing where you are
*/
is_multisite()
http://codex.wordpress.org/Function_Reference/is_multisite
is_main_site()
http://codex.wordpress.org/Function_Reference/is_main_site
is_admin()
http://codex.wordpress.org/Function_Reference/is_admin
is_network_admin()
http://codex.wordpress.org/Function_Reference/is_network_admin
/*
* Knowing who you are
*/
is_super_admin()
http://codex.wordpress.org/Function_Reference/is_super_admin
is_user_logged_in()
http://codex.wordpress.org/Function_Reference/is_user_logged_in
// returns an object with infomation about the current user
$current_user = wp_get_current_user();
http://codex.wordpress.org/Function_Reference/wp_get_current_user
// test for specific capabilities for the current user
if( current_user_can( ‘edit_themes’ ) )
http://codex.wordpress.org/Function_Reference/current_user_can
/*
* What Sub-Site are we in?
*/
global $blog_id;
$id = get_current_blog_id();
http://codex.wordpress.org/Function_Reference/get_current_blog_id
$details = get_blog_details( $id );
http://codex.wordpress.org/Function_Reference/get_blog_details
/*
* URL Functions
*/
home_url( $path );
http://codex.wordpress.org/Function_Reference/home_url
site_url( $path );
http://codex.wordpress.org/Function_Reference/site_url
admin_url( $path );
http://codex.wordpress.org/Function_Reference/admin_url
network_home_url( $path );
http://codex.wordpress.org/Function_Reference/network_home_url
network_site_url( $path );
http://codex.wordpress.org/Function_Reference/network_site_url
network_admin_url( $path );
http://codex.wordpress.org/Function_Reference/network_admin_url
$url = get_home_url( $blog_id );
http://codex.wordpress.org/Function_Reference/get_admin_url
$url = get_site_url( $blog_id );
http://codex.wordpress.org/Function_Reference/get_admin_url
$url = get_admin_url( $blog_id );
http://codex.wordpress.org/Function_Reference/get_admin_url
/*
* WP Constants
*/
http://codex.wordpress.org/Determining_Plugin_and_Content_Directories
http://codex.wordpress.org/Editing_wp-config.php
WP_CONTENT_DIR
WP_CONTENT_URL
WP_PLUGIN_DIR
WP_PLUGIN_URL
WPMU_PLUGIN_DIR
WPMU_PLUGIN_URL
/*
* Site-by-Site Options
*/
// manipulate options for the blog you are currently in
// works for single site install or any single site in the network
add_option( $option , $value );
http://codex.wordpress.org/Function_Reference/add_option
update_option( $option , $value );
http://codex.wordpress.org/Function_Reference/update_option
get_option( $option );
http://codex.wordpress.org/Function_Reference/get_option
// manipulate options for a specific blog
add_blog_option( $blog_id , $option , $value );
http://codex.wordpress.org/Function_Reference/add_blog_option
update_blog_option( $blog_id , $option , $value );
http://codex.wordpress.org/Function_Reference/update_blog_option
get_blog_option( $blog_id , $option );
http://codex.wordpress.org/Function_Reference/get_blog_option
/*
* Network Options
*/
add_site_option( $option , $value );
http://codex.wordpress.org/Function_Reference/get_blog_option
update_site_option( $option , $value );
http://codex.wordpress.org/Function_Reference/get_blog_option
get_site_option( $option );
http://codex.wordpress.org/Function_Reference/get_blog_option
/*
* Adding Menus
*/
// adds a menu and settings page to the admin menu in a single site
add_action( ‘admin_menu’ , ‘add_my_menu’ );
// adds a menu to the network admin area
add_action( ‘network_admin_menu’ , ‘add_my_menu’ );
// callback funtion that actually adds the menus
function add_my_menu() {
add_menu_page( $page_title , $menu_title , $capability , $menu_slug , $function , $icon_url , $position );
add_submenu_page( $parent_slug , $page_title , $menu_title , $capability , $menu_slug , $function );
}
/*
* User Capabilities
*/
http://codex.wordpress.org/Roles_and_Capabilities
// administrator level users lose these abilities in multisite
// these are now super admin capabilities
update_core
update_plugins
update_themes
install_plugins
install_themes
delete_themes
edit_plugins
edit_themes
edit_users
create_users
delete_users
unfiltered_html
/*
* Saving Network Settings
*/
// The register_setting functions work but ONLY on single site settings.
// Network settings need to be processed and saved.
add_action( ‘admin_init’ , ‘process_and_save_my_settings’ );
function process_and_save_my_settings() {
1) Check that your on your network admin page, using $pagenow or $current_screen or other
2) Check that your form posted information
3) Set and check a nonce of some kind
4) Sanitoze the data
5) save the site_option
}
/*
* Use Site Option to hide/show things
*/
1) add a network admin menu and provide a setting for the super admin to edit
2) in your "process_and_save_settings" function, save the option
update_site_option( ‘allow_sites_to_see_stuff’ , true );
3) Where you want to show/hide something
if ( get_site_option( ‘allow_sites_to_see_stuff’ ) ) {
// do stuff
}
/*
* Save a site option and use that to update options on every site
*/
/************************
DO NOT USE THIS METHOD
*****************************/
update_site_option( ‘awesome_site_option’ , $value );
$blogs = $wpdb->get_results( SELECT blog_id FROM {$wpdb->blogs} … a bunch of other stuff ...
$value = get_site_option( ‘awesome_site_option’ );
foreach ( $blogs as $blog ) {
update_blog_option( $blog['blog_id'] , ‘option_name’ , $value );
}
// this way will loop through EVERY blog and kill your site after more than a few blogs
/************************
A Better Way
*****************************/
update_site_option( ‘awesome_site_option’ , $value );
add_action( ‘init’ , ‘update_my_site_options’ );
function update_my_site_options() {
$value = get_site_option( ‘awesome_site_option’ );
update_option( ‘awesome_option’ , $value );
}
// this is better BUT still happens EVERY page load
/************************
A Still Better Way
*****************************/
// As you save your site option, set a time as well
update_site_option( ‘awesome_site_option’ , $value );
update_site_option( ‘awesome_site_option_updated’ , time() );
// add an action to init hook so it is run when each site loads next
add_action( ‘init’ , ‘update_my_site_options’ );
function update_my_site_options() {
// check both time options
$net_time = get_site_option( ‘awesome_site_option_updated’ );
$site_time = get_option( ‘awesome_option_updated’ );
// compare both times and only do stuff if network option was updated
if ( $net_time > $site_time ) {
$value = get_site_option( ‘awesome_site_option’ );
// make sure we save the option and a time for the local option as well
update_option( ‘awesome_option’ , $value );
update_option( ‘awesome_option_updated’ , time() );
}
}
// better for doing lots of stuff. If you are doing all this to update just one option, there is still a better way.
/*
* Best way for updating just one option
*/
// use pre_option_ filter to redifine options based on the network wide setting
add_filter( ‘pre_option_posts_per_page’ , ‘network_limit_posts’ );
function network_limit_posts( $num_posts ) {
// if the network setting is there, use that
if ( $network_posts = get_site_option( ‘network_post_limit’ ) ) {
return $network_posts;
// if not, use the setting as it was
} else {
return $num_posts;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment