Last active
August 29, 2015 14:10
-
-
Save ibrokemywp/630648406cd87719a15d to your computer and use it in GitHub Desktop.
Use variable filter names when retrieving your settings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Set up a global for storing settings in your product | |
* (if you're using classes, store it in a controller) | |
*/ | |
global $myprefix_settings; | |
/** | |
* Set up your own wrapper for accessing settings in | |
* your product | |
*/ | |
function myprefix_get_setting( $setting ) { | |
global $myprefix_settings; | |
/** | |
* Get your settings if they haven't been | |
* retrieved yet. | |
*/ | |
if ( empty( $myprefix_settings ) ) { | |
$myprefix_settings = get_option( 'mysettings_unique_slug' ); | |
} | |
/** | |
* Filter the setting before returning it | |
* if it's been set | |
*/ | |
if ( !empty( $myprefix_settings[ $setting ] ) ) { | |
return apply_filters( 'myprefix-setting-' . $setting, $myprefix_settings[ $setting ] ); | |
} | |
/** | |
* Don't forget to filter it if it's empty | |
* too. | |
*/ | |
return apply_filters( 'myprefix-setting-' . $setting, null ); | |
} | |
/** | |
* Now every time you access your setting, you get a free filter | |
*/ | |
$the_most_amazing_setting_ever = myprefix_get_setting( 'the_most_amazing_setting_ever' ); | |
/** | |
* So when you're writing an addon that needs to modify your | |
* setting, you're already good to go. | |
*/ | |
add_filter( 'myprefix-setting-the_most_amazing_setting_ever', 'myaddon_make_it_better' ); | |
function myaddon_make_it_better( $setting ) { | |
$setting .= 'even more amazingness'; | |
return $setting; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment