Skip to content

Instantly share code, notes, and snippets.

@NickDeckerDevs
Last active May 23, 2018 15:09
Show Gist options
  • Select an option

  • Save NickDeckerDevs/dde049e9f9872a95ac21ef084b98069e to your computer and use it in GitHub Desktop.

Select an option

Save NickDeckerDevs/dde049e9f9872a95ac21ef084b98069e to your computer and use it in GitHub Desktop.
Wordpress - bring in custom scripts and stylesheets on each page using custom field / page meta
<?php
function tierpoint_styles() {
$parent_style = 'parent_style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style ), 90
);
$post = get_post();
$custom_css = get_post_meta( $post->ID, 'temp_css', true );
// var_dump($custom_css);
if($custom_css) {
// die(get_stylesheet_directory_uri() . $custom_css);
wp_enqueue_style( 'square2', get_stylesheet_directory_uri() . $custom_css, 99);
} else {
wp_enqueue_style( 'square2', get_stylesheet_directory_uri() . '/css/square2styles.css', 99);
}
}
add_action( 'wp_enqueue_scripts', 'tierpoint_styles' );
function tierpoint_scripts() {
wp_enqueue_script( 'tierpoint_main_scripting', get_stylesheet_directory_uri() . '/js/tierpoint.js', [ 'jquery' ], 1.1, true );
// checking custom fields for script1 script2 and script3 and enqueing them in the wp hook
$post = get_post();
$script1 = get_post_meta( $post->ID, 'script1', true );
$script2 = get_post_meta( $post->ID, 'script2', true );
$script3 = get_post_meta( $post->ID, 'script3', true );
if( $script1 ) {
// options: unique name, file location, dependents, version, in footer
wp_enqueue_script( 'dynamic_script_1', get_stylesheet_directory_uri() . $script1, [ 'jquery' ], 1.1, true );
}
if( $script2 ) {
wp_enqueue_script( 'dynamic_script_2', get_stylesheet_directory_uri() . $script2, [ 'jquery' ], 1.1, true );
}
if( $script3 ) {
wp_enqueue_script( 'dynamic_script_3', get_stylesheet_directory_uri() . $script3, [ 'jquery' ], 1.1, true );
}
}
add_action( 'wp_enqueue_scripts', 'tierpoint_scripts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment