1

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.

2 Answers 2

2

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"; 
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

this is completely wrong. the array I showed you is how the assets are stored. its the array attached to $scripts_to_load Its is not $key=>array, it is $key=>array(array(), array()). where $key is $type and $name is one of the keys in the array() of arrays(). please see the OP.
I am recommending to rework the assets array... In this case the removing/adding of assets is much easier and You need less loops when trying to load the assets...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.