WordPress-MU Archives - Justin Silver https://www.justinsilver.com/tag/wordpress-mu/ Technology, Travel, and Pictures Fri, 03 Feb 2017 07:27:07 +0000 en-US hourly 1 https://wordpress.org/?v=6.0.1 https://www.justinsilver.com/wp-content/uploads/2013/06/cropped-apple-touch-icon-160x160.png WordPress-MU Archives - Justin Silver https://www.justinsilver.com/tag/wordpress-mu/ 32 32 Check For WordPress Plugin Updates https://www.justinsilver.com/technology/wordpress/check-wordpress-plugin-updates/?utm_source=rss&utm_medium=rss&utm_campaign=check-wordpress-plugin-updates https://www.justinsilver.com/technology/wordpress/check-wordpress-plugin-updates/#respond Tue, 15 Apr 2014 21:45:51 +0000 http://justin.ag/?p=3458 If you write and contribute plugins to the WordPress Repository then you might be interested in having your plugin be aware of updates you have made available – an admin notice or something similar....

The post Check For WordPress Plugin Updates appeared first on Justin Silver.

]]>
AmpedSense.OptimizeAdSpot('AP'); AmpedSense.OptimizeAdSpot('IL'); AmpedSense.OptimizeAdSpot('IR');

If you write and contribute plugins to the WordPress Repository then you might be interested in having your plugin be aware of updates you have made available – an admin notice or something similar. Maybe design a clever versioning scheme so that you can inform the user of critical fixes in a more dramatic way than regular patches.

Check For WordPress Plugin Updates

// Your plugin file, perhaps __FILE__?
$plugin_file = 'plugin-name/plugin-file.php';

// Check for Plugin updates, if you want to have the latest (not necessarily recommended)
wp_update_plugins();

// Results of the update check
$update_plugins = get_site_transient( 'update_plugins' );
if ( isset( $update_plugins->response[ $plugin_file ] ) ) {
    // Your plugin needs an update, do something about it?
}

Plugins With Updates

In the result of update_plugins the $update_plugins->response is an array() that will be something like the following, if there is anything else you want to check.

'response' => 
  array (
    'plugin-dir/plugin-file.php' => 
    stdClass::__set_state(array(
       'id' => '12345',
       'slug' => 'plugin-slug',
       'plugin' => 'plugin-name/plugin-file.php',
       'new_version' => '1.2.3',
       'url' => 'https://wordpress.org/plugins/plugin-name/',
       'package' => 'https://downloads.wordpress.org/plugin/plugin-name.1.2.3.zip',
    )),
),

Compare Installed and Available WordPress Plugin Versions

function my_admin_notice() {
    global $my_admin_notice;
    ?>
    <div class="updated">
        <p><?php _e( 'An update is available for your plugin!', 'my-text-domain' ); ?>
        <?php echo $my_admin_notice; ?></p>
    </div>
    <?php
}

function check_update_notices(){
    global $my_admin_notice;
    $plugin_file = 'plugin-name/plugin-file.php';
    $installed_ver = split( ',', $update_plugins->checked[$plugin_file] );
    $available_ver = split( ',', $update_plugins->response[$plugin_file]->new_version );
    $v1 = $installed_ver[0];
    $v2 = $available_ver[0];
    if ( $v1 != $v2 ){
        $behind = $v2-$v1;
        $my_admin_notice = $behind!=1? "Oh no, you're {$behind} major version(s) behind!" : "";
        add_action( 'admin_notices', 'my_admin_notice' );
        return;
    }
}

The post Check For WordPress Plugin Updates appeared first on Justin Silver.

]]>
https://www.justinsilver.com/technology/wordpress/check-wordpress-plugin-updates/feed/ 0
WordPress Plugin: Custom Functions.php https://www.justinsilver.com/technology/wordpress/wordpress-plugins/wordpress-plugin-custom-functions-php/?utm_source=rss&utm_medium=rss&utm_campaign=wordpress-plugin-custom-functions-php https://www.justinsilver.com/technology/wordpress/wordpress-plugins/wordpress-plugin-custom-functions-php/#respond Wed, 07 Nov 2012 19:04:23 +0000 http://justin.ag/?p=2814 One of the limitations of adding code to your theme’s function.php file is that these modifications are lost if you change themes, and another is that it is executed after your plugin files are...

