Skip to content
This repository has been archived by the owner on May 31, 2021. It is now read-only.

How do I check for the presence of a shortcode within the contents of the AQPB template? #199

Open
monecchi opened this issue Apr 30, 2017 · 0 comments

Comments

@monecchi
Copy link

I'm using the following function in order to only enqueue Contact Form 7 plugin dependencies if its shortcode is present within the page content.

If I place CF7 shortcode directly in the editor while creating a page, the bellow function works, however, I'm building page templates with AQPB and so CF7 shortcode is present within my AQPB blocks, what forces me to place only the AQPB shortcode in the editor.

How would one check for the presence of a shortcode within the page content coming from the AQPB templates?

/**
 * Contact Form 7 - Load css, js only when its necessary
 * Enqueue Contact Form 7 plugin dependencies only if its shortcode is present
 */

function cf7_dequeue_scripts() {
  $load_scripts = false;
  if( is_front_page() | is_page() | is_singular() ) {
    $post = get_post();
    if( has_shortcode($post->post_content, 'contact-form-7') ) {
      $load_scripts = true;
      //Additional scripts that may be used with CF7
      // wp_enqueue_style( 'select2-style', '//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css' );
      // wp_enqueue_script( 'select2-js', '//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js' );
    }
  }
  if( ! $load_scripts ) {
    wp_dequeue_script( 'contact-form-7' );
    wp_dequeue_style( 'contact-form-7' );
  }
}

add_action( 'wp_enqueue_scripts', 'cf7_dequeue_scripts', 99 );
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant