Clearing the text from the shortcode

When publishing the announcement on a wordpress discourse, I would like to clean up automatically from the text shortcode.
[vc_row][vc_column][vc_column_text]

I’m assuming those shortcodes are being added by the Visual Composer plugin. I’ve tried a few things for dealing with them. There are three different approaches in the ‘Dealing with WordPress shortcodes’ section of this topic: WP Discourse Plugin Tips and Tricks

From what I remember, with Visual Composer shortcodes, calling do_shortcode doesn’t work. The shortcode is processed, but a bunch of whitespace is added to the markup and it gets interpreted as code by Discourse.

If you need to have the content of the shortcodes published to Discourse, probably the first approach in that topic would be the best. If it’s not giving you the formatting that you need, the regular expression could be improved so that it replaces certain shortcodes with <div></div> tags.

// Removes anything that looks like a shortcode. This will remove anything in the post that
// occurs inside of brackets `[...]`.
// Replace 'my_namespace' with your namespace.
add_filter( 'wp_discourse_excerpt', 'my_namespace_remove_shortcodes' );
function my_namespace_remove_shortcodes( $excerpt ) {
    $excerpt = preg_replace( '/\[.*\]/', '', $excerpt );

    return $excerpt;
}

And where to add this code?

In your functions.php file.

I am an absolutely terrible coder but I found a simple solution for my situation. In the discourse_publish.php file I added the following code where other trimming to $baked was taking place:

	$baked     = str_replace( '[nk_title]', '<h2>', $baked );
	$baked     = str_replace( '[/nk_title]', '</h2>', $baked );
	$baked     = str_replace( '[yp_text]', '<p>', $baked );
	$baked     = str_replace( '[/yp_text]', '</p>', $baked );		

	$baked     = preg_replace('(\\[([^[]|)*])', '', $baked );

The top 4 lines reformat some of the special shortcodes used in my theme and the bottom line removes all other […] strings.

1 Like

Yes, that’s the right idea, but you can do that in your functions.php file without having to alter the plugin. That way you won’t need to alter the plugin every time it’s updated.

Look for this line just before you’ve made those changes:
$excerpt = apply_filters( 'wp_discourse_excerpt', $raw );

This lets you create a function in your functions.php file that will be passed the raw content of your post. Something like this should work:

add_filter( 'wp_discourse_excerpt', 'my_namespace_fix_shortcodes' );

function my_namespace_fix_shortcodes( $excerpt ) {
    $excerpt     = str_replace( '[nk_title]', '<h2>', $excerpt );
    $excerpt     = str_replace( '[/nk_title]', '</h2>', $excerpt ); 
    $excerpt     = str_replace( '[yp_text]', '<p>', $excerpt );
    $excerpt     = str_replace( '[/yp_text]', '</p>', $excerpt );

    $excerpt     = preg_replace('(\\[([^[]|)*])', '', $excerpt );

    return $excerpt;
}

You’re too helpful for your own good. Thank you!

I developed a free WordPress plugin to solve this problem, the plugin named ( Shortcode Cleaner Lite )

It provides an easy way to clean up unused, broken shortcodes from WordPress content automatically, so you can switch between themes and plugins without worrying and keep your content cleanly and fresh all the time, it is dealing with any theme (Divi, Avada…etc) shortcodes that are left when changing themes or plugins or page builders (Visual Composer, Divi…etc).