WordPress 使用 iframe 嵌入Github Gist代码

时间:2019-08-29   阅读:605

在网上看到在Gists地址后边加上pibb就可以生成调用的Html,刚好In笔记需要调用一些Gist的代码,就写了下边的代码。

在主题functions.php加入代码:

1
2
3
4
/**
 * WordPress 使用 iframe 嵌入Github Gist代码
 * https://www.wpdaxue.com/wp-iframe-handler-gist.html
 */function wp_iframe_handler_gist( $matches, $attr, $url, $rawattr ) {$iframe = '<iframe width="100%" height="300" src="https://gist.github.com/'. esc_attr($matches[1]) .'/'. esc_attr($matches[2]) . '.pibb" frameborder=0 ></iframe>';return apply_filters( 'iframe_gist', $iframe, $matches, $attr, $url, $rawattr );}wp_embed_register_handler( 'gist_iframe', '#https://gist.github.com/(.*?)/([\d]+)#i', 'wp_iframe_handler_gist' );

/** * WordPress 使用 iframe 嵌入Github Gist代码 * https://www.wpdaxue.com/wp-iframe-handler-gist.html */ function wp_iframe_handler_gist( $matches, $attr, $url, $rawattr ) { $iframe = '<iframe width="100%" height="300" src="https://gist.github.com/'. esc_attr($matches[1]) .'/'. esc_attr($matches[2]) . '.pibb" frameborder=0 ></iframe>'; return apply_filters( 'iframe_gist', $iframe, $matches, $attr, $url, $rawattr ); } wp_embed_register_handler( 'gist_iframe', '#https://gist.github.com/(.*?)/([\d]+)#i', 'wp_iframe_handler_gist' );

使用的时候,只需要单独起一行,然后直接插入对应的地址即可,比如:

https://gist.github.com/ccbikai/11216526

上一篇:为你的 WordPress 站点添加”历史上的今天”功能

下一篇:WordPress 在文章内容中间插入广告

网友评论