Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Better way to conditionally enqueue scripts #44

Open
JiveDig opened this issue May 4, 2018 · 3 comments
Open

Better way to conditionally enqueue scripts #44

JiveDig opened this issue May 4, 2018 · 3 comments

Comments

@JiveDig
Copy link

JiveDig commented May 4, 2018

Right now it's using this:

global $post;
$content = $post->post_content;
// Load assets if in post content.
if ( has_shortcode( $content, 'gs_faq' ) ) {
	Genesis_Simple_FAQ()->assets->enqueue_scripts();
}

This is really limiting IMO. What if we want to use the shortcode in a meta field in php via do_shortcode()? (sidenote, there should be a template tag for php)

The scripts are already registered globally in the register_scripts() method. Great.

Why not just enqueue them inside the shortcode? I do that all the time, and that insures the script always loads any time the shortcode is used, instead of guessing/hoping it's in the content.

So, inside the Genesis_Simple_FAQ_Shortcode class -> shortcode() method, after if ( $faqs->have_posts() ) { should be Genesis_Simple_FAQ()->assets->enqueue_scripts();

In Mai Theme we use a CMB2 repeater field (meta) to initially store each section content. That's when we found out the shortcode doesn't work in a section.

@JiveDig
Copy link
Author

JiveDig commented May 4, 2018

I was able to achieve my goal with this:

// Force Genesis Simple FAQs scripts to load any time faq template is loaded.
add_filter( 'gs_faq_template', function( $template ) {
	if ( function_exists( 'Genesis_Simple_FAQ' ) ) {
		Genesis_Simple_FAQ()->assets->enqueue_scripts();
	}
	return $template;
});

@seothemes
Copy link

@JiveDig thanks for sharing, saved me having to write it. Came across this issue today with a customer trying to use the shortcode in a front page widget.

@JiveDig
Copy link
Author

JiveDig commented Mar 6, 2019

No prob 👊

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

2 participants