Answers:
内容表情符号转换为:
add_filter( 'the_content', 'convert_smilies' );
其中,这部分的的convert_smilies()
作用是非常重要的:
$content = preg_replace_callback( $wp_smiliessearch, 'translate_smiley', $content );
如果我们窥视一下,translate_smiley()
就会发现以下内容:
// Don't convert smilies that aren't images - they're probably emoji.
if ( ! in_array( $ext, $image_exts ) ) {
return $img;
}
在smilies_src
应用过滤器之前。
因此,在:)
笑脸的情况下,此过滤器不可用。
我们将表情符号初始化为:
add_action( 'init', 'smilies_init', 5 );
在功能说明中,smilies_init()
我们可以阅读以下内容:
插件可以通过将设置
$wpsmiliestrans
为一个数组来覆盖默认的笑脸列表,并使用博主输入的代码和图像文件的值作为键。
这是全局$wpsmiliestrans
数组:
$wpsmiliestrans = array(
':mrgreen:' => 'mrgreen.png',
':neutral:' => "\xf0\x9f\x98\x90",
':twisted:' => "\xf0\x9f\x98\x88",
':arrow:' => "\xe2\x9e\xa1",
':shock:' => "\xf0\x9f\x98\xaf",
':smile:' => "\xf0\x9f\x99\x82",
':???:' => "\xf0\x9f\x98\x95",
':cool:' => "\xf0\x9f\x98\x8e",
':evil:' => "\xf0\x9f\x91\xbf",
':grin:' => "\xf0\x9f\x98\x80",
':idea:' => "\xf0\x9f\x92\xa1",
':oops:' => "\xf0\x9f\x98\xb3",
':razz:' => "\xf0\x9f\x98\x9b",
':roll:' => 'rolleyes.png',
':wink:' => "\xf0\x9f\x98\x89",
':cry:' => "\xf0\x9f\x98\xa5",
':eek:' => "\xf0\x9f\x98\xae",
':lol:' => "\xf0\x9f\x98\x86",
':mad:' => "\xf0\x9f\x98\xa1",
':sad:' => "\xf0\x9f\x99\x81",
'8-)' => "\xf0\x9f\x98\x8e",
'8-O' => "\xf0\x9f\x98\xaf",
':-(' => "\xf0\x9f\x99\x81",
':-)' => "\xf0\x9f\x99\x82",
':-?' => "\xf0\x9f\x98\x95",
':-D' => "\xf0\x9f\x98\x80",
':-P' => "\xf0\x9f\x98\x9b",
':-o' => "\xf0\x9f\x98\xae",
':-x' => "\xf0\x9f\x98\xa1",
':-|' => "\xf0\x9f\x98\x90",
';-)' => "\xf0\x9f\x98\x89",
// This one transformation breaks regular text with frequency.
// '8)' => "\xf0\x9f\x98\x8e",
'8O' => "\xf0\x9f\x98\xaf",
':(' => "\xf0\x9f\x99\x81",
':)' => "\xf0\x9f\x99\x82",
':?' => "\xf0\x9f\x98\x95",
':D' => "\xf0\x9f\x98\x80",
':P' => "\xf0\x9f\x98\x9b",
':o' => "\xf0\x9f\x98\xae",
':x' => "\xf0\x9f\x98\xa1",
':|' => "\xf0\x9f\x98\x90",
';)' => "\xf0\x9f\x98\x89",
':!:' => "\xe2\x9d\x97",
':?:' => "\xe2\x9d\x93",
);
或更好的分类显示:
Array
(
[;-)] => 😉
[;)] => 😉
[:|] => 😐
[:x] => 😡
[:wink:] => 😉
[:twisted:] => 😈
[:smile:] => 🙂
[:shock:] => 😯
[:sad:] => 🙁
[:roll:] => rolleyes.png
[:razz:] => 😛
[:oops:] => 😳
[:o] => 😮
[:neutral:] => 😐
[:mrgreen:] => mrgreen.png
[:mad:] => 😡
[:lol:] => 😆
[:idea:] => 💡
[:grin:] => 😀
[:evil:] => 👿
[:eek:] => 😮
[:cry:] => 😥
[:cool:] => 😎
[:arrow:] => ➡
[:P] => 😛
[:D] => 😀
[:???:] => 😕
[:?:] => ❓
[:?] => 😕
[:-|] => 😐
[:-x] => 😡
[:-o] => 😮
[:-P] => 😛
[:-D] => 😀
[:-?] => 😕
[:-)] => 🙂
[:-(] => 🙁
[:)] => 🙂
[:(] => 🙁
[:!:] => ❗
[8O] => 😯
[8-O] => 😯
[8-)] => 😎
)
因此,如果我正确理解了以上核心评论,那么我们可以执行以下操作:
/**
* :) as the cool emoji
*/
add_action( 'init', function() use ( &$wpsmiliestrans )
{
if( is_array( $wpsmiliestrans ) && get_option( 'use_smilies' ) )
$wpsmiliestrans[':)'] = $wpsmiliestrans[':cool:'];
}, 6 );
但这仅适用于预定义的笑脸键,$wp_smiliessearch
才能起作用。
但是我不喜欢这种建议的方法,修改全局数组!希望还有一个更好的!
我试图为此提出一个申请。我不确定是否已经存在,但是这里是:
<?php
/**
* Plugin Name: Santa's Smile In December
* Description: Change the emoji of :) to the Santa Claus emoji, but only in December
* Plugin URI: https://wordpress.stackexchange.com/a/218496/26350
*/
add_action( 'init', function() use ( &$wpsmiliestrans )
{
// :) as Santa Claus
if(
is_array( $wpsmiliestrans )
&& get_option( 'use_smilies' )
&& 12 == current_time( 'n' )
)
$wpsmiliestrans[':)'] = "\xF0\x9F\x8E\x85";
}, 6 );
感谢Ismael Miguel的全球评论,我相应地重写了这些摘录。
这里是新创建的车票#35905由彼得·古森,对于一个新的smilies_trans
过滤器。
新的过滤器将在WordPress 4.7+中可用,但名称 smilies
不是smilies_trans
。
我们上面的例子可以写成:
add_filter( 'smilies', function( $smilies )
{
if( isset( $smilies[':cool:'] ) )
$smilies[':)'] = $smilies[':cool:'];
return $smilies;
} );
或明确地与:
add_filter( 'smilies', function( $smilies )
{
$smilies[':)'] = "\xf0\x9f\x98\x8e";
return $smilies;
} );
该演示插件变为:
<?php
/**
* Plugin Name: Santa's Smile In December
* Description: Change the emoji of :) to the Santa Claus emoji, but only in December
* Plugin URI: https://wordpress.stackexchange.com/a/218496/26350
*/
add_filter( 'smilies', function( $smilies )
{
// :) as Santa Claus
if( get_option( 'use_smilies' ) && 12 == current_time( 'n' ) )
$smilies[':)'] = "\xF0\x9F\x8E\x85";
return $smilies;
} );
我们不再需要弄乱全局$wpsmiliestrans
数组了!
add_action( 'init', function() use (&$wpsmiliestrans){ $wpsmiliestrans[':)'] = "\xf0\x9f\x98\x8e"; }, 6 );
样
use
在此处的答案中使用了很多关键字,但这对全局变量有很好的提醒,再次感谢(:) <-也许我们可以使用对称的笑脸为每个人提供更好的可访问性@IsmaelMiguel
将您想要的具有相同名称的图像上载到服务器(例如,在wp-content / images / smilies中),并将其放入主题的function.php中:
add_filter('smilies_src','my_custom_smilies_src',10,3); 函数my_custom_smilies_src($ img_src,$ img,$ siteurl){ 返回$ siteurl。'/ wp-content / images / smilies /'.$ img; }这样会将http://example.com/wp-includes/images/smilies/icon_question.gif替换为http://example.com/wp-content/images/smilies/icon_question.gif
3
,将其设置1
为$img
,$siteurl
将使您的代码失败 ,将被忽略,因此将在您的过滤器中未定义它:-)