Synchronizing Two WordPress Sites Content

Once you have same db, you will have same users, so offcouse the password for the user ‘admin’ (please do not use ‘admin’ as username) will be the one setted in database, and of course is the same for the 2 sites, once they shared the db.

However you can create users that can login only in one site and users that can login in the other.

To do that, add a field to the users profile called ‘User Site’ that will save the value entered on the user meta ‘user_site’.

function site_profile_field( $user ) {
    if ( ! current_user_can('promote_users') ) return; // only for admins
    echo '<h3>' . __('User Site') . '</h3>';
    echo '<table class="form-table">';
    $now = get_user_meta( $user->ID, 'user_site', true ) ? : "";
    echo '<tr><th><label for="user_site">' .  __('Enter the domain') . '</label></th>';
    printf(
      '<td><input type="text" name="user_site" id="user_site" value="%s" /></td></tr>',
      esc_attr($now)
    );
    echo '</table>';
}
add_action( 'show_user_profile', 'site_profile_field' );
add_action( 'edit_user_profile', 'site_profile_field' );


function site_profile_field_save( $user_id ) {
   if ( ! current_user_can( 'edit_user', $user_id ) ) return;
   $site = filter_input(INPUT_POST, 'user_site', FILTER_SANITIZE_URL);
   if ( $site ) {
     update_user_meta( $user_id, 'user_site', $site );
   }
}
add_action( 'personal_options_update', 'site_profile_field_save' );
add_action( 'edit_user_profile_update', 'site_profile_field_fields_save' );

Then everytime a user log to site you can check that meta and compare to the WP_SITEURL constant, if no match you can block the user

add_action( 'wp_authenticate', 'check_user_site', 1, 2 );

function check_user_site( $username, $password ) {
  if ( ! defined('WP_SITEURL') ) return;
  $user = get_user_by( 'login', $username );
  if ( $user->exists ) {
     $allowed = get_user_meta($user->ID, 'user_site', true);
     if ( $allowed ) {
       $domain = parse_url(WP_SITEURL, PHP_URL_HOST);
       $allowed_domain = parse_url($allowed, PHP_URL_HOST);
       if ( ! substr_count( $domain, $allowed_domain ) ) {
         $password = NULL; // set password to null in this way login will fail
       }
     }
  }
}

If the ‘User Site’ field is left blank or WP_SITEURL is not defined the user will be able to login in both sites.

This for the first problem.

For the second (upload sync) you can set the site B to use the site A uploads folder, but doing so, every upload should be done in site A, so if users of site B want to upload a file they can’t.

This can be done using adding a setting WP_CONTENT_URL in config.php, so you’ll have (just after the definition of ABSPATH)

define('WP_HOME', 'http://siteB.com');
define('WP_SITEURL', 'http://siteB.com');
define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
define('WP_CONTENT_URL', 'http://www.siteA.com/wp-content');

So you have to upload all files in site A, and then using this config on site B, when you use a proper function to get an attachment url (wp_get_attachment_url and similar) WordPress will return the url of site A. For post that contain media url in the body, once are all uploaded on site a, the already containt the url of site A.

A more flexible solution, that allow you to upload files from both sites, can be use for uploads an external file base, e.g Amazon S3 and serve urls via Amazon Cloudfront. This plugin will make your life easy in doing this.
Other plugin like this provide functionalities to use other services for same pourpose, however googling it will be easy find a plugin that works with your favourite service.

Leave a Comment