Monday , February 3 2025

Automatically text replacement via WordPress function

Someday I will describe the way of creating a multisite for WordPress without database clones and installing plugins. But in this article I am describing a solution when several sites work with the same database and with the same admin panel, and for example, for a subdomain I need to automatically change the content (in this case, the city).

Copy and paste the function into functions.php as shown below:

$host = $_SERVER['HTTP_HOST']; 
if($host == "sub.domain.com" or $host == "www.sub.domain.com") {
	function replace_content($content) {
		$content = str_replace('first phrase to replace', 'edited first phrase',$content);
     		$content = str_replace('second phrase to replace', 'edited second phrase',$content);
     		$content = str_replace('third phrase to replace', 'edited third phrase',$content);
			return $content;
 		}
add_filter('the_content','replace_content');
}

Similarly, you can change the content on the fly depending on the language versions, when instead of checking the host, you can check the language prefix in the link.

About iryna

I'm Iryna, a web developer from Ukraine with a decade of experience solving complex technical challenges in the world of freelance. Throughout my career, I've worked on everything from troubleshooting server-side issues and optimizing website performance to enhancing user interfaces. On this blog, I share detailed solutions to the technical problems I’ve encountered and methods that have worked best for me. In addition to my technical expertise, I’m also passionate about digital drawing. I hope the tutorials and insights I provide here will help both fellow developers and creatives alike in their own projects.

Check Also

How to remove 404 //fonts.gstatic.com and //fonts.googleapis.com from WordPress head section

I tried solutions with functions that should remove from head and , but a very …

Leave a Reply

Your email address will not be published. Required fields are marked *