How To Highlight A Slogan In WordPress

In this example I'm talking about how to display a specific company slogan in a certain color. This slogan is found in the header and content.

Of course my client doesn't want to put in span class="foo" every time. This is what it should look like:

highligt a slogan in WordPress

Therefor I have to write a function. First I have to search for the string "by *FOO" and wrap a span around "by" as well as around "*FOO", since it is also displayed without "by". If I added it to the first function "*FOO" which already has a span would get an additional span. Here is the code:

function colorize_FOO($content) {
	$str = "*FOO";
	$newstr = "<span class=\"foo\">*FOO</span>";
	$html = str_replace($str, $newstr, $content);
	return $html;
}
function colorize_by($content) {
	$str = "by *FOO";
	$newstr = "<span class=\"by\">by</span> *FOO";
	$html = str_replace($str, $newstr, $content);
	return $html;
}

add_filter('the_title', 'colorize_by',1);
add_filter('the_content', 'colorize_by',1);
add_filter('the_title', 'colorize_FOO',1);
add_filter('the_content', 'colorize_FOO',1);

Sure there are more elegant methods, but a better one didn't come in my mind so quickly 😉

Comments are closed.

4 comments

  1. Pascal

    If you first think in German and then write it down in English, it'll be very ugly text...

  2. Pete

    Hi Michael,
    dumb question from a only-copy-paste-coder: What php-file of a theme-template would be the right one for this code and at what place (order before after section x)??

    A hint would be great!

    Thanky in advance,
    Pete

  3. Michael

    Hi Pete,
    copy the code in the functions.php of your theme. No special place is needed.

One pingback

  1. Added by a PAL to FAQ PAL