wprig / docs

Documents and documentation for WP Rig
GNU General Public License v3.0
22 stars 6 forks source link

wp_add_inline_style and wp-rig-global-css #8

Open megphillips91 opened 3 years ago

megphillips91 commented 3 years ago

inc/Styles/Component.php

What is the recommended best practice for pulling in customizer styles with WP_Rig 2.0?

In WP_Rig 1.x, I built a function which referenced the theme mods and output the user determined global typography, color settings into the global styles file using wp_add_inline_style.

In Wp_Rig 2.0, I've tried to do the same within inc/Styles/Component.php, but it does not work. When I used this technique in WP_Rig 1.x it worked well. Maybe I am doing it wrong, but I didn't see anything in the documentation regarding the best practice to output css back into the wp-rig-global-css styles from the customizer.

WP add inline styles from manual

` public function action_enqueue_styles() { // Enqueue Google Fonts. $google_fonts_url = $this->get_google_fonts_url(); if ( ! empty( $google_fonts_url ) ) { wp_enqueue_style( 'wp-rig-fonts', $google_fonts_url, [], null ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion }

    $css_uri = get_theme_file_uri( '/assets/css/' );
    $css_dir = get_theme_file_path( '/assets/css/' );

    $preloading_styles_enabled = $this->preloading_styles_enabled();

    $css_files = $this->get_css_files();
    foreach ( $css_files as $handle => $data ) {
        $src     = $css_uri . $data['file'];
        $version = wp_rig()->get_asset_version( $css_dir . $data['file'] );

        /*
         * Enqueue global stylesheets immediately and register the other ones for later use
         * (unless preloading stylesheets is disabled, in which case stylesheets should be immediately
         * enqueued based on whether they are necessary for the page content).
         */
        if ( $data['global'] || ! $preloading_styles_enabled && is_callable( $data['preload_callback'] ) && call_user_func( $data['preload_callback'] ) ) {
            wp_enqueue_style( $handle, $src, [], $version, $data['media'] );
        } else {
            wp_register_style( $handle, $src, [], $version, $data['media'] );
        }
        wp_style_add_data( $handle, 'precache', true );
    }
    $customizations = wprig_return_customizer_styles();
    wp_add_inline_style('wp-rig-global-css', $customizations );
}

`

benoitchantre commented 3 years ago

Hello @megphillips91,

You can look at https://github.com/felixarntz/wprig-custom/