Adding filters to child theme in wordpress

I have a child theme in wordpress that is based on twentyten.

Some of my authors have hardcoded URLs in their post titles and I want to remove those URLs.

Read More

I put the following code in my functions.php file in the child theme, but it has no effect on the display of the post title:

add_filter( ‘the_title’, ‘ib_strip_tags_from_titles’ );
function ib_strip_tags_from_titles( $title ) {
  $title = strip_tags( $title );
  return $title;
}

Any suggestions?

Related posts

Leave a Reply

1 comment

  1. strip_tags() only removes HTML tags – in your case it will change the title from

    Some Text <a href="http://someurl.com">LINK</a> Other Text

    to Some Text LINK Other Text

    If I understand you correctly, this is what you want:

    function ib_remove_links_from_titles($title) {
        $title = preg_replace('/<a([^<]*)">([^<]*)</a>/', '', $title);
        return $title;
    }
    add_filter( 'the_title', 'ib_remove_links_from_titles' );
    

    going with the above example it will output Some Text Other Text

    Note that given that you tried to accomplish the task with strip_tags(), I am assuming the “harcoded URLs”, as you described them, are enclosed in <a [...] ></a> tags. If that’s not the case you would need a regular expression that matches URLs. That is much more tricky, depending on whether the URLs your authors use are internationalized / have different domains, are not all just http:// prefaced and so on.

    I vouch for the above to work if they are enclosed in tags, if not, this regex will catch most URLs, but comes without my guarantee to work in every case:

    (([A-Za-z]{3,9})://)?([-;:&=+$,w]+@{1})?(([-A-Za-z0-9]+.)+[A-Za-z]{2,3})(:d+)?((/[-+~%/.w]+)?/?([&?][-+=&;%@.w]+)?(#[w]+)?)?

    You’d have put that between the ‘/ and /’ in the above function.