Make WordPress Core

Opened 14 years ago

Closed 14 years ago

Last modified 13 years ago

#8640 closed enhancement (wontfix)

2 new functions for Plugin API.

Reported by: momo360modena's profile momo360modena Owned by: shanef's profile 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 14 years ago.
8640.2.diff (10.6 KB) - added by ShaneF 14 years ago.
8640.3.diff (6.8 KB) - added by Denis-de-Bernardy 14 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)

#1 @momo360modena
14 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

#2 @johnbillion
14 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.

#3 @DD32
14 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.

#4 @johnbillion
14 years ago

Ah good point DD32.

#5 @jacobsantos
14 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.

#6 @Denis-de-Bernardy
14 years ago

  • Keywords has-patch dev-feedback added

#7 @ShaneF
14 years ago

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

Testing and upgrading code to use new format.

#8 @ShaneF
14 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.

@ShaneF
14 years ago

#9 @ShaneF
14 years ago

  • Keywords needs-testing added

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

#10 @jbsil
14 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)));

#11 @markjaquith
14 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.

#12 @ShaneF
14 years ago

D'oh! Oh well. :P

#13 @ShaneF
14 years ago

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

#14 @scribu
14 years ago

  • Cc scribu@… added

#15 @Denis-de-Bernardy
14 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-Bernardy
14 years ago

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

#16 @Denis-de-Bernardy
14 years ago

  • Keywords commit added

#17 @Denis-de-Bernardy
14 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.

#18 @ryan
14 years ago

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

#19 @Denis-de-Bernardy
14 years ago

thank you. :-)

#20 @nacin
13 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.