Update domain of WordPress site

If you need a new url to be the primary one for your WordPress site, read on.

Edit the DNS Record of New Domain

Point the new domain by editing the A record for the domain to point to the IP of the server.

Create a New Virtual Host

Copy the original virtual host to create the new one and then edit it:

cp /etc/apache2/sites-available/original.com.conf /etc/apache2/sites-available/new.com.conf
nano /etc/apache2/sites-available/new.com.conf

Just update the domain information part, not the directory it points to.

Enable the new virtual host:

a2ensite new.com.conf

Restart apache2 to see the new domain in effect:

service apache2 restart

When you visit the website now, you’ll be able to visit with both domains. The original domain will show all files and images correctly, while the new domain the site will look a little broken.

Update the Database

You need to update the database to use the new domain for the wp_siteurl, wp_home and all links to files and images. There are two ways to do this.

  1. WP Migrate plugin
    The quickest way to do this is to use the WP Migrate plugin to export the database. The plugin adds a Find and Replace already loaded with the current domain. Add the new domain in the Replace field beside it. You can remove the absolute path to the directory as that’s not changing. Export the database.
  2. Edit SQL file with EditPad Lite
    If you do not have WP Migrate installed, you can download the database and open the sql file with EditPad Lite which is good for opening large files. Do a find and replace with the new domain.

Drop all tables from the live website and then upload your new sql file from WP Migrate or your edited one from EditPad Lite into the database.

Now if you visit the new domain, the images will load correctly. You will also be able to see the original domain which we’ll need to change as only one should be primary.

Redirect to the Primary Domain

Update your htaccess file at the top to redirect all domains to the new domain:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^new\.com$ [NC]
RewriteRule ^(.*)$ https://new.com/$1 [R=301,L]

Now the domain is updated for your WordPress site!

Get WordPress posts from another WordPress website

<?php
$response = wp_remote_get( add_query_arg( array(
		'per_page' => 3
	), 'https://domain.com/wp-json/wp/v2/posts' ) );
	if( !is_wp_error( $response ) && $response['response']['code'] == 200 ) {
		$posts = json_decode( $response['body'] ); // our posts are here
		
		foreach( $posts as $post ) {
			$date = date('F j, Y', strtotime($post->date));
			echo '<div class="blog-post-date">' . $date . '</div>';
			echo '<h3 class="blog-post-title">'. $post->title->rendered . '</h3>';
			echo $post->content->rendered;
			//$output .= print_r( $post );
		}
	}

Add Google Analytics only for customers

<?php
function site_inline_scripts() {
	global $current_user;
	$property_ga4 = "CONTAINER";
	$staging = false;
	if($_SERVER['HTTP_HOST']!="domain.com"){
		$property_ga4 = "CONTAINER2";
		$staging = true;
	}
	if ( !isset( $current_user->roles[0] ) || $current_user->roles[0] == 'customer' || $staging == true) {
		<!-- Google Tag Manager -->
		<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
	new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
	j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
	'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
	})(window,document,'script','dataLayer','<?php echo $property_ga4; ?>');</script>
		<!-- End Google Tag Manager -->
	}
}
add_action('wp_footer', 'site_inline_scripts', 17);

Create Shortcode in WordPress

Create shortcode that can be used in posts or pages like this:

[newshortcode]

Add to functions.php

<?php 
function newshortcode_shortcode() { 
	if(is_admin()) return;
	$post_title = get_the_title();
	ob_start();
	?>
	<?php echo $post_title; ?>
	<div>Some text</div>
	<?php
	$output = ob_get_contents();
	ob_end_clean();
	return $output;
} 
add_shortcode('newshortcode', 'newshortcode_shortcode');
?>