How to modify/extend/override a core method?

This is my first post. In advance, thank you for welcoming me…

Context

When you enqueue style, you can output your css link into conditional comments.

global $wp_styles;
wp_enqueue_style("my_styles_ie");
$wp_styles->add_data("my_styles_ie", "conditional", "(lt IE 9) & (!IEMobile)");

It will produce the following code :

<!--[if (lt IE 9) & (!IEMobile)]>
    <link rel="stylesheet" href="#" />
<![endif]-->

This is done by the do_item() method of the WP_Styles class (class.wp_styles.php).

Goal

I’d like to add a “anticonditionnal” parameter with the condition value…

$wp_styles->add_data("my_styles_ie", "anticonditional", "(gt IE 8) | (IEMobile)");

to be able to output this (“nested comments”):

<!--[if (gt IE 8) | (IEMobile)]><!-->
    <link rel="stylesheet" href="#">
<!--<![endif]-->

I have searched about “how to modify core method” but I didn’t find any solution… 🙁

For now, I can do it with a hook but there is no “arguments”. Stylesheet and condition are hardcoded…

function antiConditionnal($tag, $handle) {
    if('my_styles' == $handle)
        $tag = '<!--[if (gt IE 8) | (IEMobile)]><!-->' . "\n" . $tag . '<!--<![endif]-->' . "\n";

    return $tag;
}
add_filter( 'style_loader_tag', 'antiConditionnal', 10, 2);

This is mainly for me an opportunity to improve my knowledge and dive deeper into WordPress!

Any idea?

Thank you…

tm

1 Answer
1

There’s already a simliar answer by toscho here. Based on this one and from a look at WP_Styles, which extends WP_Dependencies and _WP_Dependency, I can’t see a reason why it should not work:

Whatever got added as extraconditional, gets thrown in:

// ~/wp-includes/class.wp-styles.php
if ( isset($obj->extra['conditional']) && $obj->extra['conditional'] ) {
    $tag .= "<!--[if {$obj->extra['conditional']}]>\n";
    $end_cond = "<![endif]-->\n";
}

_WP_Dependency defines add_data() the following way:

function add_data( $name, $data ) {
    if ( !is_scalar($name) )
        return false;
    $this->extra[$name] = $data;
    return true;
}

and WP_Dependencies defines add_data() like this:

function add_data( $handle, $key, $value ) {
    if ( !isset( $this->registered[$handle] ) )
        return false;

    return $this->registered[$handle]->add_data( $key, $value );
}

Leave a Comment