Opened 10 years ago
Closed 10 years ago
#33146 closed enhancement (wontfix)
Add optional $name variable to theme_mod_$name Filter.
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.2.3 |
Component: | Themes | Keywords: | |
Focuses: | template | Cc: |
Description
I am making a plugin and using this filter: https://codex.wordpress.org/Plugin_API/Filter_Reference/theme_mod_$name
If I could access the name of the Theme Mod when the filter is applied, I could remove redundant code.
The filter is applied in 2 places
https://core.trac.wordpress.org/browser/tags/4.2.3/src/wp-includes/theme.php#L921
return apply_filters( "theme_mod_{$name}", $mods[$name] );
$name can be added to the end for optional use
return apply_filters( "theme_mod_{$name}", $mods[$name], $name );
Same change for line 928
https://core.trac.wordpress.org/browser/tags/4.2.3/src/wp-includes/theme.php#L928
return apply_filters( "theme_mod_{$name}", $default );
Becomes
return apply_filters( "theme_mod_{$name}", $default, $name );
Change History (5)
#1
in reply to:
↑ description
;
follow-up:
↓ 2
@
10 years ago
#2
in reply to:
↑ 1
@
10 years ago
Thanks obenland!
Replying to obenland:
You need to know the name of the theme mod to use that filter, hat keeps you from using that same name within the callback?
This is true, however, I would like to do it with 1 callback function. The way it currently exists, I have to create a unique function for each.
If I could get the name as a variable I could do this.
example:
function my_function($value, $name){ //stuff } add_filter("theme_mod_X", "my_function", 10, 2); add_filter("theme_mod_Y", "my_function", 10, 2); add_filter("theme_mod_Z", "my_function", 10, 2);
Currently I'm doing it like this
function my_function($value, $name){ //stuff } $callback = create_function("$value", 'my_function("$value", "X");'); add_filter("theme_mod_X", $callback); $callback = create_function("$value", 'my_function("$value", "Y");'); add_filter("theme_mod_Y", $callback); $callback = create_function("$value", 'my_function("$value", "Z");'); add_filter("theme_mod_Z", $callback);
#3
follow-up:
↓ 4
@
10 years ago
You could get the name with $name = str_replace( 'theme_mod_', '', current_filter() );
.
Welcome clintstegman! :)
Replying to clintstegman:
You need to know the name of the theme mod to use that filter, hat keeps you from using that same name within the callback?