Question

I have an array simmilar to this:

$scripts_to_load  = array(
    'css' => array(
        array(
            'name'=>'core-css',
            'path'=>get_bloginfo('stylesheet_url')
        ),
        array(
            'name'=>'media-query-css',
            'path'=>get_template_directory_uri() . '/assets/mediaquery.css'
        ), 
    ),
);

The above is how assets are stored.

I'd like to writer a function that takes type, css or js and a name, the type will find either css or js and then the name will be used to each for a key that matches the name key in one of the arrays, if it s found that array will be unset.

So what I have, is nothing accept: public function remove_asset($type, $name){}, so If I pass in remove_asset('css', 'media-query-css') the array should then look like:

$scripts_to_load  = array(
    'css' => array(
        array(
            'name'=>'core-css',
            'path'=>get_bloginfo('stylesheet_url')
        ),
    ),
);

I am just not sure how to do that ... I can find the $type just fine, but its finding the $name that's giving me troubles and then unsetting that array.

Was it helpful?

Solution

This function does what you need:

function remove_asset($type, $name, &$scripts_to_load) {
    if( isset($scripts_to_load[$type] ) ) {
        foreach( $scripts_to_load[$type] as $key => $value ) {
            if( !empty( $value['name'] ) && $value['name'] == $name ) {
                unset( $scripts_to_load[$type][$key] );
            }
        }
    }
}

There is an control run:

$scripts_to_load  = array(
    'css' => array(
        array(
            'name'=>'core-css',
            'path'=>'stylesheet_url'
        ),
        array(
            'name'=>'media-query-css',
            'path'=>'/assets/mediaquery.css'
        ), 
    ),
);

print_r( $scripts_to_load );
echo "\n";

remove_asset('css', 'media-query-css', $scripts_to_load);

print_r( $scripts_to_load );
echo "\n";

OTHER TIPS

Try this (assuming assets are stored in object property):

public function remove_asset($type, $name){
    if(isset($this->assets[$type][$name])) {
        unset($this->assets[$type][$name]);
    }
}

This should remove the asset by given type and name.

Oh, and of course, the array of assets should look like this:

$this->assets = array(
    'css' => array(
        'css_name1' => 'path_to_css_1',
        'css_name2' => 'path_to_css_2',
    ),
    'js' => array(
        'js_name1' => 'path_to_js_1',
        'js_name2' => 'path_to_js_2',
    )
);

Now, if You call:

$this->remove_asset('css', 'css_name2');
$this->remove_asset('js', 'js_name1');

you should end up with:

$this->assets = array(
    'css' => array(
        'css_name1' => 'path_to_css_1',
    ),
    'js' => array(
        'js_name2' => 'path_to_js_2',
    )
);

EDIT: basically when working with asset properties You want more control to be stored, not only the path, so my suggested (end) array should be:

$this->assets = array(
    'css' => array(
        'css_name1' => array(
            'src' => 'path_to_css_1',
            'media' => 'screen',
            'type' => 'text/css',
        ),
        'css_name2' => array(
            'src' => 'path_to_css_2',
            'media' => 'print',
            'type' => 'text/css',
        ),
    ),
    'js' => array(
        'js_name1' => array(
            'src' => 'path_to_js_1',
            'type' => 'text/javascript',
        ),
    ),
    'icon' => array(
        'favicon' => array(
            'href' => 'path_to_favicon',
            'rel' => 'shortcut icon',
        ),
    'meta' => array(/* ... */)
);

And the remove_asset function will still work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top