wordpress 4.2版本后恢复自定义表情和评论表情的方法
wordpress更新到4.2后,程序自带了Emoji表情,但发现自定义表情都不能正常显示
自定义函数也不能显示表情,一下无能为力了。
后来终于找到了解决的方法,就是重新定义函数。
把下面的代码直接扔进functions.php即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | < ?php remove_action( 'wp_head','print_emoji_detection_script',7); //解决4.2版本部分主题大量404请求问题 remove_action('admin_print_scripts', 'print_emoji_detection_script'); //解决后台404请求 remove_action( 'init', 'smilies_init', 5); //移除4.2版本表情钩子 remove_action( 'wp_print_styles', 'print_emoji_styles' ); //移除4.2版本前台表情样式钩子 remove_action( 'admin_print_styles', 'print_emoji_styles'); //移除4.2版本后台表情样式钩子 remove_action( 'the_content_feed', 'wp_staticize_emoji'); //移除4.2 emoji相关钩子 remove_action( 'comment_text_rss', 'wp_staticize_emoji'); //移除4.2 emoji相关钩子 remove_action( 'comment_text', 'convert_smilies', 20 ); //移除4.2 表情相关钩子 remove_action( 'the_content', 'convert_smilies' ); //移除4.2 表情相关钩子 remove_action( 'the_excerpt', 'convert_smilies' ); //移除4.2 表情相关钩子 add_action( 'comment_text', 'convert_smilies_diy', 20); //自定义表情相关钩子 add_action( 'the_content', 'convert_smilies_diy' ); //自定义表情相关钩子 add_action( 'the_excerpt', 'convert_smilies_diy' ); //自定义表情相关钩子 add_action( 'init', 'smilies_init_old', 5 ); //自定义表情钩子 //原函数 smilies_init 位于wp-includes/functions.php function smilies_init_old() { global $wpsmiliestrans, $wp_smiliessearch; // don't bother setting up smilies if they are disabled if ( !get_option( 'use_smilies' ) ) return; if ( !isset( $wpsmiliestrans ) ) { $wpsmiliestrans = array( ':mrgreen:' => 'icon_mrgreen.gif', ':neutral:' => 'icon_neutral.gif', ':twisted:' => 'icon_twisted.gif', ':arrow:' => 'icon_arrow.gif', ':shock:' => 'icon_eek.gif', ':smile:' => 'icon_smile.gif', ':???:' => 'icon_confused.gif', ':cool:' => 'icon_cool.gif', ':evil:' => 'icon_evil.gif', ':grin:' => 'icon_biggrin.gif', ':idea:' => 'icon_idea.gif', ':oops:' => 'icon_redface.gif', ':razz:' => 'icon_razz.gif', ':roll:' => 'icon_rolleyes.gif', ':wink:' => 'icon_wink.gif', ':cry:' => 'icon_cry.gif', ':eek:' => 'icon_surprised.gif', ':lol:' => 'icon_lol.gif', ':mad:' => 'icon_mad.gif', ':sad:' => 'icon_sad.gif', '8-)' => 'icon_cool.gif', '8-O' => 'icon_eek.gif', ':-(' => 'icon_sad.gif', ':-)' => 'icon_smile.gif', ':-?' => 'icon_confused.gif', ':-D' => 'icon_biggrin.gif', ':-P' => 'icon_razz.gif', ':-o' => 'icon_surprised.gif', ':-x' => 'icon_mad.gif', ':-|' => 'icon_neutral.gif', ';-)' => 'icon_wink.gif', // This one transformation breaks regular text with frequency. // '8)' => 'icon_cool.gif', '8O' => 'icon_eek.gif', ':(' => 'icon_sad.gif', ':)' => 'icon_smile.gif', ':?' => 'icon_confused.gif', ':D' => 'icon_biggrin.gif', ':P' => 'icon_razz.gif', ':o' => 'icon_surprised.gif', ':x' => 'icon_mad.gif', ':|' => 'icon_neutral.gif', ';)' => 'icon_wink.gif', ':!:' => 'icon_exclaim.gif', ':?:' => 'icon_question.gif', ); } if (count($wpsmiliestrans) == 0) { return; } /* * NOTE: we sort the smilies in reverse key order. This is to make sure * we match the longest possible smilie (:???: vs :?) as the regular * expression used below is first-match */ krsort($wpsmiliestrans); $spaces = wp_spaces_regexp(); // Begin first "subpattern" $wp_smiliessearch = '/(?< =' . $spaces . '|^)'; $subchar = ''; foreach ( (array) $wpsmiliestrans as $smiley => $img ) { $firstchar = substr($smiley, 0, 1); $rest = substr($smiley, 1); // new subpattern? if ($firstchar != $subchar) { if ($subchar != '') { $wp_smiliessearch .= ')(?=' . $spaces . '|$)'; // End previous "subpattern" $wp_smiliessearch .= '|(?< =' . $spaces . '|^)'; // Begin another "subpattern" } $subchar = $firstchar; $wp_smiliessearch .= preg_quote($firstchar, '/') . '(?:'; } else { $wp_smiliessearch .= '|'; } $wp_smiliessearch .= preg_quote($rest, '/'); } $wp_smiliessearch .= ')(?=' . $spaces . '|$)/m'; } //原函数 convert_smilies 位于wp-includes/formatting.php function convert_smilies_diy( $text ) { global $wp_smiliessearch; $output = ''; if ( get_option( 'use_smilies' ) && ! empty( $wp_smiliessearch ) ) { // HTML loop taken from texturize function, could possible be consolidated $textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // capture the tags as well as in between $stop = count( $textarr );// loop stuff // Ignore proessing of specific tags $tags_to_ignore = 'code|pre|style|script|textarea'; $ignore_block_element = ''; for ( $i = 0; $i < $stop; $i++ ) { $content = $textarr[$i]; // If we're in an ignore block, wait until we find its closing tag if ( '' == $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')>/', $content, $matches ) ) { $ignore_block_element = $matches[1]; } // If it's not a tag and not in ignore block if ( '' == $ignore_block_element && strlen( $content ) > 0 && '< ' != $content[0] ) { $content = preg_replace_callback( $wp_smiliessearch, 'translate_smiley_diy', $content ); } // did we exit ignore block if ( '' != $ignore_block_element && '</' . $ignore_block_element . '>' == $content ) { $ignore_block_element = ''; } $output .= $content; } } else { // return default text. $output = $text; } return $output; } //原函数 translate_smiley 位于wp-includes/formatting.php function translate_smiley_diy( $matches ) { global $wpsmiliestrans; if ( count( $matches ) == 0 ) return ''; $smiley = trim( reset( $matches ) ); $img = $wpsmiliestrans[ $smiley ]; $matches = array(); $ext = preg_match( '/\.([^.]+)$/', $img, $matches ) ? strtolower( $matches[1] ) : false; $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' ); // Don't convert smilies that aren't images - they're probably emoji. if ( ! in_array( $ext, $image_exts ) ) { return $img; } /** * Filter the Smiley image URL before it's used in the image element. * * @since 2.9.0 * * @param string $smiley_url URL for the smiley image. * @param string $img Filename for the smiley image. * @param string $site_url Site URL, as returned by site_url(). */ //请注意!已将表情路径定义到主题目录下的 images/smilies 文件夹 $src_url = apply_filters( 'smilies_src', get_bloginfo('template_directory').'/images/smilies/'.$img, $img, site_url() ); return sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="/*height: 1em; max-height: 1em;*/" />', esc_url( $src_url ), esc_attr( $smiley ) ); } ?> |
很多同学不明白最后这个函数是干什么用的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | //原函数 translate_smiley 位于wp-includes/formatting.php function translate_smiley_diy( $matches ) { global $wpsmiliestrans; if ( count( $matches ) == 0 ) return ''; $smiley = trim( reset( $matches ) ); $img = $wpsmiliestrans[ $smiley ]; $matches = array(); $ext = preg_match( '/\.([^.]+)$/', $img, $matches ) ? strtolower( $matches[1] ) : false; $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' ); // Don't convert smilies that aren't images - they're probably emoji. if ( ! in_array( $ext, $image_exts ) ) { return $img; } /** * Filter the Smiley image URL before it's used in the image element. * * @since 2.9.0 * * @param string $smiley_url URL for the smiley image. * @param string $img Filename for the smiley image. * @param string $site_url Site URL, as returned by site_url(). */ //请注意!已将表情路径定义到主题目录下的 images/smilies 文件夹 $src_url = apply_filters( 'smilies_src', get_bloginfo('template_directory').'/images/smilies/'.$img, $img, site_url() ); return sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="/*height: 1em; max-height: 1em;*/" />', esc_url( $src_url ), esc_attr( $smiley ) ); } |
因为明凯博客的右侧栏调用了最新评论小插件,默认小插件不是会显示表情,然后我把自定义的表情也显出出来了。
发现上面的钩子,不管怎么改都不会都小插件的表情起作用,于是我就在想,到底是哪里出了错误呢。
根本没法替换嘛,最后我找到我写的插件小工具上面发现我是这么调用的 :
1 2 3 4 | foreach ( $comments as $comment ) { $output .= "<li>" . get_avatar($comment->comment_author_email,32) ."<div class='rcomment'><strong>".$comment->comment_author."</strong> " .human_time_diff(strtotime($comment->comment_date), current_time('timestamp'))."前 在 <a href='". get_comment_link($comment->comment_ID) . "' title=' " . $comment->post_title . "'> " . $comment->post_title . "</a> 评论:<div class='box'><span class='jt'>◆<span class='jt2'>◆</span></span>". convert_smilies($comment->comment_content)."</div></div></li>"; } echo $output; |
我这里调用的是convert_smilies,我查看了原函数,发现在钩子发生之前,就已经替换了,所以我们要改成这样子:
1 2 3 4 | foreach ( $comments as $comment ) { $output .= "<li>" . get_avatar($comment->comment_author_email,32) ."<div class='rcomment'><strong>".$comment->comment_author."</strong> " .human_time_diff(strtotime($comment->comment_date), current_time('timestamp'))."前 在 <a href='". get_comment_link($comment->comment_ID) . "' title=' " . $comment->post_title . "'> " . $comment->post_title . "</a> 评论:<div class='box'><span class='jt'>◆<span class='jt2'>◆</span></span>". convert_smilies_diy($comment->comment_content)."</div></div></li>"; } echo $output; |
就是把convert_smilies改成convert_smilies_diy,就万事大吉了。
织梦列表页调用所有顶级栏目文章的方法 dedecms实现文章列表隔行换色和随机颜色的方法