Question

In my WooCommerce online store, I frequently copy and paste product description from the OEM webpage. This works well for the most part, however depending on the site, the spaces are sometimes encoded as &nbsp. While this renders just fine in the browser, it does cause issues with text wrapping as the browser appears to see the string as one long word (so the text will inevitably overflow, unless I use word-break which will just break words in the middle). What I'd like to have, ideally, is something that automatically converts these &nbsp to a regular space character. Is this possible, are there any downsides and what are other solutions to consider?

Was it helpful?

Solution

Yes, this is possible. You could do it when saving the post, or just when rendering (which won't change the actual content in the editor).

Using the_content filter, replacing characters in rendering:

function wpse_387560( $content ) {
    $content = str_replace( ' ', ' ', $content );
    return $content;
}

add_filter( 'the_content', 'wpse_387560' );

Using the wp_insert_post_data hook, replacing them in the content when saving:

function wpse_387560( $data ) {
    $data['post_content'] = str_replace( ' ', ' ', $data['post_content'] );
    return $data;
}

add_filter( 'wp_insert_post_data', 'wpse_387560' );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top