LoginSignup
0
0

More than 5 years have passed since last update.

#WordPress enqueue scripts/styles by custom fields

Posted at
customfield_scripts_styles.php
<?php
/*
Plugin Name: Customfield Add Header
Author: Takuro Hishikawa
Version: 0.1
*/

function customfield_scripts_styles(){
    if ( is_singular() ) {
        $post = get_post();
        if ( is_a( $post, 'WP_Post' ) ) {

            $custom_scripts = get_post_meta( $post->ID, 'custom_scripts', false );
            $queue_id = 0;
            foreach ( (array) $custom_scripts as $custom_script ) {
                wp_enqueue_script( 'custom-script-'.$post->ID.'-'.$queue_id, $custom_script );
                $queue_id++;
            }

            $custom_styles = get_post_meta( $post->ID, 'custom_styles', false );
            $queue_id = 0;
            foreach ( (array) $custom_styles as $custom_style ) {
                wp_enqueue_style( 'custom-style-'.$post->ID.'-'.$queue_id, $custom_style );
                $queue_id++;
            }
        }
    }
}
add_action( 'wp_enqueue_scripts', 'customfield_scripts_styles' );
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0