Im Normalfall werden in WordPress automatisch die für die eingestellte Benutzersprache zutreffenden Anführungszeichen verwendet – vorausgesetzt der wp-content/languages
-Ordner enthält die entsprechenden Sprachdateien.
Allerdings gilt das nur für native WordPress-Felder wie the_content
oder the_excerpt
und nicht automatisch auch für Custom Fields, Widgets oder Text aus Page Buildern.
Eine weitreichende Lösung
Der folgende Code hat bei mir letztllich zum Ziel geführt: deutsche Anführungszeichen in allen Bereichen der Website, in meinem Fall also die WordPress-eigenen Felder, Advanced Custom Fields (ACF), Text aus Widgets sowie Beaver Builder.
Zuerst werden alle gewünschten Bereiche durch wptexturize()
gefiltert. Da manche Plugins jedoch die Funktion von wptexturize()
stören, überprüfen wir anschließend, ob immer noch englische (gleichzeitig aber keine deutschen) Anführungszeichen im Text zu finden sind und ersetzen sie ggf. via str_replace
.
Einen Teil der Filter Hooks habe ich von dem Plugin wp-Typography übernommen, ohne sie selbst zu testen.
add_action( 'wp', 'bubdev_change_english_to_german_quote_marks' );
/**
* Germanize Quotation Marks
*
* Changes the default English quotation marks to German (upper/lower) quotation
* marks through available filters. Custom fields can also be filtered
* using 'acf_the_content': apply_filters('acf_the_content', $field_content );
*
* @return string The HTML with updated quotation marks.
*/
function bubdev_change_english_to_german_quote_marks() {
// Content
add_filter( 'the_content' , 'bubdev_german_quotes', 12 );
add_filter( 'the_content_feed', 'bubdev_german_quotes', 12 );
add_filter( 'the_excerpt', 'bubdev_german_quotes', 12 );
add_filter( 'the_excerpt_embed', 'bubdev_german_quotes', 12 );
add_filter( 'the_excerpt_rss', 'bubdev_german_quotes', 12 );
add_filter( 'comment_author', 'bubdev_german_quotes', 12 );
add_filter( 'comment_text', 'bubdev_german_quotes', 12 );
add_filter( 'term_name', 'bubdev_german_quotes', 12 );
add_filter( 'term_description', 'bubdev_german_quotes', 12 );
add_filter( 'link_name', 'bubdev_german_quotes', 12 );
add_filter( 'widget_text', 'bubdev_german_quotes', 12 );
// Headings
add_filter( 'the_title', 'bubdev_german_quotes', 12 );
add_filter( 'the_title_rss', 'bubdev_german_quotes', 12 );
add_filter( 'single_post_title', 'bubdev_german_quotes', 12 );
add_filter( 'single_cat_title', 'bubdev_german_quotes', 12 );
add_filter( 'single_tag_title', 'bubdev_german_quotes', 12 );
add_filter( 'single_month_title', 'bubdev_german_quotes', 12 );
add_filter( 'nav_menu_attr_title', 'bubdev_german_quotes', 12 );
add_filter( 'nav_menu_description', 'bubdev_german_quotes', 12 );
add_filter( 'widget_title', 'bubdev_german_quotes', 12 );
add_filter( 'list_cats', 'bubdev_german_quotes', 12 );
// Plugins
add_filter( 'acf_the_content', 'bubdev_german_quotes', 12 ); // ACF
/**
* Replace English Quote Marks with German Quote Marks
*
* @param string $text The text to be processed.
* @return string The text with updated quotation marks.
*/
function bubdev_german_quotes( $text ) {
// Set a flag to determine if 'str_replace' should be used for quote mark replacements.
$use_str_replace = false;
// Apply 'wptexturize' to the text if the locale is set to German, which usually
// handles the replacement of English with German quote marks. If the locale is not
// German, exit the function and return the original text.
if ( strpos( get_locale(), 'de_' ) === 0 ) {
$text = wptexturize( $text );
}
else {
return $text;
}
// Check if English quote marks are still being used: If the text contains
// English opening quote marks but not their German equivalents, update the
// flag to enable 'str_replace'.
if (
// English curly double open vs. German double open:
( strpos( $text, '“' ) !== false && strpos( $text, '„' ) === false ) ||
// English curly single open vs. German single open:
( strpos( $text, '‘' ) !== false && strpos( $text, '‚' ) === false )
) {
$use_str_replace = true;
}
// Replace English curly quotes with German quotes.
if ( $use_str_replace ) {
$text = str_replace( '“' , '„' , $text ); // replace english curly double open
$text = str_replace( '”' , '“' , $text ); // replace english curly double close
$text = str_replace( '‘' , '‚' , $text ); // replace english curly single open
$text = str_replace( '’' , '‘' , $text ); // replace english curly single close
}
return $text;
}
}
Bestimmte Plugins sind bekannt dafür, sich in wptexturize()
einzumischen und Probleme mit deutschen Anführungszeichen zu verursachen. Statt nur auf die Verwendung englischer Anführungszeichen zu prüfen, können wir auch überprüfen, ob eines dieser Plugins aktiv ist und die Flagge entsprechend setzen. Bitte aktualisieren Sie die Liste mit den problematischen Plugins:
// Certain plugins are known to interfere with 'wptexturize' and cause
// issues with German quote marks. Let's check if any of these plugins are
// active and set the flag accordingly. Update the list with any newly
// discovered plugins.
// $interfering_plugin_paths = array(
'mollie-payments-for-woocommerce/mollie-payments-for-woocommerce.php',
'my-private-site/my-private-site.php',
'wp-statistics/wp-statistics.php',
'really-simple-ssl/really-simple-ssl.php',
);
foreach ( $interfering_plugin_paths as $plugin_path ) {
if ( is_plugin_active( $plugin_path ) ) {
$use_str_replace = true;
break;
}
}
Alternativen: wptexturize() vs. str_replace
Während ein Filter mit st_replace()
der jeweiligen Anführungszeichen bzw. ihrer HTML-Codes teilweise zum Ziel führt (Custom Fields), hatte ich nur mit wptexturize()
auch bei Output aus Page Buildern, z.B. Beaver Builder, Erfolg.
// Filtering with str_replace()
function bubdev_german_quotes( $text ) {
$text = str_replace( '“' , '„' , $text );
$text = str_replace( '”' , '“' , $text );
$text = str_replace( '‘' , '‚' , $text );
$text = str_replace( '’' , '‘' , $text );
return $text;
}
// Filtering with wptexturize()
function bubdev_german_quotes( $text ) {
$text = wptexturize( $text );
return $text;
}
Deutsche Anführungszeichen mit Guillemets ersetzen
Manche bevorzugen im Deutschen die Verwendung umgekehrter französische Guillemets. Das folgende Snippet dient dazu, die deutschen Anführungszeichen in deutschen WordPress-Installationen zu ersetzen und berücksichtigt auch solche Anführungszeichen, die unachtsamerweise duch Copy & Paste aus einer externen Quelle übernommen wurden.
// Replace german quote marks with reverse Guillemets.
$text = str_replace( '„' , '»' , $text ); // replace german double open
$text = str_replace( '“' , '«' , $text ); // replace german double close
$text = str_replace( '‚' , '›' , $text ); // replace german single open
$text = str_replace( '‘' , '‹' , $text ); // replace single close
// Replace pasted german quote marks (from external source) with reverse Guillemets.
$text = str_replace( '„' , '»' , $text ); // replace german double open
$text = str_replace( '“' , '«' , $text ); // replace german double close
$text = str_replace( '‚' , '›' , $text ); // replace german single open
$text = str_replace( '‘' , '‹' , $text ); // replace single close
Deaktivieren der wptexturize
-Funktion
Um wptexturize()
zu umgehen und den originalen Text, der der Funktion übergeben wurde, auszugeben, können wir folgenden Filter verwenden. Dieser Filter wird nur einmal angewendet, nämlich beim ersten Aufruf von wptexturize()
, was normalerweise ausreichend ist, um die Umwandlung der geraden englischen Anführungszeichen in geschweifte zu verhindern.
/**
* Short-circuit 'wptexturize'
*
* The filter runs only once, the first time 'wptexturize' is called, which can
* be sufficient to disable curly quote marks throughout the site.
*
* Limitations: For example, if you have a plugin that uses the 'wptexturize'
* function, the curly replacement will still be applied to the content that is
* generated by the plugin.
*/
add_filter( 'run_wptexturize', '__return_false' );
Um wptexturize()
gezielt von den Standardbereichen zu entfernen, können wir folgendes Snippet verwenden:
add_action( 'wp', 'bubdev_disable_wptexturize_at_wp_default_locations' );
/**
* Disable 'wptexturize'
*
* Disables 'wptexturize' at its default locations.
*/
function bubdev_disable_wptexturize_at_wp_default_locations() {
remove_filter( 'the_content', 'wptexturize' );
remove_filter( 'the_title', 'wptexturize' );
remove_filter( 'the_excerpt', 'wptexturize' );
remove_filter( 'comment_text', 'wptexturize' );
remove_filter( 'widget_title', 'wptexturize' );
}
Falls es aus irgendeinem Grund nicht möglich ist, die englischen geschweiften Anführungszeichen abzuschalten, können wir stattdessen gezielt eine Umwandlung in gerade Anführungszeichen vornehmen:
function bubdev_german_quotes( $text ) {
// Replace English curly with straight quotes.
$text = str_replace( '“' , '"' , $text ); // replace curly english double open
$text = str_replace( '”' , '"' , $text ); // replace curly english double close
$text = str_replace( '‘' , ''' , $text ); // replace curly english single open
$text = str_replace( '’' , ''' , $text ); // replace curly english single close
}
Anwendung der Filter auf Ausgabewerte
Wo es nötig ist, werden die Filter explizit auf die Ausgabewerte angewendet:
echo apply_filters( 'the_content', $field_content );
Für die Arbeit mit Advanced Custom Fields bietet sich alternativ der Hook acf_the_content
an.
// Replace english quotes in ACF wysiwyg and textarea fields.
if ( function_exists( 'get_field' ) ) {
$acf_field_object = get_field_object( $field_name );
if ( in_array( $acf_field_object['type'], array( 'wysiwyg', 'textarea' ) ) ) {
$field_value = apply_filters( 'acf_the_content', $field_value );
}
}
Um in reinen ACF-Textfeldern unerwünschte paragraph tags zu vermeiden, können wir wpautop()
für acf_the_content
deaktivieren und es stattdessen ausdrücklich nur dort anwenden, wo wir es wünschen. Das müsste eigentlich auch umgekehrt funktionieren, allerdings habe ich das nicht probiert.
add_action( 'wp', 'bubdev_acf_add_remove_filters' );
/**
* Customize ACF Filters
*/
function bubdev_acf_add_remove_filters() {
if ( ! function_exists( 'get_field' ) ) return;
// Remove paragraph tags from 'acf_the_content' filter.
// We can then use it for plain text fields, e.g. to
// replace english quotes.
remove_filter ( 'acf_the_content', 'wpautop' );
}
Redaktionelle Vorsicht
Beim Kopieren von Text aus PDFs oder Textvearbeitungsprogrammen enthalten die Anführungszeichen meist Formatierungen, die von WordPress übernommen werden.
Entweder wird der Text unformatiert in den WordPress-Editor kopiert, die Formatierung für den gesamten Text wird innerhalb des Editors entfernt oder die Anführungszeichen werden einzeln gelöscht und neu eingegeben.
Nur so können sie von WordPress richtig interpretiert und dargestellt werden.
Weitere Snippets
add_filter( 'gettext_with_context', 'bubdev_use_straight_english_quotes_everywhere', 10, 4 );
/**
* Use Straight English Quote Marks Everywhere
*
* Changes WP's curly quotes to normal ones everywhere. Also overwrites
* WP's translation specific quotation marks (e.g. upper-lower), which
* tend to be inconsistant throughout a site (e.g. inside custom fields).
*/
function bubdev_use_straight_english_quotes_everywhere( $translations, $text, $context, $domain ) {
if ( 'opening curly single quote' == $context && '‘' == $text )
$translations = ''';
if ( 'closing curly single quote' == $context && '’' == $text )
$translations = ''';
if ( 'opening curly double quote' == $context && '“' == $text )
$translations = '"';
if ( 'closing curly double quote' == $context && '”' == $text )
$translations = '"';
return $translations;
}
Tags: deutsche anführungszeichen, german quotes, german quotation marks, german quote marks, smart quotes, straight quotes