WordPress小教程在文章中插入HTML/PHP代碼。如果不使用插件的話,在文章中和評論中是無法插入一些 PHP、CSS、HTML、JS 等代碼的,就算使用了 code 或 pre 標簽也不行,因為 WordPress 自身有一個強大的 HTML 標簽過濾系統,你插入的 html 或者 PHP 標簽會直接消失;
將下面的代碼加入主題的 functions.php 中,然后使用 code 或者 pre 標簽來插入代碼即可:
// 在文章和評論中插入代碼add_filter('pre_comment_content', 'lxtx_encode_code_in_posts_comments');add_filter('the_content', 'lxtx_encode_code_in_posts_comments');function lxtx_encode_code_in_posts_comments($source) { $encoded = preg_replace_callback('/<code>(.*?)<//code>/ims', create_function( '$matches', '$matches[1] = preg_replace( array("/^[/r|/n]+/i", "/[/r|/n]+$/i"), "", $matches[1]); return "<code>" . esc_html( $matches[1] ) . "</code>";' ), $source); if ($encoded) return $encoded; else return $source;}以上就是WordPress小教程在文章中插入HTML/PHP代碼的全部內容,希望對大家的學習和解決疑問有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答
圖片精選