Skip to content

Instantly share code, notes, and snippets.

@mrtrom
Created May 4, 2017 21:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrtrom/dc5483a2f2546e649885ac32d02102f6 to your computer and use it in GitHub Desktop.
Save mrtrom/dc5483a2f2546e649885ac32d02102f6 to your computer and use it in GitHub Desktop.
Migration a Wordpress site from HTTP to HTTPS
  1. Create a backup copy of your site and your database!

  2. I reccomend starting with a plugin that manages all of this stuff for you, but if it doesn't work keep reading! :D

  3. Add these lines to to wp-config.php file

    define('FORCE_SSL_ADMIN', true);
    
    if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') $_SERVER['HTTPS']='on';

    Make sure you add them before this line:

    require_once(ABSPATH . 'wp-settings.php');
  4. Update the wp-config.php file

    define('WP_HOME','https://www.example.com/');
    define('WP_SITEURL','https://www.example.com/');
  5. Sometimes we have plain hard-coded http protocols, to fix that we have to create wrappers for the wp_head() and wp_footer. So, just call these new functions instead. ​ I recommend using something like grep -rnw './' -e "wp_head" so you know all of the places where this function is being called.

    /**
     * Wrapper for wp_head() which manages SSL
     *
     * @uses	wp_head()
     * @param	bool	$ssl
     * @return	void
     */
    function custom_wp_head() {
      // Capture wp_head output with buffering
      ob_start();
      wp_head();
      $wp_head = ob_get_contents();
      ob_end_clean();
    
      // Replace plain protocols
      $wp_head = preg_replace( '/(["\'])http:\/\//', '\1https://', $wp_head );
    
      // Output
      echo $wp_head;
    }
    
    function custom_wp_footer() {
      // Capture wp_head output with buffering
      ob_start();
      wp_footer();
      $wp_footer = ob_get_contents();
      ob_end_clean();
    
      // Replace plain protocols
      $wp_footer = preg_replace( '/(["\'])http:\/\//', '\1https://', $wp_footer );
    
      // Output
      echo $wp_footer;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment