[wp-hackers] On slug change, do something

Dobri dyordan1 at ramapo.edu
Tue Jul 30 21:13:22 UTC 2013


> function do_something($var1,$var2,$var3,,,,$varN){

If you really wanted to go down that path, look at

http://php.net/manual/en/function.func-num-args.php
http://php.net/manual/en/function.func-get-args.php
http://php.net/manual/en/function.func-get-arg.php

Essentially, just define your function as foo() [no parameters] and then use those functions to get the parameters. Of course, you should hook with a param count of something ridiculous like 999 or so and overall the solution just seems awkward.

Another idea would be to just give defaults. Chances are that that hook is used in multiple instances with different number of parameters but whatever happens, they are always in the same order. So, think about this - what do you really actually need? If you need just the term id, problem solved - define your function function($term_id) and you're done. If you need more, give default values to everything but the first parameter [function($term_id, $tt_id=-1,$whatever="potato")]. Then, check for those default values and act accordingly. I hope I made sense with all of that.

P.S. keep in mind that if you say in a hook for example "give me 3 parameters" and the actual code that calls do_action just gives you 2, nothing blows up you just get no value for the third parameter so you better have a default or use the num_args/get_args approach.

~Dobri

On Tue, 30 Jul 2013, at 4:59 PM, Haluk Karamete wrote:

> Thank you Nicholas,
> 
> Whenever I need to write a hook, I always get confused as to how many args
> I can pass to my hooked function from the core.
> And in edit_terms, I faced that too.
> For this very issue, codex has this;
> 
> To find out the number and name of arguments for an action, simply search
>> the code base for the matching do_action() call. For example, if you are
>> hooking into 'save_post', you would find it in post.php:
>> <?php do_action( 'save_post', $post_ID, $post ); ?>
>> 
>> Your add_action call would look like:
>> <?php add_action( 'save_post', 'my_save_post', 10, 2 ); ?>
>> And your function would be:
>> function my_save_post( $post_ID, $post )
>> {
>> // do stuff here
>> }
> 
> 
> so from that, it looks easy. But when I locate the code in the core,
> reaching a final conclusion is hard;
> 
> as this URI ( http://adambrown.info/p/wp_hooks/hook/save_post ) demonstrates
> that there are just too many occurrences and in one instance save_post gets
> to be used with one arg (
> http://adambrown.info/p/wp_hooks/hook/save_post?version=2.0&file=wp-includes/functions-post.php
> )
> 
> edit_terms is another good example to illustrate this frustration;
> 
> I searched the core, in one instance, the core uses it as
> 
> do_action("edit_term", $term_id, $tt_id);
> 
> 
> and in another instance, as
> 
> do_action("edit_term", $term_id, $tt_id, $taxonomy);
> 
> 
> as you see, in the former, the action takes 2 args whereas in the latter
> does it with 3.
> 
> Now the question becomes do I need to check all occurrences of the
> "do_action edit_terms" in the core to find out what's available in the most
> detailed case, otherwise it's a hit and miss?
> 
> for example, if I were to only spot do_action("edit_term", $term_id,
> $tt_id), then how would I have know that it is also possible to get the 3rd
> arg as $taxonomy?
> 
> So I write my own function ( the do_something function below), how will I
> know how many args available to me from the core?
> 
> add_action( 'edit_terms', 'do_something' );
> 
> function do_something($var1,$var2,$var3,,,,$varN){
> }
> 
> do you see what I mean?
> 
> 
> 
> On Tue, Jul 30, 2013 at 7:41 AM, Nicholas Ciske <nl at thoughtrefinery.com>wrote:
> 
>> 
>> permalink_structure_changed fires when the permalink structure is changed
>> on the Settings page -- not when a slug is updated.
>> 
>> You're probably looking for the edit_terms action which fires just before
>> the term slug is updated (keep in mind that one term can be in multiple
>> taxonomies). This gets run in wp_update_term() in wp-includes/taxonomy.php.
>> 
>> This triggers every time a term is updated, you'll need to write some code
>> to determine if the slug has actually changed or not.
>> 
>> _________________________
>> Nick Ciske
>> http://thoughtrefinery.com/
>> @nciske
>> 
>> On Jul 29, 2013, at 9:44 PM, Haluk Karamete wrote:
>> 
>>> I'd like to hook into the event when a tag slug changes, I can fire an
>>> action - for the sake of simplicity, let's say, I want to send en email
>>> when a tag slug is modified.
>>> 
>>> I tried the  following piece of code but that did not do the trick. I'm
>>> curious to know what is it that I need to do?
>>> 
>>> add_action("permalink_structure_changed","do_something");
>>> 
>>> function do_something($data){
>>> 
>>> wp_mail( 'an_email_address_here', 'subject_here', 'some text here');
>>> }
>>> 
>>> 
>>> To try this, I changed the slug of a post_tag, but no email was fired.
>>> I also tried changing the permalink of a post, that did not fired an
>> email
>>> neither.
>>> 
>>> What's the hook to tap into when a slug is changed? I'm particularly
>>> interested in post_tag slugs change,
>>> 
>>> Thanks
>> 
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>> 
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list