Create a gist now

Instantly share code, notes, and snippets.

WordPress theme twentyfifteen_scripts function modified to load page or post specific stylesheets and or scripts using custom fields. More info at http://jimfrenette.com/2015/07/wordpress-page-specific-styles-or-scripts/
/**
* This is the twentyfifteen_scripts function and its respective add_action from
* the functions.php file. This function has been modified to load page or post
* specific stylesheets and or scripts using custom fields.
* You can view the original unmodified functions.php file in svn at
* https://themes.svn.wordpress.org/twentyfifteen/1.0/functions.php
*
* Enqueue scripts and styles.
*
* @since Twenty Fifteen 1.0
*/
function twentyfifteen_scripts() {
// retrieve custom fields from the current post
$custom_fields = get_post_custom();
// Add custom fonts, used in the main stylesheet.
wp_enqueue_style( 'twentyfifteen-fonts', twentyfifteen_fonts_url(), array(), null );
// Add Genericons, used in the main stylesheet.
wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.2' );
// Load our main stylesheet.
wp_enqueue_style( 'twentyfifteen-style', get_stylesheet_uri() );
// Load the Internet Explorer specific stylesheet.
wp_enqueue_style( 'twentyfifteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentyfifteen-style' ), '20141010' );
wp_style_add_data( 'twentyfifteen-ie', 'conditional', 'lt IE 9' );
// Load the Internet Explorer 7 specific stylesheet.
wp_enqueue_style( 'twentyfifteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentyfifteen-style' ), '20141010' );
wp_style_add_data( 'twentyfifteen-ie7', 'conditional', 'lt IE 8' );
wp_style_add_data( 'twentyfifteen-ie7', 'conditional', 'lt IE 8' );
// styles custom_field
if (!empty($custom_fields['bn_styles'])) {
foreach ( $custom_fields['bn_styles'] as $key => $value ) {
wp_enqueue_style('twentyfifteen_custom_field_' . $key, get_template_directory_uri() . '/css/' . $value, false, null);
}
}
wp_enqueue_script( 'twentyfifteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20141010', true );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
if ( is_singular() && wp_attachment_is_image() ) {
wp_enqueue_script( 'twentyfifteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20141010' );
}
wp_enqueue_script( 'twentyfifteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20150330', true );
wp_enqueue_script( 'twentyfifteen-script', get_template_directory_uri() . '/js/main.js', array( 'jquery' ), '20150527', true );
wp_localize_script( 'twentyfifteen-script', 'screenReaderText', array(
'expand' => '<span class="screen-reader-text">' . __( 'expand child menu', 'twentyfifteen' ) . '</span>',
'collapse' => '<span class="screen-reader-text">' . __( 'collapse child menu', 'twentyfifteen' ) . '</span>',
) );
// scripts custom_field
if (!empty($custom_fields['bn_scripts'])) {
foreach ( $custom_fields['bn_scripts'] as $key => $value ) {
$script = explode(',', $value);
$deps = array();
foreach ( $script as $i => $v ) {
if ($i > 0) { // values after $script[0]
$deps[] = trim($v);
}
}
wp_enqueue_script('twentyfifteen_custom_field_' . $key, get_template_directory_uri() . '/js/' . $value, $deps, null, true);
}
}
}
add_action( 'wp_enqueue_scripts', 'twentyfifteen_scripts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment