Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Text Filter: Replace string before saving/publishing WordPress

I need a function that will scan my unpublished/unsaved post <strong>BEFORE</strong> I publish/save it. I need the function to replace specified characters/character strings. Once that is done, it should save or publish the post with the changes. Changes should be reflected in the database so that I may output them flawlessly on wordpress via php. Scanning should take place in the following areas: <strong>titles, article content, permalink, custom fields</strong>

Primarily I've been having a problem with my blog. I need ALL instances of "–" to be replaced with "-" <strong>before saving/publishing articles.</strong> Below is an example of how the permalinks have turned out if I save/publish the article with "–" in my title.

http://blog.mysite.com/artist-–-title-of-song

when it SHOULD be:

http://blog.mysite.com/artist-title-of-song


Hopefully that makes sense. Please let me know if you have any questions.

I found this plugin: [[LINK href="http://wordpress.org/extend/plugins/text-filter-suite"]]http://wordpress.org/extend/plugins/text-filter-suite[[/LINK]]
but I'm unsure if this will work the way I want it to work.

<strong>**I also want to be able to add other filters:**</strong>
example: &amp; should show up as & in the back-end. (please see attached image)

Answers (2)

2011-01-22

Oleg Butuzov answers:

add_filter('sanitize_file_name', 'custom_sanitize');
add_filter('sanitize_name', 'custom_sanitize');
function custom_sanitize($string){
return str_replace(array('-–', '–'),array('-','-'), $string);
}


Oleg Butuzov comments:

in axample provided abov you can use first parametr of the function as patterns example, and second one as replacements example.


Andrew C comments:

No, that doesnt work. I need a function that will look at my post BEFORE I publish/save it. I nee the function to also replace specified characters. Once that is done, it should save the post with the changes.

What you provided me with doesnt alter (save over) the database entry. It simply replaces the output via php.

2011-01-22

Roberto Mas answers:

You could try renaming those in the .htaccess by creating a rewrite rule

ex: for &amp;

RewriteCond %{QUERY_STRING} ^(.*)&amp;(.*)$
RewriteRule ^(.*)$ /$1?%1&%2 [L,R=301]

Interesting post you could read this it might be another option

http://www.farfromfearless.com/2010/09/07/url-token-replacement-techniques-for-wordpress-3-0/


Andrew C comments:

I need a function that will look at my post BEFORE I publish/save it. I nee the function to also replace specified characters. Once that is done, it should save the post with the changes.