WordPress.org

Make WordPress Core

Opened 14 months ago

Closed 8 months ago

Last modified 7 months ago

#35905 closed enhancement (fixed)

Filter to filter default smilies

Reported by: pietergoosen Owned by: pento
Milestone: 4.7 Priority: normal
Severity: normal Version:
Component: Formatting Keywords: has-patch has-unit-tests
Focuses: ui Cc:

Description

My request comes from the following question on WPSE

At this stage, if we need to override a default smiley in the smilies_init() function, we need to modify the $wpsmiliestrans global. This is from the doc block of the smilies_init() function - Source

Plugins may override the default smiley list by setting the $wpsmiliestrans
to an array, with the key the code the blogger types in and the value the
image file.

Can we add a filter so that we make this filterable. There is really no need why we should alter global variables, it is messy and really hard to debug.

Proposed syntax:

if ( !isset( $wpsmiliestrans ) ) {
	$wpsmiliestrans = apply_filters( 'smilies_trans', array(
	':mrgreen:' => 'mrgreen.png',
	':neutral:' => "\xf0\x9f\x98\x90",
	':twisted:' => "\xf0\x9f\x98\x88",
	  ':arrow:' => "\xe2\x9e\xa1",
	  ':shock:' => "\xf0\x9f\x98\xaf",
	  ':smile:' => 'simple-smile.png',
		':???:' => "\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:' => 'frownie.png',
		  '8-)' => "\xf0\x9f\x98\x8e",
		  '8-O' => "\xf0\x9f\x98\xaf",
		  ':-(' => 'frownie.png',
		  ':-)' => 'simple-smile.png',
		  ':-?' => "\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",
		   ':(' => 'frownie.png',
		   ':)' => 'simple-smile.png',
		   ':?' => "\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",
	) );
}

We can then use the filter as follow

add_filter( 'smilies_trans', function( $smilies )
{
	$smilies[':)'] = "\xf0\x9f\x98\x8e";

	return $smilies;
});

Attachments (2)

35905.patch (552 bytes) - added by Mte90 11 months ago.
35905.diff (2.1 KB) - added by jorbin 8 months ago.

Download all attachments as: .zip

Change History (10)

#1 @SergeyBiryukov
12 months ago

  • Component changed from General to Formatting
  • Keywords needs-patch added
  • Type changed from feature request to enhancement

@Mte90
11 months ago

#2 @Mte90
11 months ago

  • Keywords has-patch dev-feedback added; needs-patch removed

That patch contain the filter wp_smilies and the PHPDoc section for it.

#3 @jorbin
8 months ago

@pento would love for your opinion on this topic.

#4 @pento
8 months ago

  • Keywords needs-refresh needs-unit-tests added; dev-feedback removed
  • Milestone changed from Awaiting Review to 4.7
  • Version 4.4.2 deleted

Yeah, I'm cool with adding this.

Let's call the filter smilies, there's no need for the wp_ prefix.

I'd also like to see a unit test for it.

@jorbin
8 months ago

#5 @jorbin
8 months ago

  • Keywords has-unit-tests added; needs-refresh needs-unit-tests removed

Patch is updated and includes two unit tests.

#6 @pento
8 months ago

  • Owner set to pento
  • Resolution set to fixed
  • Status changed from new to closed

In 38504:

Smilies: Add the smilies filter.

This new filter allows the smilies array to be modified with a filter, instead of having to directly access the global.

Props mte90, jorbin.
Fixes #35905.

#7 @SergeyBiryukov
8 months ago

In 38505:

Docs: Correct @since entry for the smilies filter added in [38504].

See #35905.

#8 @SergeyBiryukov
7 months ago

In 38608:

Docs: Use a third-person singular verb for smilies filter added in [38504].

See #35905.

Note: See TracTickets for help on using tickets.