Skip to content

Instantly share code, notes, and snippets.

@Page-Carbajal
Created June 17, 2016 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Page-Carbajal/a033555b2a73fd8507714651c0909519 to your computer and use it in GitHub Desktop.
Save Page-Carbajal/a033555b2a73fd8507714651c0909519 to your computer and use it in GitHub Desktop.
WordPress - How to dynamically load scripts and styles based on post content
<?php
add_action('template_redirect','justIfContentHas');
function justIfContentHas()
{
global $post;
// Set the condition for loading your resources
if( has_shortcode($post->post_content, 'my_short_code') ){
$pathToFile = dir(__FILE__);
wp_enqueue_scripts('handler', $pathToFile, array('jquery','or','your','dependencies') );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment