問題描述
如果您在 WordPress 中輸入:),它會自動替換為:
有沒有辦法為:)使用不同的笑臉
最佳解決方案
覆蓋:) 的表情符號到ǟÂ~ÂŽ
內容表情符號轉換為:
add_filter( 'the_content', 'convert_smilies' );
其中 convert_smilies()功能的 this part 非常重要:
$content = preg_replace_callback( $wp_smiliessearch, 'translate_smiley', $content );
如果我們偷看 translate_smiley(),那麼我們找到 following:
// 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()的功能描述中,我們可以閱讀 following:
Plugins may override the default smiley list by setting the
$wpsmiliestransto an array, with the key the code the blogger types in and the value the image file.
這是全球 $wpsmiliestrans 陣列:
$wpsmiliestrans = array(
':mrgreen:' => 'mrgreen.png',
':neutral:' => "xf0x9fx98x90",
':twisted:' => "xf0x9fx98x88",
':arrow:' => "xe2x9exa1",
':shock:' => "xf0x9fx98xaf",
':smile:' => "xf0x9fx99x82",
':???:' => "xf0x9fx98x95",
':cool:' => "xf0x9fx98x8e",
':evil:' => "xf0x9fx91xbf",
':grin:' => "xf0x9fx98x80",
':idea:' => "xf0x9fx92xa1",
':oops:' => "xf0x9fx98xb3",
':razz:' => "xf0x9fx98x9b",
':roll:' => 'rolleyes.png',
':wink:' => "xf0x9fx98x89",
':cry:' => "xf0x9fx98xa5",
':eek:' => "xf0x9fx98xae",
':lol:' => "xf0x9fx98x86",
':mad:' => "xf0x9fx98xa1",
':sad:' => "xf0x9fx99x81",
'8-)' => "xf0x9fx98x8e",
'8-O' => "xf0x9fx98xaf",
':-(' => "xf0x9fx99x81",
':-)' => "xf0x9fx99x82",
':-?' => "xf0x9fx98x95",
':-D' => "xf0x9fx98x80",
':-P' => "xf0x9fx98x9b",
':-o' => "xf0x9fx98xae",
':-x' => "xf0x9fx98xa1",
':-|' => "xf0x9fx98x90",
';-)' => "xf0x9fx98x89",
// This one transformation breaks regular text with frequency.
// '8)' => "xf0x9fx98x8e",
'8O' => "xf0x9fx98xaf",
':(' => "xf0x9fx99x81",
':)' => "xf0x9fx99x82",
':?' => "xf0x9fx98x95",
':D' => "xf0x9fx98x80",
':P' => "xf0x9fx98x9b",
':o' => "xf0x9fx98xae",
':x' => "xf0x9fx98xa1",
':|' => "xf0x9fx98x90",
';)' => "xf0x9fx98x89",
':!:' => "xe2x9dx97",
':?:' => "xe2x9dx93",
);
或更好的 ksorted 顯示:
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[':)'] = "xF0x9Fx8Ex85";
}, 6 );
感謝 Ismael Miguel 為全球 comment,我相應地重寫了片段。
關於新的 smilies_trans 濾波器,Pieter Goosen 的 newly created newly created 門票 #35905 。
更新 – WordPress 4.7+
新的過濾器將在 WordPress 4.7+中為 available,但其名稱將為 smilies,不是 smilies_trans 。
我們上面的例子可以寫成:
add_filter( 'smilies', function( $smilies )
{
if( isset( $smilies[':cool:'] ) )
$smilies[':)'] = $smilies[':cool:'];
return $smilies;
} );
或明確地與:
add_filter( 'smilies', function( $smilies )
{
$smilies[':)'] = "xf0x9fx98x8e";
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[':)'] = "xF0x9Fx8Ex85";
return $smilies;
} );
我們不需要再混淆全球 $wpsmiliestrans 陣列了!
次佳解決方案
根據 WordPress Codex on using smilies:
Upload the images you want with the same name to your server (say in wp-content/images/smilies) and put this in your theme’s function.php:
add_filter( 'smilies_src', 'my_custom_smilies_src', 10, 3 ); function my_custom_smilies_src($img_src, $img, $siteurl){ return $siteurl.'/wp-content/images/smilies/'.$img; }That will replace http://example.com/wp-includes/images/smilies/icon_question.gif with http://example.com/wp-content/images/smilies/icon_question.gif
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。
