Tags: unfilteritemcontents
Manipulating the title with a plugin
tilqicom came with a problem. He wanted some specific changes to the title of a post automated. At first I got it wrong, thinking that he wanted some text replacement in the body of a post. That's fairly easy, use the RenderItemAsHtml function in a plugin. When I finished that I heard that he specifically wanted to automatically change some text in the title. We are talking about a change like the input '[date]' to 'Tuesday March 24 2008'.
That caused some serious problems because I hadn't seen it done before. ¥åßßå however had and pointed me in the right direction, I had to use the (Un)FilterItemContents( & $params ) functions. They filter (and you can make changes) at the point where you enter the write section and at leaving it (hit 'save'). Furthermore the $params are passed through the functions so both $params[ 'title' ] and $params[ 'content' ] are available.
Let's have a look at the results. This is the FilterItemContents function that is called after leaving the edit / write section of a post:
PHP:
function FilterItemContents( & $params ) | |
{ | |
$title = & $params[ 'title' ]; | |
$content = & $params[ 'content' ]; | |
| |
if( strpos( $title, '[cat wrote]' ) !== false ) | |
{ | |
global $edited_Item; | |
$original_title = $title; | |
| |
$foo = get_the_category_by_ID( $edited_Item->main_cat_ID ); | |
| |
$title = str_replace( '[cat wrote]', $foo[ 'cat_name' ] . ' wrote: ' . implode( ', ', $edited_Item->get_tags() ), $title ); | |
| |
$params[ 'title' ] = $title; | |
$original_title = '<!-- item title ' . $original_title . ' -->'; | |
$content = $content . $original_title; | |
$params[ 'content' ] = $content; | |
} | |
| |
return true; | |
} |
The magic quote here is '[cat wrote]' in the title of the post, so we first filter the title on the presence of that line. The long codeline does the hard job, it changes the magic block into something else and if you plan to use this block, this is where you should focus your attention upon.
What's next is an improvement I made later on. We store the original title in a comment in the post. That we will retrieve in the UnFilterItemComments function.
In that function we see a partial reverse, I am trying to restore the original situation:
PHP:
function UnfilterItemContents( & $params ) | |
{ | |
$title = & $params[ 'title' ]; | |
$content = & $params[ 'content' ]; | |
| |
// global $edited_Item; | |
if( strpos( $content, '<!-- item title' ) ) | |
{ | |
$original_title = $content; | |
preg_match( '~<\!\-\- item title (.+?) \-\->~', $original_title, $original_title_array ); | |
| |
$params[ 'title' ] = $original_title_array[1]; | |
| |
$content = preg_replace( '#<!-- item title .* -->#', '', $content ); | |
$params[ 'content' ] = $content; | |
| |
} | |
| |
return true; | |
} |
First of all we look for the comment in the body of the post. If it's there we extract the original title, place it back where it belongs and delete the comment.
So the way I did it is nearly failsafe. The only point is that the plugin needs to be set to 'always' or 'stealth' to avoid nasty complications.
Any one care to find out what *this* plugin actually does with the title?