Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function request: queue_script/style 'local' parameter
#1
I keep having trouble due to plugins not loading assets only for their page.
This is because the queue_script and queue_style functions by default load the scripts on any page (which is good, because some plugins offer cross-tab/ front-end functionality). Ideally all plugin devs would be conscious of this, but as that isn't the case, I'd like to request either a new parameter

Consequences are that JS scripts will throw errors, and CSS from plugins messes up CSS from other plugins/ pages.
For example, the excellent ItemManager plugin loads a jquery-ui theme, and messes up the lay-out of the GS Custom Settings datepicker (look at the icons in the orange bar): 

[Image: iXbQCZ4.png]

The simple solution is to modify the queue_ functions in plugin_functions.php:

PHP Code:
<?php 
function queue_style($handle,$where=1,$localID=NULL){
  global $GS_styles$id;
 
 if (array_key_exists($handle$GS_styles)){
 
    if (!$localID || (($id === $localID) || (is_array($localID) && array_search($id$localID) !== FALSE))) { 
        $GS_styles
[$handle]['load']=true;
 
       $GS_styles[$handle]['where']=$GS_styles[$handle]['where'] | $where;
 
    }
  }
?>


With the above solution, one could queue scripts/styles as follows:
PHP Code:
<?php 
  queue_style
('jqui'GSBACK);                 // load on all backend pages
  
queue_style('jqui'GSBACK'imanager');     // only load on backend page with id === 'imanager'
  
$pages = array('imanager','custom_settings');// load on all backend pages with id's in the array
  
queue_style('jqui'GSBACK$pages); 
?>

Alternatively, the $where parameter could be modified to hold id string/ array as well; eg:

PHP Code:
function get_styles_backend(){
    global 
$GS_styles$id;
    foreach (
$GS_styles as $style){
        if ((
$style['where'] && GSBACK && is_numeric($style['where'])) || (($style['where'] === $id) || (is_array($style['where']) && array_search($id$style['where']) !== FALSE))) {
                if (
$style['load']==TRUE){
                    echo 
"\t".'<link href="'.$style['src'].'?v='.$style['ver'].'" rel="stylesheet" media="'.$style['media']."\">\n";
                }
        }
    }



Usage as follows:
PHP Code:
<?php 
  queue_style
('jqui'GSBACK);                 // load on all backend pages
  
queue_style('jqui''imanager');     // only load on backend page with id === 'imanager'
  
$pages = array('imanager','custom_settings');// load on all backend pages with id's in the array
  
queue_style('jqui'$pages); 
?>
Reply


Messages In This Thread
Function request: queue_script/style 'local' parameter - by Tyblitz - 2015-10-28, 05:16:51



Users browsing this thread: 1 Guest(s)