elegantthemes / Divi-Beta

8 stars 0 forks source link

Divi Core - Enqueuing Child Theme Styles :: Adjust re-queue/replace methods to better handle conditional enqueuing in child themes #64

Open DigitalServicesLab opened 6 months ago

DigitalServicesLab commented 6 months ago

et_requeue_child_theme_styles() (core/functions line 871) : fetches all registered styles in child theme directories et_core_replace_enqueued_style() (core/functions line 967) : deregisters, dequeues and then enqueues these styles

So you cannot conditionally enqueue styles using wp_register_style() and later/somewhere else wp_enqueue_style()as they are forcibly enqueued by Divi whether you want them to be or not.

Divi does this with a huge and undocumented priority so users can't easily work around this unless they happen to try a priority higher than 99999999 which is highly unlikely.

add_action( 'wp_enqueue_scripts', 'et_requeue_child_theme_styles', 99999999 ); (core/functions line 493)

This will not work....

/**
 * Enqueue scripts and styles for services post type
 * Example only - the wp_enqueue_style() call could be in a shortcode or some other location
 * 
 * @return void
 */
function dsl_services_enqueue_scripts() {

    wp_register_style( 'services-css', get_stylesheet_directory_uri() . '/css/services-styles.css', array(), '1.00', 'all' );
    if ( is_single()  && 'service' == get_post_type() ) {
        wp_enqueue_style('services-css');
    } 
}
add_action( 'wp_enqueue_scripts', 'dsl_services_enqueue_scripts' );

This process should be reworked.

Alternative approaches:

  1. only look in the root directory of the child theme - and document that this happens
  2. only dequeue/requeue styles that are already enqueued, not just registered styles - and document that this happens
  3. reduce the priority of this method to something sensible - and document that this happens