Last active
March 3, 2019 12:25
filters an enqueued script tag on WordPress (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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @summary filtra uma tag de script na fila e adiciona um elemento noscript após ela | |
* | |
* @description filtra uma tag de script na fila (identificada pela variável $handle) e adiciona | |
* um elemento noscript após ela. Se também houver um script inline após o script | |
* identificado por $handle, adiciona o elemento noscript após ele. | |
* | |
* @access público | |
* @param string $tag A string da tag enviada pelo filtro `script_loader_tag` em WP_Scripts::do_item | |
* @param string $handle O nome (handle) do como enviado pelo filtro `script_loader_tag` em WP_Scripts::do_item | |
* @param string $src A origem (src) do script como enviado pelo filtro `script_loader_tag` em WP_Scripts::do_item | |
* @return string $tag A variável $tag filtrada e com o elemento `noscript` | |
*/ | |
function add_noscript_filter($tag, $handle, $src){ | |
// como este filtro roda para cada script na fila | |
// precisamos verificar se o $handle é igual o script | |
// que queremos filtrar e adicionar o noscript a ele | |
if ( 'script-handle' === $handle ){ | |
$noscript = '<noscript>'; | |
// você poderia pegar o conteúdo interno de outra função | |
$noscript .= '<p>Este site demanda JavaScript</p>'; | |
$noscript .= '</noscript>'; | |
$tag = $tag . $noscript; | |
} | |
return $tag; | |
} | |
// adiciona a função add_noscript_filter aos filtros em script_loader_tag | |
// deve ysar 3 no último parmetro para garantirque $tag, $handle, $src | |
// estejam disponíveis à funço add_noscript_filter | |
add_filter('script_loader_tag', 'add_noscript_filter', 10, 3); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 ( 'script-handle' === $handle ){ | |
$noscript = '<noscript>'; | |
// you could get the inner content from other function | |
$noscript .= '<p>this site demands javascript</p>'; | |
$noscript .= '</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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @summary filters an enqueued style tag and adds a noscript element after it | |
* | |
* @description filters an enqueued style tag (identified by the $handle variable) and | |
* adds a noscript element after it. | |
* | |
* @access public | |
* @param string $tag The tag string sent by `style_loader_tag` filter on WP_Styles::do_item | |
* @param string $handle The script handle as sent by `script_loader_tag` filter on WP_Styles::do_item | |
* @param string $href The style tag href parameter as sent by `script_loader_tag` filter on WP_Styles::do_item | |
* @param string $media The style tag media parameter as sent by `script_loader_tag` filter on WP_Styles::do_item | |
* @return string $tag The filter $tag variable with the noscript element | |
*/ | |
function add_noscript_style_filter($tag, $handle, $href, $media){ | |
// 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 ( 'script-handle' === $handle ){ | |
$noscript = '<noscript>'; | |
// you could get the inner content from other function | |
$noscript .= '<p>this site demands javascript</p>'; | |
$noscript .= '</noscript>'; | |
$tag = $tag . $noscript; | |
} | |
return $tag; | |
} | |
// adds the add_noscript_filter function to the style_loader_tag filters | |
// it must use 4 as the last parameter to make $tag, $handle, $href, $media available | |
// to the filter function | |
add_filter('style_loader_tag', 'add_noscript_style_filter', 10, 4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment