Dynamically Add Inline Styles Based on Custom Fields or Post Meta

Last updated 1 month ago

PHP
function add_inline_styles_based_on_meta() {
    if ( is_single() ) {
        global $post;
        $custom_style = get_post_meta( $post->ID, '_custom_css', true );

        if ( ! empty( $custom_style ) ) {
            wp_add_inline_style( 'theme-styles', $custom_style );
        }
    }
}
add_action( 'wp_enqueue_scripts', 'add_inline_styles_based_on_meta' );

All code snippets are licensed GPLv2 or later unless otherwise stated.