Skip to Content
Frontlane Studio
All Snippets
PHP WordPress November 14, 2024

Dynamically Add Inline Styles Based on Custom Fields or Post Meta

A PHP snippet for Dynamically Add Inline Styles Based on Custom Fields or Post Meta.

WordPress PHP

A PHP snippet for Dynamically Add Inline Styles Based on Custom Fields or Post Meta.

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' );