Se você deseja detectar links para Github Gist em posts e substituí-los por shortcodes, então os shortcodes serão substituídos por gists incorporados.
Instruções: Adicione o seguinte código ao functions.php do seu tema WordPress.
<?php
// [gist id="ID" file="FILE"]
function gist_shortcode($atts) {
return sprintf(
'<script src="https://gist.github.com/%s.js%s"></script>',
$atts['id'],
$atts['file'] ? '?file=' . $atts['file'] : ''
);
} add_shortcode('gist','gist_shortcode');
// Remove this function if you don't want autoreplace gist links to shortcodes
function gist_shortcode_filter($content) {
return preg_replace('/https://gist.github.com/([d]+)[.js?]*[#]*file[=|_]+([w.]+)(?![^<]*</a>)/i', '[gist id="${1}" file="${2}"]', $content );
} add_filter( 'the_content', 'gist_shortcode_filter', 9);
?>
Formatos:
https://gist.github.com/1147076
https://gist.github.com/1147076#file_annotated.js
https://gist.github.com/1147076.js?file=annotated.js
[gist id=1147076]
[gist id=1147076 file=annotated.js]
Funciona perfeitamente na maioria dos casos, mas há um (ou mais?) caso em que não funciona como deveria:
https://gist.github.com/1147076#file_license.txt
Se adicionarmos este URL ao nosso post, ele exibirá todos os arquivos deste gist. Isso ocorre porque o nome do arquivo no gist está em maiúsculas. Este funcionará corretamente:
https://gist.github.com/1147076.js?file=LICENSE.txt
Você também pode gostar de verificar se o shortcode já existe.
Comentários Deixe uma Resposta