The post WordPress Plugin: Custom Functions.php appeared first on Justin Silver.

]]>
AmpedSense.OptimizeAdSpot('AP'); AmpedSense.OptimizeAdSpot('IL'); AmpedSense.OptimizeAdSpot('IR');

One of the limitations of adding code to your theme’s function.php file is that these modifications are lost if you change themes, and another is that it is executed after your plugin files are loaded. This means that if you want to modify any behavior in your other plugins that call filtered functions but aren’t hooked to WordPress Actions, you’re out of luck.

I have worked around this on all my sites by creating a custom plugin to handle all of my non-theme specific additions. The only thing this plugin does is ensure that it is loaded first when you activate it, and then pulls in the contents of a functions.php file in the parent plugins directory. The result is that you have custom code running before any of your other plugins. The functions.php file is specifically not included, but it would just but a standard PHP file like the one in your theme.

This file is always up to date based on the trunk of my SVN repository.

custom-functions.php

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.23.1</center>
</body>
</html>

The post WordPress Plugin: Custom Functions.php appeared first on Justin Silver.

]]>
https://www.justinsilver.com/technology/wordpress/wordpress-plugins/wordpress-plugin-custom-functions-php/feed/ 0
Activate WP SlimStat Dashboard Widgets in WordPress Multisite https://www.justinsilver.com/technology/wordpress/activate-wp-slimstat-dashboard-widgets-in-wordpress-multisite/?utm_source=rss&utm_medium=rss&utm_campaign=activate-wp-slimstat-dashboard-widgets-in-wordpress-multisite https://www.justinsilver.com/technology/wordpress/activate-wp-slimstat-dashboard-widgets-in-wordpress-multisite/#comments Wed, 07 Nov 2012 00:24:26 +0000 http://justin.ag/?p=2807 If you have WordPress Multisite installed and the WP SlimStat plugin activated network wide, the optional WP SlimStat Dashboard widgets won’t be available to you. The problem is that the dashboard widgets file is...

The post Activate WP SlimStat Dashboard Widgets in WordPress Multisite appeared first on Justin Silver.

]]>
AmpedSense.OptimizeAdSpot('AP'); AmpedSense.OptimizeAdSpot('IL'); AmpedSense.OptimizeAdSpot('IR');

If you have WordPress Multisite installed and the WP SlimStat plugin activated network wide, the optional WP SlimStat Dashboard widgets won’t be available to you. The problem is that the dashboard widgets file is using get_option('active_plugins') to see if the plugin is active, and if it’s active network wide it won’t be in this array – it will be in get_site_options('active_sitewide_plugins'). Rather than modifying the WP SlimStat plugin files (which will be overwritten when you upgrade), I recommend adding the following filter to a custom functions plugin to add the value to the array before it is read by the dashboard widgets. Using functions.php in your theme is out because get_option() in the WP SlimStat plugin is called before it loads. You could also use a Must Use Plugin placed in /wp-content/mu-plugins.

There is some checking to make sure this only happens on the admin index page, but that’s just to be safe – it really shouldn’t have any adverse affects on your site except a few edge cases.

function activate_slimstat_dashboard_multisite($plugins){
	// only run on multisite admin index screens.
	// $pagenow isn't available, so we have to preg_match the SCRIPT_NAME (should work for subdirectories)
	if (is_multisite() && preg_match("~/wp-admin/index\.php$~", $_SERVER['SCRIPT_NAME'])){
		$slimstat_plugin = 'wp-slimstat/wp-slimstat.php';
		// check if plugin is active network wide and if so add to site plugins
		if (array_key_exists($slimstat_plugin, get_site_option('active_sitewide_plugins', array())))
			$plugins[] = $slimstat_plugin;
	}
	return $plugins;
}
add_filter( 'option_active_plugins', 'activate_slimstat_dashboard_multisite' );

The post Activate WP SlimStat Dashboard Widgets in WordPress Multisite appeared first on Justin Silver.

]]>
https://www.justinsilver.com/technology/wordpress/activate-wp-slimstat-dashboard-widgets-in-wordpress-multisite/feed/ 1
WordPress Plugin: wp-server-migration https://www.justinsilver.com/technology/wordpress/wordpress-plugin-wp-server-migration/?utm_source=rss&utm_medium=rss&utm_campaign=wordpress-plugin-wp-server-migration https://www.justinsilver.com/technology/wordpress/wordpress-plugin-wp-server-migration/#comments Sun, 04 Nov 2012 04:50:12 +0000 http://justin.ag/?p=2757 This plugin is designed to make managing migration from dev->staging->production easier for WordPress and WordPress Multisite. Allows for WP_HOME and WP_SITEURL to be set in WordPress Multisite, and for URLs in content and meta...

The post WordPress Plugin: wp-server-migration appeared first on Justin Silver.

]]>
AmpedSense.OptimizeAdSpot('AP'); AmpedSense.OptimizeAdSpot('IL'); AmpedSense.OptimizeAdSpot('IR');

This plugin is designed to make managing migration from dev->staging->production easier for WordPress and WordPress Multisite. Allows for WP_HOME and WP_SITEURL to be set in WordPress Multisite, and for URLs in content and meta data to be rewritten automatically when the environment changes.

This file is always up to date based on this gist.

wp-server-migration.php

<?php
/*
Plugin Name: Wordpress Hostname Migration
Plugin URI: http://doublesharp.com/
Description: Functionality to ease in migration of Wordpress code and databases between environments.
Author: Justin Silver
Version: 1.0
Author URI: http://doublesharp.com
License: GPL2
*/

if ( ! class_exists( 'WordpressMigration' ) ):
class WordpressMigration {

	// token used to replace site URLs, overridden with SITE_URL_TOKEN constant
	private static $site_url_token = '%%site_url_token%%';

	private static $instance;

	private function __construct() { }

	public static function init() {
		if ( ! self::$instance ) {
			self::$instance = new WordpressMigration();

			// Multisite only filters
			if ( defined( 'WP_DEVELOPMENT ') && WP_DEVELOPMENT && is_multisite() ):
				 self::$instance->set_multisite_hooks();
			endif;

			// Content create url token
			self::$instance->set_content_update_hooks();
			// Content replace url token
			self::$instance->set_content_fetch_hooks();

			// MetaData create url token
			self::$instance->set_meta_update_hooks();
			// MetaData replace url token
			self::$instance->set_meta_fetch_hooks();
		}
	}

	/**
	 * Hooks that are only needed on multisite
	 */
	function set_multisite_hooks() {
		// _config_wp_siteurl() and _config_wp_home() are used by default but disabled for multisite
		add_filter( 'option_siteurl',		array( &$this, '_ms_config_wp_siteurl' ),		10, 1 );
		add_filter( 'option_home',			array( &$this, '_ms_config_wp_home' ), 			10, 1 );
		add_filter( 'content_url',			array( &$this, '_ms_content_url' ), 			10, 2 );
		add_filter( 'plugins_url',			array( &$this, '_ms_plugins_url' ), 			10, 3 );
	}

	/**
	 * Set the filters that replace the token with the site URL in the content and comments
	 */
	function set_content_fetch_hooks() {
		// Post Content
		add_filter( 'the_content',			array( &$this, '_content_url_replace_token' ), 	11, 1 );
		add_filter( 'the_excerpt',			array( &$this, '_content_url_replace_token' ), 	11, 1 );
		add_filter( 'content_edit_pre',   	array( &$this, '_content_url_replace_token' ), 	11, 1 );
		add_filter( 'the_editor_content', 	array( &$this, '_content_url_replace_token' ), 	11, 1 );

		// Post Comments
		add_filter( 'comment_excerpt ',		array( &$this, '_content_url_replace_token' ), 	11, 1 );
		add_filter( 'comment_text',			array( &$this, '_content_url_replace_token' ), 	11, 1 );
	}

	/**
	 * Set the filters that replace the site URL with the token in the content
	 */
	function set_content_update_hooks() {
		// Post Content
		add_filter( 'content_save_pre',	 	array( &$this, '_content_url_create_token' ), 	11, 1 );

		// Post Comments
		add_filter( 'comment_save_pre',	 	array( &$this, '_content_url_create_token' ), 	11, 1 );

	}

	/**
	 * Set the filters that replace the token with the site URL in post, user and comment meta values
	 */
	function set_meta_fetch_hooks() {
		add_filter( 'get_post_metadata',	array( &$this, '_get_post_metadata' ), 			11, 4 );
		add_filter( 'get_user_metadata',	array( &$this, '_get_user_metadata' ), 			11, 4 );
		add_filter( 'get_comment_metadata', array( &$this, '_get_comment_metadata' ), 		11, 4 );
	}

	/**
	 * Unset the filters that replace the token with the site URL in post, user and comment meta values
	 */
	function unset_meta_fetch_hooks() {
		remove_filter( 'get_post_metadata',	array( &$this, '_get_post_metadata' ), 			11, 4 );
		remove_filter( 'get_user_metadata',	array( &$this, '_get_user_metadata' ), 			11, 4 );
		remove_filter( 'get_comment_metadata', array( &$this, '_get_comment_metadata' ), 	11, 4 );
	}

	/**
	 * Set the actions that replace the site URL with the token in post, user and comment meta values
	 */
	function set_meta_update_hooks() {
		add_action( 'updated_post_meta',	array( &$this, '_updated_post_meta' ), 			11, 4 );
		add_action( 'updated_comment_meta', array( &$this, '_updated_comment_meta' ), 		11, 4 );
		add_action( 'updated_user_meta',	array( &$this, '_updated_user_meta' ), 			11, 4 );
		add_action( 'added_post_meta',		array( &$this, '_updated_post_meta' ), 			11, 4 );
		add_action( 'added_comment_meta',	array( &$this, '_updated_comment_meta' ), 		11, 4 );
		add_action( 'added_user_meta',		array( &$this, '_updated_user_meta' ), 			11, 4 );
	}

	/**
	 * Unset the actions that replace the site URL with the token in post, user and comment meta values.
	 * Primarily used to prevent loops when replacing the url and updating the value.
	 */
	function unset_meta_update_hooks() {
		remove_action( 'updated_post_meta',	array( &$this, '_updated_post_meta' ), 			11, 4 );
		remove_action( 'updated_comment_meta', array( &$this, '_updated_comment_meta' ), 	11, 4 );
		remove_action( 'updated_user_meta',	array( &$this, '_updated_user_meta' ), 			11, 4 );
		remove_action( 'added_post_meta',	array( &$this, '_updated_post_meta' ), 			11, 4 );
		remove_action( 'added_comment_meta', array( &$this, '_updated_comment_meta' ), 		11, 4 );
		remove_action( 'added_user_meta',	array( &$this, '_updated_user_meta' ), 			11, 4 );
	}

	/**
	 * Replace this site's WP_SITEURL URL based on constant values
	 *
	 * @param string  $url - The original URL
	 * @return string - The replaced URL if overridden in wp-config.php
	 */
	function _ms_config_wp_siteurl( $url = '' ) {
		if ( is_multisite() ):
			global $blog_id, $current_site;
			$cur_blog_id = defined( 'BLOG_ID_CURRENT_SITE' )? BLOG_ID_CURRENT_SITE : 1;
			$key = ( $blog_id != $cur_blog_id )? "{$blog_id}_" : '';
			$constant = "WP_{$key}SITEURL";
			if ( defined( $constant ) )
				return untrailingslashit( constant( $constant ) );
		endif;
		return $url;
	}

	/**
	 * Replace this site's WP_HOME URL based on constant values
	 *
	 * @param string $url 		- The original URL
	 * @return string 			- The replaced URL if overridden in wp-config.php
	 */
	function _ms_config_wp_home( $url = '' ) {
		if ( is_multisite() ):
			global $blog_id;
			$cur_blog_id = defined( 'BLOG_ID_CURRENT_SITE' )? BLOG_ID_CURRENT_SITE : 1;
			$key = ( $blog_id != $cur_blog_id )? "{$blog_id}_" : '';
			$constant = "WP_{$key}HOME";
			if ( defined( $constant ) )
				return untrailingslashit( constant( $constant ) );
		endif;
		return $url;
	}

	function _ms_content_url($url, $path){
		if ( is_multisite() ):
			// just swap out the host
			$url_array = explode( "/", $url );
			$replaced = explode( "/", $this->_ms_config_wp_siteurl() );
			$url_array[2] = $replaced[2];

			$url = implode( "/", $url_array );
		endif;
		return $url;
	}

	function _ms_plugins_url($url, $path, $plugin){
		if ( is_multisite() ):
			// just swap out the host
			$url_array = explode( "/", $url );
			$replaced = explode( "/", $this->_ms_config_wp_siteurl() );
			$url_array[2] = $replaced[2];

			$url = implode( "/", $url_array );
		endif;
		return $url;
	}

	/**
	 * Get the URL Token for this site
	 *
	 * @return string 			- The URL token for this site
	 */
	function _ms_get_site_url_token() {
		if ( defined( 'SITE_URL_TOKEN' ) ) return SITE_URL_TOKEN;
		return self::$site_url_token;
	}

	/**
	 * Find instances of the current domain and replace them with a token
	 *
	 * @param string $content 	- The orignal content
	 * @return string 			- The updated content with the token replacement in place of the site URL
	 */
	function _content_url_create_token( $content ) {
		$domain = get_bloginfo( 'url' );
		$token = $this->_ms_get_site_url_token();

		// Find instances of the current domain and replace them with the token
		$content = str_replace( $domain, $token, $content );

		return $content;
	}

	/**
	 * Find instances of the token and replace them with the current domain
	 *
	 * @param string $content 	- The orignal content
	 * @return string 			- The updated content with the site URL in place of the token
	 */
	function _content_url_replace_token( $content ) {
		$domain = get_bloginfo( 'url' );
		$token = $this->_ms_get_site_url_token();

		// Find instances of the token and replace them with the current domain
		$content = str_replace( $token, $domain, $content );
		// case insensitive using preg_replace, may be useful?
		//$content = preg_replace('/'.str_replace("/", "\/", $domain).'/i', $token, $content);

		return $content;
	}

	/**
	 * Hook to get_post_metadata
	 *
	 * @param null $metadata 	- always null
	 * @param int $object_id 	- database id of this post object
	 * @param string $meta_key  - The meta key used for lookup on this post id
	 * @param bool $single		- If the value is an array, return the first element, otherwise just the value
	 * @return mixed 			- The updated value with the site URL in place of the token
	 */
	public function _get_post_metadata( $metadata, $object_id, $meta_key, $single ) {
		return $this->_get_metadata( 'post', $metadata, $object_id, $meta_key, $single );
	}

	/**
	 * Hook to get_user_metadata
	 *
	 * @param null $metadata 	- always null
	 * @param int $object_id 	- database id of this user object
	 * @param string  $meta_key - The meta key used for lookup on this user id
	 * @param bool $single		- If the value is an array, return the first element, otherwise just the value
	 * @return mixed 			- The updated value with the site URL in place of the token
	 */
	public function _get_user_metadata( $metadata, $object_id, $meta_key, $single ) {
		return $this->_get_metadata( 'user', $metadata, $object_id, $meta_key, $single );
	}

	/**
	 * Hook to get_comment_metadata
	 *
	 * @param null $metadata 	- always null
	 * @param int $object_id 	- database id of this comment object
	 * @param string $meta_key 	- The meta key used for lookup on this comment id
	 * @param bool $single		- If the value is an array, return the first element, otherwise just the value
	 * @return mixed 			- The updated value with the site URL in place of the token
	 */
	public function _get_comment_metadata( $metadata, $object_id, $meta_key, $single ) {
		return $this->_get_metadata( 'comment', $metadata, $object_id, $meta_key, $single );
	}

	/**
	 * This function is called by the hooked functions and will return the data with the token replaced by the site url
	 *
	 * @param string $meta_type - The object type of his meta key/value
	 * @param null $metadata 	- always null
	 * @param int $object_id 	- database id of this object
	 * @param string $meta_key 	- The meta key used for lookup on this id
	 * @param bool $single		- If the value is an array, return the first element, otherwise just the value
	 * @return mixed 			- The updated value with the site URL in place of the token
	 */
	private function _get_metadata( $meta_type, $metadata, $object_id, $meta_key, $single ) {
		// Unset the hooks to get the default data
		$this->unset_meta_fetch_hooks();
		// Fetch the default data
		$data = get_metadata( $meta_type, $object_id, $meta_key, $single );
		// Reset the hooks to intercept
		$this->set_meta_fetch_hooks();

		return $data;

		// Create a small cache key that is based on the data value (cleared in update code as needed)
		$key = $meta_type.$object_id.$meta_key.$single;

		// Check to see if we have already processed this value
		if ( false === ( $cached = wp_cache_get( $key, 'wordpress_migration' )  ) ) {
			$cached = $data;
			$domain = get_bloginfo( 'url' );
			$token = $this->_ms_get_site_url_token();

			$hastoken = false;
			// Check for the token and replace it with the url
			if ( is_array( $cached ) ) {
				foreach ( $cached as $value ) {
					// Only process strings for now
					if ( is_string( $value ) && false !== strpos( $value, $token ) ) {
						  // Value contains token
						$value = str_replace( $token, $domain, $value );
						$hastoken = true;
					}
				}
			} else {
				if ( false !== strpos( $cached, $token ) ) {
					// Value contains token
					$cached = str_replace( $token, $domain, $cached );
					$hastoken = true;
				}
			}

			// If we didn't replace the token just show that it has been processed.
			if ( ! $hastoken ) $cached = true;

			// Set the cache so we don't have to loop/replace
			wp_cache_set( $key, $cached, 'wordpress_migration', 600 ); // 5 mins
			
		} 

		// It has been processed, but did not have the token, return $data
		if ( $cached !== true ) {
			$data = $cached;
		}

		return $data;
	}

	/**
	 * Hook to updated_post_meta
	 *
	 * @param int $meta_id		- Database id of the meta key/value
	 * @param int $object_id	- Database id of this post object
	 * @param string $meta_key	- The meta key used for update on this post id
	 * @param mixed $_meta_value - The current meta value
	 */
	public function _updated_post_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
		$this->_updated_meta( 'post', $meta_id, $object_id, $meta_key, $_meta_value );
	}

	/**
	 * Hook to updated_comment_meta
	 *
	 * @param int $meta_id		- Database id of the meta key/value
	 * @param int $object_id 	- Database id of this comment object
	 * @param string $meta_key	- The meta key used for update on this comment id
	 * @param mixed $_meta_value - The current meta value
	 */
	public function _updated_comment_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
		$this->_updated_meta( 'comment', $meta_id, $object_id, $meta_key, $_meta_value );
	}

	/**
	 * Hook to updated_user_meta
	 *
	 * @param int $meta_id 		- Database id of the meta key/value
	 * @param int $object_id 	- Database id of this user object
	 * @param string $meta_key 	- The meta key used for update on this user id
	 * @param mixed $_meta_value - The current meta value
	 */
	public function _updated_user_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
		$this->_updated_meta( 'user', $meta_id, $object_id, $meta_key, $_meta_value );
	}

	/**
	 * Called by hooked functions with the meta type. Checks the values for the site url, replaces
	 * with the token, and updates the meta_value if necessary.
	 *
	 * @param string $meta_type - The object type of his meta key/value
	 * @param int $meta_id 		- Database id of the meta key/value
	 * @param int $object_id 	- Database id of this object
	 * @param string $meta_key 	- The meta key used for update on this object id
	 * @param mixed $_meta_value - The current meta value
	 */
	private function _updated_meta( $meta_type, $meta_id, $object_id, $meta_key, $_meta_value ) {
		$domain = get_bloginfo( 'url' );
		$token = $this->_ms_get_site_url_token();

		// Only update the meta value if we find an instance of the url
		$update = false;
		$meta_value = $_meta_value;
		if ( is_array( $meta_value ) ) {
			// Loop through the array and update the values
			foreach ( $meta_value as &$value ) {
				// only check if this is a string, don't support arrays of arrays
				if ( is_string( $value ) && false!==strpos( $value, $domain ) ) {
					$value = str_replace( $domain, $token, $value );
					$update = true;
				}
			}
		} else {
			// Check meta value for url
			if ( false!==strpos( $meta_value, $domain ) ) {
				$meta_value = str_replace( $domain, $token, $meta_value );
				$update = true;
			}
		}

		// The meta value contained a url which was replaced with a token
		if ( $update ) {
			// Unset the update hooks to prevent a loop
			$this->unset_meta_update_hooks();
			// Update the meta value
			update_metadata( $meta_type, $object_id, $meta_key, $meta_value, $_meta_value );
			// Reset the hooks
			$this->set_meta_update_hooks();
		}

		// Since the value was updated, clear the cache
		$key = $meta_type.$object_id.$meta_key;
		wp_cache_delete( $key.true, 'wordpress_migration' ); // single = true
		wp_cache_delete( $key.false, 'wordpress_migration' ); // single = false
	}
}

// init the class/filters
WordpressMigration::init();

endif; // class exists

The post WordPress Plugin: wp-server-migration appeared first on Justin Silver.

]]>
https://www.justinsilver.com/technology/wordpress/wordpress-plugin-wp-server-migration/feed/ 3
WP_HOME and WP_SITEURL for WordPress Multisite Development & Migration https://www.justinsilver.com/technology/wordpress/wp_home-and-wp_siteurl-for-wordpress-multisite-development-migration/?utm_source=rss&utm_medium=rss&utm_campaign=wp_home-and-wp_siteurl-for-wordpress-multisite-development-migration Fri, 19 Oct 2012 15:10:35 +0000 http://justin.ag/?p=2677 I needed to be able to copy the wp_options table(s) to my development environment as they contain configurations that are needed. To overcome the issue of not being able to set the WP_SITEURL and...

The post WP_HOME and WP_SITEURL for WordPress Multisite Development & Migration appeared first on Justin Silver.

]]>
AmpedSense.OptimizeAdSpot('AP'); AmpedSense.OptimizeAdSpot('IL'); AmpedSense.OptimizeAdSpot('IR');

I needed to be able to copy the wp_options table(s) to my development environment as they contain configurations that are needed. To overcome the issue of not being able to set the WP_SITEURL and WP_HOME values in WordPress MultiSite, I wrote a custom filter to replace the _config_wp_siteurl() and _config_wp_home() functions that are available for non-multisite installs that is included in a plugin that is available network-wide and is configured in wp-config.php. I am then able to copy all of the database tables except wp_site and wp_blogs to a local database.

I highly recommend the URL Token Replacement Techniques for WordPress 3.0 article by Chris Murphy to help handle URLs in your content.

This example assumes a subdomain multisite install, with a domain of example.com and two subdomains, www.example.com and second.example.com. The local development URLs will be www.example.local and second.example.local respectively.

WordPress Multisite Updates

Database Changes:

Update the domain value in wp_site:

UPDATE wp_site
SET domain = 'example.local'
WHERE domain = 'example.com';

Update the domain value(s) in wp_blogs:

UPDATE wp_blogs
SET domain = 'www.example.local'
WHERE domain = 'www.example.com';
UPDATE wp_blogs
SET domain = 'second.example.local'
WHERE domain = 'second.example.com';

Plugin Code:

The following plugin should be installed network-wide.

<?php
/*
Plugin Name: MultiSite WP_HOME and WP_SITEURL
Plugin URI: http://doublesharp.com/
Description: Allows wp_options values to be overwritten in wp-config.php for MultiSite
Author: Justin Silver
Version: 1.0
Author URI: http://doublesharp.com
License: GPL2
*/

/**
 * Replace this site's WP_SITEURL URL based on constant values
 *
 * @param String  $url - The original URL
 * @return String - The replaced URL if overridden in wp-config.php
 */
function _ms_config_wp_siteurl( $url = '' ) {
    if (is_multisite()):
        global $blog_id, $current_site;
        $cur_blog_id = defined( 'BLOG_ID_CURRENT_SITE' )?
            BLOG_ID_CURRENT_SITE :
            1;
        $key = ( $blog_id!=$cur_blog_id )? $blog_id.'_' : '';
        $constant = 'WP_'.$key.'SITEURL';
        if ( defined( $constant ) )
            return untrailingslashit( constant($constant) );
    endif;
    return $url;
}
add_filter( 'option_siteurl', '_ms_config_wp_siteurl' );

/**
 * Replace this site's WP_HOME URL based on constant values
 *
 * @param String  $url - The original URL
 * @return String - The replaced URL if overridden in wp-config.php
 */
function _ms_config_wp_home( $url = '' ) {
    if (is_multisite()):
        global $blog_id;
        $cur_blog_id = defined( 'BLOG_ID_CURRENT_SITE' )?
            BLOG_ID_CURRENT_SITE :
            1;
        $key = ( $blog_id!=$cur_blog_id )? $blog_id.'_' : '';
        $constant = 'WP_'.$key.'HOME';
        if ( defined( $constant ) )
            return untrailingslashit( constant($constant) );
    endif;
    return $url;
}
add_filter( 'option_home',    '_ms_config_wp_home'    );
?>

Configure wp-config.php:

Add new constants to wp-config.php. The primary site should use the standard WP_HOME and WP_SITEURL and the tertiary URLs should use WP_{$blog_id}_HOME and WP_{$blog_id}_SITEURL where {$blog_id} is the numeric blog ID value that you want to replace from the wp_blogs table.

define('WP_HOME',      'http://www.example.local');
define('WP_SITEURL',   'http://www.example.local');
define('WP_2_HOME',    'http://secondary.example.local');
define('WP_2_SITEURL', 'http://secondary.example.local');

This post is the result of a question that I originally asked on StackOverflow – Override WP_SITEURL and WP_HOME for WordPress Multisite – and ultimately answered with the help of Chris Murphy.

The post WP_HOME and WP_SITEURL for WordPress Multisite Development & Migration appeared first on Justin Silver.

]]>