Skip to content

Instantly share code, notes, and snippets.

@aaronsummers
Last active February 8, 2018 14:45
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 aaronsummers/50b4f8f7c746a32a040ed39f80cdb5e5 to your computer and use it in GitHub Desktop.
Save aaronsummers/50b4f8f7c746a32a040ed39f80cdb5e5 to your computer and use it in GitHub Desktop.
Check metabox to add a body class, then include scripts if condition is met.
/* IN YOUR TEMPLATE FILE ABOVE THE HEADER CALL */
add_action( 'body_class', 'add_my_bodyclass');
function add_my_bodyclass( $classes ) {
global $post;
$condition = get_post_meta( $post->ID, 'METABOX_ID', true );
// Some condition to compare / check
if ( ! EMPTY( $conditon ) ) :
$classes[] = 'CUSTOM_CLASS';
endif;
$classes[] = '';
return $classes;
}
/* THEN IN THE ENQUE FUNCTION */
$classes = get_body_class(); // Check body classes must be included
if ( in_array('CUSTOM_CLASS', $classes) ) :
wp_enqueue_script( 'video', get_template_directory_uri() . '/assets/js/jquery.background-video.js', array( 'jquery' ), '1.0.0', true );
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment