Visual Composer Detection

Is there a way to determine if a WordPress page uses Visual Composer?

I have two different page templates:

  • Standard template for regular pages.
  • Template for visual composer pages.

I hope there is a way to determine if a user uses a visual composer to build a page, and not so that each user selects a visual composer template.

Is there a way to determine which page is being built and then assign a template based on this?

+7
source share
4 answers

Yes, you can determine if a visual composer is included for the message. It is stored in the _wpb_vc_js_status meta tag.

 $vc_enabled = get_post_meta($post_id, '_wpb_vc_js_status', true); 

Please note that the message may contain short codes of visual composers, even if editing the visual composer is not currently enabled. For example, if I set up a page with a visual composer and go back to the normal editor, _wpb_vc_js_status will be false.

+8
source

Actually _wpb_vc_js_status since 4.8 is incorrect because it was no longer used. The easiest way to check if a page is using a visual composer is to check the vc_row in the content.

 $post = get_post(); if ( $post && preg_match( '/vc_row/', $post->post_content ) ) { // Visual composer works on current page/post } 
+9
source

You can detect with is_plugin_active:

 if ( is_plugin_active( 'js_composer/js_composer.php' ) ) { //your code here } 
+3
source
 if( defined( 'WPB_VC_VERSION' ) ) { ... } 

works like a charm. I was looking for an opportunity to hide the Nag screen that appears when you get WPBakery Page Builder as a plugin with a commercial theme to avoid confusion with clients. In case someone might need this snippet:

 // hide nag screen of WP Bakery Visual composer if found if( defined( 'WPB_VC_VERSION' ) ) { if(!isset($_COOKIE['vchideactivationmsg_vc11'])) { setcookie('vchideactivationmsg', '1', strtotime('+3 years'), '/'); setcookie('vchideactivationmsg_vc11', WPB_VC_VERSION, strtotime('+3 years'), '/'); } } 

Connect this to admin_init and you're done!

0
source

Source: https://habr.com/ru/post/984343/


All Articles