• Hi,

    I am using the WordPress block editor and core blocks to build the website. Is there a way to replace all of the hyphens with non-breaking hyphens?
    I’ve tried using this filter hook ‘the_content’, but it doesn’t work and it breaks the site.
    This is the code that I’ve used:

    function replace_content($content){
    $content = str_replace('-', "‑",$content);
    return $content;
    }
    add_filter('the_content','replace_content');
    

    Is there a way or any other hooks to do this when using the wordpress core blocks?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Your code works properly on my site. It could be conflicting with your theme or a plugin.

    Maybe try replacing with the equivalent HTML entity code? &#_8209; (without the underscore, which I had to insert to prevent it from being rendered as a non-breaking hyphen)

    Thread Starter rose18

    (@rose18)

    Hi @bcworkz ,

    My code was actually this:

    I did use the non-breaking hyphen HTML symbol , not sure why it just changed to that other symbol here.

    Does replacing it with the non-breaking hyphen HTML symbol work for you?

    Moderator bcworkz

    (@bcworkz)

    Ah, of course you did, I should have guessed. It’s this forum’s parser that forces entity codes into the UTF-8 equivalent char and why I put an underscore in my redundant suggestion.

    Anyway, your code works either way on my site, the UTF-8 char itself or the entity code. I did not confirm if it is really non-breaking or not, but the correct char is in place in the HTML output. If it should break anyway, it’s due to browser behavior, not WP.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WordPress Blocks – Don’t split hyphenated words’ is closed to new replies.