4

When I put -- into a post it is automatically converted to the – – character in the output by wordpress.

How can I get normal '--' double dashes in my content.

4 Answers 4

8

In your functions.php:

remove_filter( 'the_content', 'wptexturize' );

And the same for the_excerpt or the_title (if required).

6
  • what else does this disable, or does it just target the -- conversion?
    – radman
    Aug 1, 2012 at 19:36
  • @radman View codex - wptexturize for what this function does, obviously filtering this function will stop all other conversions mentioned there.
    – Amit Kosti
    Aug 3, 2012 at 18:29
  • Is there a way to disable 'wptexture' specifically for embeds (github gists)? I've had readers on my blog unable to copy a line from a gist with a minus sign. They would get syntax errors because it was replaced by an emdash.
    – Antonvh
    Oct 7, 2020 at 9:29
  • @Antonvh What is the embed plugin/code you're using? I'm not aware of core WordPress supporting gist embeds. Oct 7, 2020 at 11:00
  • I used the default Gutenberg embed URL block. Pasted a link to my gist inside: gist.github.com/antonvh/695ab1c36b61171714a84f700858cdb3
    – Antonvh
    Oct 12, 2020 at 18:18
1

I found a solution: If you are using WordPress, in the beginning of your file single.php add these lines:

<?php
remove_filter( 'the_title', 'wptexturize' );
remove_filter( 'the_content', 'wptexturize' );
remove_filter( 'the_excerpt', 'wptexturize' );
?>

And do your test. I am 99.99% sure that will work because I had the same problem and I fixed it using that lines 10 seconds ago. ;)

1

Another solution that override the origin function of wptexturize and update all related filters.

define('EXCERPT_RARELY_2', '{[2}]');
function wptexturize_custom($text = '')
{
    $text = preg_replace('!(^|[^\-])\-\-([^\-]|$)!', '$1' . EXCERPT_RARELY_2 . '$2', $text);

    // get through origin filter
    $text = wptexturize($text);

    return str_replace(EXCERPT_RARELY_2, '--', $text);
}

// remove default filter
remove_filter('the_content', 'wptexturize');

// add custom filter
add_filter('the_content', 'wptexturize_custom');

// remove default filter
remove_filter('the_excerpt', 'wptexturize');

// add custom filter
add_filter('the_excerpt', 'wptexturize_custom');
1

I had the same problem as the OP and the solution proposed by @TheDeadMedic almost worked for me. But my WP version has several function_something.php files and I had to add this to them all (making sure to add them after the php opening code <?php):

remove_filter( 'the_title', 'wptexturize' );
remove_filter( 'the_content', 'wptexturize' );
remove_filter( 'the_excerpt', 'wptexturize' );

Of course only one of them would be needed if you only want to remove the filter for titles, content or excerpt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.