wordpress 4.2版本后恢复自定义表情和评论表情的方法
wordpress更新到4.2后,程序自带了Emoji表情,但发现自定义表情都不能正常显示
自定义函数也不能显示表情,一下无能为力了。
后来终于找到了解决的方法,就是重新定义函数。
把下面的代码直接扔进functions.php即可
< ?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( '
', esc_url( $src_url ), esc_attr( $smiley ) );
}
?>
很多同学不明白最后这个函数是干什么用的
//原函数 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( '
', esc_url( $src_url ), esc_attr( $smiley ) );
}
因为明凯博客的右侧栏调用了最新评论小插件,默认小插件不是会显示表情,然后我把自定义的表情也显出出来了。
发现上面的钩子,不管怎么改都不会都小插件的表情起作用,于是我就在想,到底是哪里出了错误呢。
根本没法替换嘛,最后我找到我写的插件小工具上面发现我是这么调用的 :
foreach ( $comments as $comment ) {
$output .= "".$comment->comment_author." " .human_time_diff(strtotime($comment->comment_date), current_time('timestamp'))."前 在 " . $comment->post_title . " 评论:
◆◆". convert_smilies($comment->comment_content)."
我这里调用的是convert_smilies,我查看了原函数,发现在钩子发生之前,就已经替换了,所以我们要改成这样子:
foreach ( $comments as $comment ) {
$output .= "".$comment->comment_author." " .human_time_diff(strtotime($comment->comment_date), current_time('timestamp'))."前 在 " . $comment->post_title . " 评论:
◆◆". convert_smilies_diy($comment->comment_content)."
就是把convert_smilies改成convert_smilies_diy,就万事大吉了。
织梦列表页调用所有顶级栏目文章的方法 dedecms实现文章列表隔行换色和随机颜色的方法