1

I'm trying to remove all unnecessary shortcodes from visual composer with some preg_replace. E.g. there is some elements like this:

[vc_row][/vc_row][vc_row el_class="hidepdf]

The best way would be to remove anything between [] starting with vc_ and followed by anything until the end bracket ]

I've tried it with following RegEx:

/\[[\/]?vc_*[^\]]\]/

But it does not seem to work.

Kirk Beard
  • 9,569
  • 12
  • 43
  • 47
Konrad
  • 77
  • 1
  • 8
  • why are you using visual composer, if you are not using it's shortcodes? also, where are you adding that code? in your functions.php? – Stender Aug 02 '17 at 08:40
  • Your regex is wrong. You have to escape the square brackets you want to match and the forward slash (within /.../) must be escaped too. try something like /\[\/?vc_row(?: .*?)\]/ – Peter Aug 02 '17 at 08:45
  • You can test your preg_replace on www.phpliveregex.com – Peter Aug 02 '17 at 08:45
  • Oh I just realized SO removed my escaping backslashes... Just think if you still need Visual Composer. If not, remove it... Your way of doing it is not great. – Peter Aug 02 '17 at 08:48
  • I'm generating pdf documents with dk_pdf and "translate" some VC shortcodes but I want to remove the rest. – Konrad Aug 02 '17 at 08:59
  • I developed a free WordPress plugin to solve this problem, the plugin named ( Shortcode Cleaner Lite ) https://wordpress.org/plugins/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, 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). – mohamdio Feb 26 '18 at 10:43

4 Answers4

2

Try this regex "/\[(\/*)?vc_(.*?)\]/"

Amit Joshi
  • 1,334
  • 1
  • 8
  • 10
  • do you have any idea how to change [vc_row el_class="hidepdf"] => to
    with preg_replace
    – Konrad Aug 02 '17 at 09:06
  • Great it worked for you. I had tested this with regex before I submitted it here. I dont know why someone downvoted it though. I only gave regex because that exactly what you seemed to be looking for. – Amit Joshi Aug 02 '17 at 09:07
  • This shoudl work for that replace: `$div = preg_replace("/\[vc_row(.*?)el_class=\"(.*?)\"\]/", "
    ", $str); ` Replace $str with your input string of course.
    – Amit Joshi Aug 02 '17 at 09:22
0
<?php
while($posts->have_posts()) {
      $postContent = get_the_content();
      //Remove visual composer tags [vc_column] etc
      $postContent = preg_replace( "/\[(\/*)?vc_(.*?)\]/", '', $postContent );
}
?>
0

To remove unnecessary wpbakery shortcodes from your WordPress content, you can use a custom function in your theme's functions.php file. Here's how you can do it:

function remove_unnecessary_shortcodes($content) {
$pattern = '/\[\/?vc_[^\]]*\]/';
$replacement = '';
$new_content = preg_replace($pattern, $replacement, $content);
return $new_content;
}
add_filter('the_content', 'remove_unnecessary_shortcodes');
-2

You can just remove the content in the admin control panel. Why should you replace the content, rofl.

But if you very-very need:

$start = preg_quote('[vc_row]', '/');
$end = preg_quote('[/vc_row]', '/');
$content = preg_replace("/$start.*$end/imsU", '', $content);
Sergej
  • 2,030
  • 1
  • 18
  • 28