Tagged: localization, str_replace
- This topic has 7 replies, 2 voices, and was last updated 6 years ago by
bubdev.
-
AuthorPosts
-
-
January 10, 2016 at 12:39 pm #5537
bubdevMemberHi,
I’m trying to replace the default quotes in WordPress (” “) with their german equivalents („ “). Adding a filter to the_content and doing a str_replace works fine for normal post and page content but of course not for custom fields.
I found a solution using output buffering to do a string replace on everything before a page is sent to to browser. Again it works fine everywhere but my Piklist fields.
ob_start(); add_action('shutdown', function() { $final = ''; $levels = ob_get_level(); for ($i = 0; $i < $levels; $i++) { $final .= ob_get_clean(); } // Apply any filters to the final output echo apply_filters('final_output', $final); }, 0); add_filter('final_output', function($text) { $text = str_replace( '“' , '„' , $text ); $text = str_replace( '”' , '“' , $text ); $text = str_replace( '‘' , '‚' , $text ); $text = str_replace( '’' , '‘' , $text ); return $text; });I’ve already tried different hooks to no avail. Can you think of another simple solution that would work with Piklist’s custom field content without having to deal with it on a field by field basis?
Thank you
Stefan -
January 10, 2016 at 12:49 pm #5538
bubdevMemberIt looks like the output buffering solution does work with normal text (“Bar” replaces “Foo”) even for Piklist fields. Only the html entities, that I have to use for my quotes problem, are being ignored.
-
January 10, 2016 at 3:55 pm #5543
-
January 10, 2016 at 8:19 pm #5549
bubdevMemberThank you, Steve, and sorry for not making quite clear, what I’m trying to achieve. I don’t want to replace any quotation marks within my files, so the translation file won’t help. I’m aiming at the text the user types into the text editor.
In the code I posted above the Unicode entities for the quote marks have been translated to symbols on saving the post. The str_replace part should actually look as follows (without the spaces after the ampersands):
`add_filter( ‘final_output’, function( $text ) {
$text = str_replace( ‘& #8220;’ , ‘& #8222;’ , $text );
$text = str_replace( ‘& #8221;’ , ‘& #8220;’ , $text );
$text = str_replace( ‘& #8216;’ , ‘& #8218;’ , $text );
$text = str_replace( ‘& #8217;’ , ‘& #8216;’ , $text );
return $text;});`
The filter works fine everywhere on my site and replaces the quotation marks – except for the content of my Piklist text editor fields. They do accept a str_replace of normal text, only the Unicode entities are being ignored.
-
January 11, 2016 at 5:15 pm #5557
SteveKeymasterWhich version of Piklist are you using?
-
January 11, 2016 at 5:37 pm #5559
bubdevMember0.9.9.6
-
January 12, 2016 at 10:15 am #5566
SteveKeymaster@bubdev– Can you email your plugin to [email protected] ? It will be easier for us to debug if we can see the full code.
-
January 12, 2016 at 10:41 am #5571
bubdevMemberI’ve sent you an email. Thanks!
-
-
AuthorPosts
- You must be logged in to reply to this topic.