# Clearing the text from the shortcode

**URL:** https://meta.discourse.org/t/clearing-the-text-from-the-shortcode/58036
**Category:** WordPress
**Created:** [February 27, 2017, 8:32pm UTC](https://meta.discourse.org/t/clearing-the-text-from-the-shortcode/58036 "2017-02-27T20:32:32Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![11145](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/11145/32/119636_2.png) [@11145](https://meta.discourse.org/u/11145)
#### Post date: [February 27, 2017, 8:32pm UTC](https://meta.discourse.org/t/clearing-the-text-from-the-shortcode/58036/1 "2017-02-27T20:32:32Z")

</div>

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]`

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [February 27, 2017, 9:32pm UTC](https://meta.discourse.org/t/clearing-the-text-from-the-shortcode/58036/2 "2017-02-27T21:32:32Z")

</div>

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](https://meta.discourse.org/t/wp-discourse-plugin-tips-and-tricks/50757)

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.

```plaintext
// 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;
}

```

---

<div class="post-metadata">

### Author: ![11145](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/11145/32/119636_2.png) [@11145](https://meta.discourse.org/u/11145)
#### Post date: [March 1, 2017, 3:45am UTC](https://meta.discourse.org/t/clearing-the-text-from-the-shortcode/58036/3 "2017-03-01T03:45:03Z")

</div>

And where to add this code?

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [March 1, 2017, 3:47am UTC](https://meta.discourse.org/t/clearing-the-text-from-the-shortcode/58036/4 "2017-03-01T03:47:39Z")

</div>

In your `functions.php` file.

---

<div class="post-metadata">

### Author: ![Del33t](https://avatars.discourse-cdn.com/v4/letter/d/7ab992/32.png) [@Del33t](https://meta.discourse.org/u/Del33t)
#### Post date: [March 1, 2017, 8:38pm UTC](https://meta.discourse.org/t/clearing-the-text-from-the-shortcode/58036/5 "2017-03-01T20:38:49Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [March 1, 2017, 8:52pm UTC](https://meta.discourse.org/t/clearing-the-text-from-the-shortcode/58036/6 "2017-03-01T20:52:36Z")

</div>

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:

```plaintext
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;
}

```

---

<div class="post-metadata">

### Author: ![Del33t](https://avatars.discourse-cdn.com/v4/letter/d/7ab992/32.png) [@Del33t](https://meta.discourse.org/u/Del33t)
#### Post date: [March 2, 2017, 10:11pm UTC](https://meta.discourse.org/t/clearing-the-text-from-the-shortcode/58036/7 "2017-03-02T22:11:20Z")

</div>

> [@simon](#):
>
> 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!

---

<div class="post-metadata">

### Author: ![mohamdio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mohamdio/32/89523_2.png) [@mohamdio](https://meta.discourse.org/u/mohamdio)
#### Post date: [February 12, 2018, 11:38am UTC](https://meta.discourse.org/t/clearing-the-text-from-the-shortcode/58036/8 "2018-02-12T11:38:36Z")

</div>

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

> **[Shortcode Cleaner Lite](https://wordpress.org/plugins/shortcode-cleaner-lite/)**
>
> Clean your WordPress content from unused broken shortcodes.

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).
