WordPress.org

Ready to get started?Download WordPress

Forums

podPress
[resolved] Possible to modify feed titles? (5 posts)

  1. MCM
    Member
    Posted 1 year ago #

    I have post titles like this for SEO.

    Speaker Name - Lecture Title

    e.g. Joe Blogs - Life is Great sometimes

    I noticed in iTunes that the title need not have the 'Joe Blogs - ' part.

    Is it possible to have something remove this bit JUST for the feed?

    http://wordpress.org/extend/plugins/podpress/

  2. ntm
    Member
    Plugin Author

    Posted 1 year ago #

    This is something you could solve also with a little filter function. A Filter Hook which suitable is probably the_title_rss.
    If you add a filter to this hook, you can filter and modify the post titles in RSS and ATOM feeds of the blog.
    The code could look like:

    add_filter('the_title_rss', 'pff_modify_post_title');
    function pff_modify_post_title($title) {
    printphpnotices_var_dump($title);
    	if ( FALSE === empty($title) ) {
    		$title_last_part = end( mb_split(' – ', strval($title)) );
    		if ( FALSE === empty($title_last_part) ) {
    			$title = $title_last_part;
    		}
    	}
    	return $title;
    }

    You can use for instance mb_split() to cut the title at the hyphen in to parts. The function end() gives you the last part of an array (the last of the two parts). The search criteria is in this case the hyphen and leading and a trailing white space. (& #8211; is the numeric character reference for the hyphen character (minus))

  3. MCM
    Member
    Posted 1 year ago #

    I got this to work on my local dev install uploaded to my live system not sure if it is all the caching (cloudflare and wp_supercache) all of which I cleared but not working yet.

    add_filter('the_title_rss', 'pff_modify_post_title');
    function pff_modify_post_title($title) {
    
    	if ( FALSE === empty($title) )
    	{
    		$str = "–";
    		$title_split = mb_split($str, strval($title));
    
    		if ( FALSE === empty($title_last_part) )
    		{
    			$title = $$title_split[1];
    			if (sizeof($$title_split) > 2)
    			{
    				$title = $$title_split[1] . ' ' . $title = $$title_split[2];;
    			}
    		}
    	}
    	return $title ;
    }
  4. ntm
    Member
    Plugin Author

    Posted 1 year ago #

    First of all I noticed a line in my code which needs to be removed:

    printphpnotices_var_dump($title);

    is a call to a function which I use for debugging purposes and which is pat of a different plugin. I forgot to remove the line from the snippet before posting.

    to your filter:
    The cache plugins are most likely not the problem.
    The variable $title_last_part is not defined in your code. That means that the condition if ( FALSE === empty($title_last_part) ) will never be fulfilled and is one reason why your is not working.

    You should be aware that index of the array which may originate from mb_split starts with 0 and not with 1.
    I'm also not sure why you check for a case in which $title_split has more than 2 parts.
    Are we still talking about a case like you described in your first post?
    If that is the case then the condition should probably be if (sizeof($title_split) >= 2).
    Also the line

    $title = $$title_split[1] . ' ' . $title = $$title_split[2];;

    should read

    $title = $title_split[0] . ' ' . $title = $title_split[1];

    Which brings me to a further issue. Why do you use double dollar characters and double semicolons?

    Furthermore I don't understand why $title = $$title_split[1]; is outside the condition which checks whether the $title_split array has more than two parts? If you leave it were it is right now than it overwrites $title in every case and not only when it contains a hyphen. Combined with the index mistake that means that the returned $title is empty when the original $title does not contain a hyphen.
    I believe you can remove this line.

    function pff_modify_post_title($title) {
    	if ( FALSE === empty($title) )
    	{
    		$str = "–";
    		$title_split = mb_split($str, strval($title));
    		if ( FALSE === empty($title_split) )
    		{
    			if (sizeof($title_split) >= 2)
    			{
    				$title = $title_split[0] . ' ' . $title = $title_split[1];
    			}
    		}
    	}
    	return $title ;
    }

    You can probably simplify (maybe combine) the inner conditions.

    However it is possible that the hyphen characters in the titles are encoded. That is why you may use the numerical character reference instead of using the "-" character in the code.

  5. MCM
    Member
    Posted 1 year ago #

    Apologies, perhaps I forgot to REDO after UNDO or something..

    So, no my requirement is still the same and tested it this morning and works 100%.

    Speaker Name - Lecture Title
    e.g. Joe Blogs - Life is Great sometimes

    It it now displays 'Life is Great somtimes'

    The reason for the IF on the sizeOf is because sometimes the title is as folllows

    Part A - Part B - Part C

    and I want to remove Part A only and show Parts B and C and even possibly Part D and E if applicable.

    add_filter('the_title_rss', 'pff_modify_post_title');
    function pff_modify_post_title($title) {
    
    	if ( FALSE === empty($title) )
    	{
    		$str = "–";
    		$title_split = mb_split($str, strval($title));
    
    		if ( FALSE === empty($title_split) )
    		{
    			$title = $title_split[1];
    			if (sizeof($$title_split) > 2)
    			{
    				$title = $title_split[1] . ' ' . $title = $title_split[2];
    			}
    		}
    	}
    	return $title ;
    }

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic