Detect Visual Composer

♀尐吖头ヾ 提交于 2019-12-09 05:32:28

问题


Is there a way to detect if a WordPress page is using Visual Composer?

I have 2 different page templates:

  1. Default template for regular pages.
  2. Template for visual composer pages.

I'm hoping there is a way to detect if a user is using visual composer to build the page instead of relying on the user selecting the visual composer template each time.

Is there a way to detect what page is being built and then assign a template based on that?


回答1:


Yes, you can detect if visual composer is enabled for a post. It's stored in the _wpb_vc_js_status post meta attribute.

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

Note that a post can still contain the visual composer shortcodes, even when visual composer editing is not currently enabled. For example, if I setup a page with visual composer and then revert back to the normal editor, _wpb_vc_js_status will be false.




回答2:


Actually _wpb_vc_js_status since 4.8 is not correct, because it was not used anymore. The simplest way to check if page is using visual composer - it is check for vc_row shortcode in content.

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



回答3:


You can detect with is_plugin_active:

if ( is_plugin_active( 'js_composer/js_composer.php' ) ) {
     //your code here
}



回答4:


if( defined( 'WPB_VC_VERSION' ) ) { ... }

works like a charm. I was searching for a possibility to hide the Nag screen that occurs when you get the WPBakery Page Builder as a bundeled plugin with a commercial theme to avoid customer's confusion. In case someone could need that 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'), '/');
     }
}

Hook that into admin_init and you're good to go!



来源:https://stackoverflow.com/questions/29312067/detect-visual-composer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!