Discussion:
On slug change, do something
Haluk Karamete
2013-07-30 02:44:06 UTC
Permalink
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
Nicholas Ciske
2013-07-30 14:41:49 UTC
Permalink
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
Post by Haluk Karamete
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
Haluk Karamete
2013-07-30 20:59:52 UTC
Permalink
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
<?php do_action( 'save_post', $post_ID, $post ); ?>
<?php add_action( 'save_post', 'my_save_post', 10, 2 ); ?>
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?
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
Post by Haluk Karamete
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
Post by Haluk Karamete
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
http://lists.automattic.com/mailman/listinfo/wp-hackers
Dobri
2013-07-30 21:13:22 UTC
Permalink
Post by Haluk Karamete
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
Post by Haluk Karamete
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
<?php do_action( 'save_post', $post_ID, $post ); ?>
<?php add_action( 'save_post', 'my_save_post', 10, 2 ); ?>
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?
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
Post by Haluk Karamete
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
Post by Haluk Karamete
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
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
J.D. Grimes
2013-07-30 21:15:26 UTC
Permalink
Post by Haluk Karamete
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.
Where are you seeing this? What is the file and line number? I just did a search and the "edit_term" action is called only once, in wp-includes/taxonomy.php (http://adambrown.info/p/wp_hooks/hook/edit_term?version=3.5&file=wp-includes/taxonomy.php). It takes the three parameters as shown in your second example above. There is an "edit_$taxonomy" hook with calls like that of your first example, and there is also an "edit_terms" (plural) hook, but I don't see any other calls to "edit_term".

-J.D.
Haluk Karamete
2013-07-30 21:51:43 UTC
Permalink
for do_action("edit_term", $term_id, $tt_id); please see
http://adambrown.info/p/wp_hooks/hook/edit_term?version=2.3&file=wp-includes/taxonomy.php

and for do_action("edit_term", $term_id, $tt_id, $taxonomy); please see
http://adambrown.info/p/wp_hooks/hook/edit_term?version=2.9&file=wp-includes/taxonomy.php

I just noticed, the only diff is the version number in the query string!
2nd one is WordPress version: 2.9 while the first is version 2.3

Boy! I had not noticed them before,
thanks to your trigger... now I know I was looking at the
http://adambrown.info site the wrong way all this time.
In this case, all I need to worry about is the latest WP version out there.

http://adambrown.info/p/wp_hooks/hook/edit_term?version=3.5&file=wp-includes/taxonomy.php

and there we have it as do_action("edit_term", $term_id, $tt_id,
$taxonomy);

But I'm not sure, if looking for the latest is gonna solve the original
problem I had. Even on the latest version, are all function calls to that
action hook pass the same number of args? That I do not know.

Isn't there a function exactly named as "edit_term" in the core which I can
go and look up to check how many args does that function possibly take ?
That way I'd be going to the function def ( which is in one place) as
opposed to going to function calls to it - thru sites like adambrown?
Post by J.D. Grimes
Post by Haluk Karamete
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.
Where are you seeing this? What is the file and line number? I just did a
search and the "edit_term" action is called only once, in
wp-includes/taxonomy.php (
http://adambrown.info/p/wp_hooks/hook/edit_term?version=3.5&file=wp-includes/taxonomy.php).
It takes the three parameters as shown in your second example above. There
is an "edit_$taxonomy" hook with calls like that of your first example, and
there is also an "edit_terms" (plural) hook, but I don't see any other
calls to "edit_term".
Post by J.D. Grimes
-J.D.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
J.D. Grimes
2013-07-30 21:58:31 UTC
Permalink
Post by Haluk Karamete
Isn't there a function exactly named as "edit_term" in the core which I can
go and look up to check how many args does that function possibly take ?
That way I'd be going to the function def ( which is in one place) as
opposed to going to function calls to it - thru sites like adambrown?
If you look at this page, it shows each time that the "edit_term" action is used:

http://adambrown.info/p/wp_hooks/hook/edit_term?version=3.5&file=wp-includes/taxonomy.php

In this case, it is only used once.

-J.D.
Nicholas Ciske
2013-07-30 22:12:41 UTC
Permalink
Here's a Gist that'll do what you want using the 2 actions fired before and after a term slug is updated.
https://gist.github.com/nciske/6117419

I had to use a global variable, but it works for all taxonomies (and a transient seems like overkill for this). I'm open to suggestions for improvement.

If you just wanted to do a single taxonomy, you could use this one hook version that exploits the fact that the slug change doesn’t immediately update the object cache, so we can compare the cached version to the database:

https://gist.github.com/nciske/6117497


_________________________
Nick Ciske
http://thoughtrefinery.com/
@nciske
Haluk Karamete
2013-07-31 08:47:43 UTC
Permalink
@Nicholas
object cache idea is very interesting.
thank you for the code.

@Grimes
it may be just that. one occurrence of the hook in the current code base.
in that case, my original concern has no base.
I'm glad I asked though. thank you
Post by Nicholas Ciske
Here's a Gist that'll do what you want using the 2 actions fired before
and after a term slug is updated.
https://gist.github.com/nciske/6117419
I had to use a global variable, but it works for all taxonomies (and a
transient seems like overkill for this). I'm open to suggestions for
improvement.
If you just wanted to do a single taxonomy, you could use this one hook
version that exploits the fact that the slug change doesn’t immediately
update the object cache, so we can compare the cached version to the
https://gist.github.com/nciske/6117497
_________________________
Nick Ciske
http://thoughtrefinery.com/
@nciske
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Loading...