Skip to content

Instantly share code, notes, and snippets.

@tatianepires
Last active November 3, 2023 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tatianepires/ac22e9a2faf60d59274349969aa36795 to your computer and use it in GitHub Desktop.
Save tatianepires/ac22e9a2faf60d59274349969aa36795 to your computer and use it in GitHub Desktop.
<?php
/**
* @summary filters an enqueued script tag and adds a noscript element after it
*
* @description filters an enqueued script tag (identified by the $handle variable) and
* adds a noscript element after it. If there is also an inline script enqueued
* after $handled, adds the noscript element after it.
*
* @access public
* @param string $tag The tag string sent by `script_loader_tag` filter on WP_Scripts::do_item
* @param string $handle The script handle as sent by `script_loader_tag` filter on WP_Scripts::do_item
* @param string $src The script src as sent by `script_loader_tag` filter on WP_Scripts::do_item
* @return string $tag The filter $tag variable with the noscript element
*/
function add_noscript_filter($tag, $handle, $src){
// as this filter will run for every enqueued script
// we need to check if the handle is equals the script
// we want to filter. If yes, than adds the noscript element
if ( 'handle' === $handle ){
$noscript = '<noscript>No script content.</noscript>';
$tag = $tag . $noscript;
}
return $tag;
}
// adds the add_noscript_filter function to the script_loader_tag filters
// it must use 3 as the last parameter to make $tag, $handle, $src available
// to the filter function
add_filter('script_loader_tag', 'add_noscript_filter', 10, 3);
/*
* Source: https://wordpress.stackexchange.com/questions/281587/does-wordpress-wp-enqueue-style-support-noscript
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment