WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#33146 closed enhancement (wontfix)

Add optional $name variable to theme_mod_$name Filter.

Reported by: clintstegman 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: @obenland
6 years ago

Welcome clintstegman! :)

Replying to clintstegman:

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.

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?

#2 in reply to: ↑ 1 @clintstegman
6 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: @ocean90
6 years ago

You could get the name with $name = str_replace( 'theme_mod_', '', current_filter() );.

#4 in reply to: ↑ 3 @clintstegman
6 years ago

Replying to ocean90:

You could get the name with $name = str_replace( 'theme_mod_', '', current_filter() );.

Thank you!!!

Now i'm off to go clean up this mess I made. ; )

#5 @obenland
6 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Great!

Note: See TracTickets for help on using tickets.