Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can actually do this much more simply than what you're trying. You're on the right track though. </p> <p>Basically, what you want to do is create your own function that will strip out the "Protected: " part of the WordPress titles. The easiest way to do this is to simply create a function that uses preg_replace() to search for the "Protected: " text and strip it. You could just as easily have it auto-replace the string with your own text. </p> <p>Here's a sample function that does that. We're taking the $title as a parameter and returning the modified version of it.</p> <pre><code>function remove_protected_text($title) { $match = '/Protected: /'; $replacement = ''; $title = preg_replace($match, $replacement, $title); return $title; } </code></pre> <p>The next thing we want to do is actually add our function to a filter hook. The filter hook that we're interested in in this cases is 'the_title'. So, we add the following line below the function we just wrote:</p> <pre><code>add_filter( 'the_title', 'remove_protected_text', 10); </code></pre> <p>This adds our function <code>remove_protected_text()</code> to the 'the_title' filter. In this case I've used the third argument to give our filter a priority of 10. This is totally optional, but I figure this filter is a pretty low priority.</p> <p>So all together our code should look like this:</p> <pre><code>function remove_protected_text($title) { $match = '/Protected: /'; $replacement = ''; $title = preg_replace($match, $replacement, $title); return $title; } add_filter( 'the_title', 'remove_protected_text', 10); </code></pre> <p>Adding that code to the functions.php file in your theme will allow it to work. You can write filters like this for most of the parts of WordPress that output text.</p> <p><strong>Update</strong></p> <p>Here's a revised version of the function that should get the translated string of "Protected: " and remove it:</p> <pre><code>function remove_protected_text($title) { $protected = __('Protected: %s'); $protected = preg_replace('/ %s/', '', $protected); $match = "/${protected}/"; $replacement = ''; $title = preg_replace($match, $replacement, $title); return $title; } add_filter( 'the_title', 'remove_protected_text'); </code></pre> <p>Basically the only change here is that we are using the __() function to translate the protected string and then striping out the extra bits. This is kind of hackish, and I'm sure there's a better way to do it, but it does work in my testing.</p> <p>I tested this out on a Spanish version of WordPress and it worked, so let me know if it works for your project.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload