WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#8640 closed enhancement (wontfix)

2 new functions for Plugin API.

Reported by: momo360modena Owned by: ShaneF
Milestone: Priority: normal
Severity: minor Version: 2.7
Component: Plugins Keywords: has-patch tested commit
Focuses: Cc:

Description

With my plugins, i use often the same action for differents Hooks.

The code is:

add_action('save_post', 'example');
add_action('publish_post', 'example');
add_action('post_syndicated_item', 'example');

Why not to create add_actions() and add_filters() ?

The result will be:

add_actions( array('save_post', 'publish_post', 'post_syndicated_item'), 'example' );

Props functions:

function add_filters($tags, $function_to_add, $priority = 10, $accepted_args = 1) {
	if ( is_array($tags) ) {
		foreach ( (array) $tags as $tag ) {
			add_filter($tag, $function_to_add, $priority, $accepted_args);
		}
		return true;
	} else {
		return add_filter($tags, $function_to_add, $priority, $accepted_args);
	}
}

function add_actions($tags, $function_to_add, $priority = 10, $accepted_args = 1) {
	return add_filters($tags, $function_to_add, $priority, $accepted_args);
}

Attachments (3)

8640.diff (2.9 KB) - added by Denis-de-Bernardy 5 years ago.
8640.2.diff (10.6 KB) - added by ShaneF 5 years ago.
8640.3.diff (6.8 KB) - added by Denis-de-Bernardy 5 years ago.
same as 8640.diff, except that the filters actually get used in the default-filters.php file.

Download all attachments as: .zip

Change History (23)

comment:1 momo360modena5 years ago

  • Component changed from General to Plugins
  • Milestone changed from 2.8 to 2.7.1
  • Owner anonymous deleted
  • Severity changed from normal to minor
  • Version set to 2.7

comment:2 johnbillion5 years ago

Looks handy.

Or add_filter() and add_action() could be modified to accept an array as the first argument to achieve the same thing.

comment:3 DD325 years ago

There's been something before about multiple actions being passed, IIRC it was thought that modifying the existing functions to accept an array would add a overhead which wasnt really needed when you could mearly have a new function for it.

comment:4 johnbillion5 years ago

Ah good point DD32.

comment:5 jacobsantos5 years ago

  • Milestone changed from 2.7.1 to 2.8

Well, I suppose it really depends, if the overhead doesn't make sense to that function.

I forget what other function it was that wanted to be added to the add_filter and add_action, but it didn't make sense at the time.

Add overhead the majority of people are not going to use will lower the limit of actions and filters that can be run before the total equals more than a second in itself.

Adding an if statement might not seem like much, but the CPU still has to make an decision on whether or not it should choose the branch that goes into the if statement or the one that ignores it. If it chooses wrong, then it has to start over.

However, making multiple function calls might be more than the overhead of the single if statement.

New functions / features usually aren't added to a minor release.

Denis-de-Bernardy5 years ago

comment:6 Denis-de-Bernardy5 years ago

  • Keywords has-patch dev-feedback added

comment:7 ShaneF5 years ago

  • Owner set to ShaneF
  • Status changed from new to assigned

Testing and upgrading code to use new format.

comment:8 ShaneF5 years ago

  • Keywords commit added; dev-feedback removed

Updated orginal patch file to include the same thing for filters 'the_comment' to process many function call backs. Can not add the priority unless someone wants to take a stab at it.

Optimized current default-filters.php file to start this off.

ShaneF5 years ago

comment:9 ShaneF5 years ago

  • Keywords needs-testing added

Just need someone to confirm that this is all good! :)

comment:10 jbsil5 years ago

If we're going to go down this road, why not allow the function parameter to be an array of function names as well?

I.E. Change

foreach ( $filters as $filter ) { 
     add_filter($filter, 'strip_tags'); 
     add_filter($filter, 'trim'); 
     add_filter($filter, 'wp_filter_kses'); 
     add_filter($filter, 'wp_specialchars', 30); 
} 

to

add_filters($filers, array('strip_tags', 'trim', 'wp_filter_kses', 'wp_specialchars'), array(null,null,null,30));

or at least

add_filters($filters, array('strip_tags', 'trim', 'wp_filter_kses'));
add_filters($filters, 'wp_specialchars', 30);

or ideally

add_filters($filters, array('strip_tags', 'trim', 'wp_filter_kses', array('wp_specialchars', 30)));

comment:11 markjaquith5 years ago

  • Milestone changed from 2.8 to 2.9

A little late for this. Will consider it early in 2.9 cycle. Shane, your 8650.2.diff patch won't work... you're not calling the new function properly. You're still passing $filter when you've removed the foreach loop.

comment:12 ShaneF5 years ago

D'oh! Oh well. :P

comment:13 ShaneF5 years ago

  • Keywords needs-patch added; has-patch needs-testing commit removed

comment:14 scribu5 years ago

  • Cc scribu@… added

comment:15 Denis-de-Bernardy5 years ago

  • Keywords has-patch tested added; needs-patch removed
  • Milestone changed from 2.9 to 2.8

I don't think it's correct to try to something like:

add_filters($filters, array('strip_tags', 'trim', 'wp_filter_kses'));

each function can have its own priority and number of arguments, and this renders the whole idea more or less pointless. and that is not to mention the clunky looking syntax with classes (especially when they're mixed up):

add_filters($filters, array(array('foo', 'bar'), array(&$bar, 'foo'), 'foobar'));

the correct approach, I think, is the first patch, 9640.diff, which is still good against 11300.

add_filters($filters_array, $single_callback, $priority, $num_args);

Denis-de-Bernardy5 years ago

same as 8640.diff, except that the filters actually get used in the default-filters.php file.

comment:16 Denis-de-Bernardy5 years ago

  • Keywords commit added

comment:17 Denis-de-Bernardy5 years ago

itching to close as wontfix myself, as I don't find this very useful.

so please patch in 2.8, or close this. this is one of those "well, why not?" tickets that can wait for years if not committed straight away.

comment:18 ryan5 years ago

  • Milestone 2.8 deleted
  • Resolution set to wontfix
  • Status changed from assigned to closed

comment:19 Denis-de-Bernardy5 years ago

thank you. :-)

comment:20 nacin4 years ago

A subset of this ticket, for add_filter() and add_action() to accept an array of hooks, has been again proposed in #14280, this time with benchmarks.

Note: See TracTickets for help on using tickets.