0

Working on a child theme for site with a good deal of separate stylesheets being registered/enqueued from the theme and plugins. I'd like to change some attributes of the <link> tags in the final output, while avoiding conflicts after a parent theme update by the site owner.

The goal is to not re-register/re-enqueue the same .css files already set by the parent theme and/or plugins, while effectively passing a new set of arguments to the wp_register_style function.

For example, currently this is what the parent theme & plugins are doing:

// parent theme or plugin's php
wp_register_style( 'a_stylesheet', $uri . 'css/a-stylesheet.css', array(), $version, 'screen' );

// html output
<link rel='stylesheet' id='a_stylesheet-css'  href='http://domain.com/wp-content/themes/theme/css/a-stylesheet.css' type='text/css' media='screen' />

The goal is for the child theme to in effect change the wp_register_style call, like so:

// child theme's php
some_function_to_reregister( 'already_registered_file', $inherit, $inherit, $inherit, 'handheld' )
// if the above doesn't exist, do nothing; else, re-register with the new argument(s).

// html output
<link rel='stylesheet' id='a_stylesheet-css'  href='http://domain.com/wp-content/themes/theme/css/a-stylesheet.css' type='text/css' media='handheld' />

This changes the <link media> attribute to handheld instead of screen, but preserves everything else being done by the parent theme/plugin, and skips registering the file if it hasn't been registered yet elsewhere (i.e. after an update).

Seems like this would be a much more efficient approach than de-registering/de-enqueuing css from the parent theme and plugins only to copy/paste it back in the child theme and have unexpected wreckage in future updates.

1 Answer 1

0

I don't think there's a built-in safe way to do this, but you could directly modify the $wp_styles global after things are registered but before they're output, or at least use it to fetch the parameters a style was originally registered with.

global $wp_styles;
if( isset( $wp_styles->registered['a_stylesheet'] ) ){
    $wp_styles->registered['a_stylesheet']->args = 'handheld';
}

Not ideal, but it may work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.