smilies_init

函数
smilies_init ( No parameters )

smilies_init:这个动作钩子用于注册在文章和评论中使用的笑脸(表情符号)。你可以使用这个钩子来注册你自己的自定义表情符号或修改现有的表情符号。

将笑脸代码转换为相当于图标的图形文件。

你可以关闭笑脸,方法是进入写作设置界面并取消勾选,或者将’use_smilies’选项设置为false或删除该选项。

插件可以通过将$wpsmiliestrans设置为一个数组来覆盖默认的笑脸列表,其关键是博主输入的代码,其值是图像文件。

$wp_smiliessearch全局用于正则表达式,每次调用该函数时都会被设置。

完整的笑脸列表可以在函数中找到,不会在描述中列出。也许应该为它创建一个Codex页面,这样它就可以使用了。

function smilies_init() {
	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:' => '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:'    => "xf0x9fx99x84",
			':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",
		);
	}

	/**
	 * Filters all the smilies.
	 *
	 * This filter must be added before `smilies_init` is run, as
	 * it is normally only run once to setup the smilies regex.
	 *
	 * @since 4.7.0
	 *
	 * @param string[] $wpsmiliestrans List of the smilies' hexadecimal representations, keyed by their smily code.
	 */
	$wpsmiliestrans = apply_filters( 'smilies', $wpsmiliestrans );

	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';

}

常见问题

FAQs
查看更多 >