How to Use SG Optimizer’s Filters (Procedure)
-
Dear Community,
As a courtesy, below please find a procedure for using the SG Optimizer (SGO) filters provided here. Procedure has been tested and works like a charm.
Specifically, procedure provided below applies to the SGO filters that call out the following variables or attributes:
$exclude_list[] = 'style-handle'; $exclude_list[] = 'style-handle-2';
and
$exclude_list[] = 'script-handle'; $exclude_list[] = 'script-handle-2';
Procedure:
(1) Install and activate the plugin “Code Snippets.” This plugin will allow you to use SGO’s filters without the need to modify your functions.php file directly. It will also allow you to add “snippets” (php code) to your website in a very simple way.
(2) After activating “Code Snippets,” add and activate the following snippet:
<?php add_filter( 'script_loader_tag', 'cameronjonesweb_add_script_handle', 10, 3 ); function cameronjonesweb_add_script_handle( $tag, $handle, $src ) { return str_replace( '<script', sprintf( '<script data-handle="%1$s"', esc_attr( $handle ) ), $tag ); }
Above snippet obtained here.
(3) Once activated, the above snippet will generate a ‘data-handle’ (i.e., style or script handle) attribute to all of your enqueued style (css) and script (js) files.
(4) Add the needed SGO filter (snippet) to “Code Snippets.” For example, if you need to exclude a certain script (JS file) from minification, add the following SGO snippet:
<?php add_filter( 'sgo_js_minify_exclude', 'js_minify_exclude' ); function js_minify_exclude( $exclude_list ) { $exclude_list[] = 'script-handle'; $exclude_list[] = 'script-handle-2'; return $exclude_list; }
Note: Do not activate the above snippet at this point.
(5) Go to the affected URL page of your website containing the style or script that needs to be excluded from minification or combination.
(6) After the affected URL page has been loaded, right-click on the page (any location) and select “Inspect Element.” Under “Styles,” enter and search for “handle” or “data-handle.” Click here. You will then see a list of all styles and scripts with a “data-handle.” The “data-handles” presented are equivalent to the “style-handle” or “script-handle” noted in SGO’s filters. Click here for an example of what the results looks like.
(7) Once you identify the affected ‘data-handle'(s) enter it/them into the above snippet (e.g., replace ‘script-handle’ with the ‘data-handle’ name you identified). In our case (example), replace ‘script-handle’ with “tribe-events-dynamic’
(8) After you’ve added all affected ‘data-handles'(s) to SGO’s filter or snippet, save and activate it. Your affected files are now excluded from minification or combination.
Note: It’s perfectly OK to deactivate or delete the ‘data-handle’ snippet presented in Step (2) above after completing Step (8) above.
Comments always welcome. Did the above work for you?
Cheers!
Notice: I do not work for (nor represent) SiteGround nor any affiliates.
- The topic ‘How to Use SG Optimizer’s Filters (Procedure)’ is closed to new replies.