Problem in wordpress with “-“

Background:

WordPress converts normal dash (-) to long dash (), straight quotes to curly quotes and some other similar symbols and punctuations to their printer friendly versions using wptexturize.

Generally it’s recommended to leave them up to WordPress. However, occasionally, we may want to override this behaviour. For example, in case we are writing Programming CODE or command and want people to copy paste them.

Solution:

One way to avoid this conversion is to have these CODE inside <code></code> block. That way WordPress will know that they are meant to be kept as is. However, we may have already written it and don’t want a rewrite. In that case, it’s possible to stop WordPress to do these auto conversions all together by disabling wptexturize.

For WordPress 4.0 and above, it’s easy to do using the following CODE in a plugin or your theme’s functions.php file:

add_filter( 'run_wptexturize', '__return_false' );

For before WordPress 4.0, you’ll need a little more CODE:

foreach( array(
    'bloginfo',
    'the_content',
    'the_excerpt',
    'the_title',
    'comment_text',
    'comment_author',
    'link_name',
    'link_description',
    'link_notes',
    'list_cats',
    'nav_menu_attr_title',
    'nav_menu_description',
    'single_post_title',
    'single_cat_title',
    'single_tag_title',
    'single_month_title',
    'term_description',
    'term_name',
    'widget_title',
    'wp_title'
) as $texturize_disable_for )
remove_filter( $texturize_disable_for, 'wptexturize' );

Of course, you may choose to disable wptexturize only for part of your content. Say, to disable only for title, you may use:

remove_filter( 'the_title', 'wptexturize' );

Leave a Comment