WordPress: How to Check if a String Exists in a URL with PHP

For one of the sites I was working on, I had to create a multilingual site for English and French where the English version would use the English logo and the French site would use the French logo. The plugin I used allowed me to have both English and French versions on a single WordPress installation. But, the one challenge I had was working around the base theme’s Theme Options settings where I could only specific one logo file.

I found the answer on stackoverflow and I wanted to share the solution I found and I how I implemented it to solve my problem.

The first step is to build the URL string into a variable then use strpos() function to check if a string exists and do what you need to do as in the example below where they’re checking to see if the word, “car”, exists in the URL:

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'car') !== false) {
    echo 'Car exists.';
} else {
    echo 'No cars.';
}

To fix my problem, I simply changed the echo statement to output the theme’s settings to display the logo selected in the Theme Options and echoed the image tag with the French logo.

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

if (strpos($url,'car') !== false) {
    echo '<img src="'.$flatsome_opt['site_logo'].'" class="header_logo '.$logo_class.'" alt="'.$site_title.'"/>';
} else {
    echo '<img src="http://www.domainname.ca/wp-content/uploads/2016/11/companyname-logo-fr.png" class="header_logo '.$logo_class.'" alt="'.$site_title.'"/>';
}

I hope this helped you as it did and worked for me.

Related Posts

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.