admin管理员组文章数量:1418410
i am having trouble getting any usable information or example of the update_option action hook. my aim is to compare both old and new value of my data, then send email to user for changes. below is the hook:
add_action('update_option', function( $option_name, $old_value, $value ) {
//....
}, 10, 3)
i have compied the code as is to test run but nothing happened. my question is what goes into $option_name, $old_value, $value? sorry am fairly new to php. any explanation with any example will be appreciated.
EDIT:
below is the full code. the idea is to compare the data if it has changed, then echo:
add_action('updated_option', function( $options, $old_value, $value ) {
$options = $options;
foreach ($options as $value) {
$value['id'] = $old_value;
$value['id'] = $value;
}
if($old_value == $value){
echo 'data is current';
else{
echo 'there has been changes';
}
}, 10, 3);
i still don't get anything.
i am having trouble getting any usable information or example of the update_option action hook. my aim is to compare both old and new value of my data, then send email to user for changes. below is the hook:
add_action('update_option', function( $option_name, $old_value, $value ) {
//....
}, 10, 3)
i have compied the code as is to test run but nothing happened. my question is what goes into $option_name, $old_value, $value? sorry am fairly new to php. any explanation with any example will be appreciated.
EDIT:
below is the full code. the idea is to compare the data if it has changed, then echo:
add_action('updated_option', function( $options, $old_value, $value ) {
$options = $options;
foreach ($options as $value) {
$value['id'] = $old_value;
$value['id'] = $value;
}
if($old_value == $value){
echo 'data is current';
else{
echo 'there has been changes';
}
}, 10, 3);
i still don't get anything.
Share Improve this question edited Aug 3, 2019 at 11:16 WondaTechnologies asked Aug 2, 2019 at 22:26 WondaTechnologiesWondaTechnologies 33 bronze badges 5 |1 Answer
Reset to default 0Looking at the full code in the updated question, it seems that you're just checking if the option has been changed; so for that purpose, you could simply do so, where <option name>
is the option name like my_option
:
add_action( 'update_option_<option name>', function(){
// you shouldn't echo, but this is just for testing
echo 'option changed';
} );
And if the option's value is actually the same as the old one, the above function won't be called. Because that's how update_option()
works — if it's called but no changes in the option's value, then the hook update_option_<option name>
won't be called. And neither does the updated_option
or update_option
hook.
Now you said, "my option name contains user ID" and mentioned that the option name is <user ID>_interview_date
. And in that case, you can do something like this:
Option 1: Validates the user ID/data:
add_action( 'updated_option', function( $option_name ){ if ( preg_match( '/^(\d+)_interview_date$/', $option_name, $matches ) ) { $user_id = absint( $matches[1] ); // Check if the user exists. if ( $user_id && ( $user = get_userdata( $user_id ) ) ) { // you shouldn't echo, but this is just for testing echo $option_name . ' option changed'; } } } );
Option 2: No validations..
add_action( 'updated_option', function( $option_name ){ if ( preg_match( '/^(\d+)_interview_date$/', $option_name, $matches ) ) { // you shouldn't echo, but this is just for testing echo $option_name . ' option changed; no validations performed..'; } } );
本文标签: optionshow to compare updateoption() after it saves to database
版权声明:本文标题:options - how to compare update_option() after it saves to database? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745268763a2650761.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
//....
is just like that in your actual code, then nothing would happen. And the parameters are:$option_name
- the option name;$old_value
- the old option value;$value
- the new option value. See here. – Sally CJ Commented Aug 3, 2019 at 0:20$options
is the option name and not the option value. If you just want to see if the option has been changed, thenadd_action( 'update_option_{your_option_name}', function(){ echo 'Option changed'; } );
would do it because if there were no changes, WordPress won't trigger theupdate_option_your_option_name
orupdated_option
hook. – Sally CJ Commented Aug 4, 2019 at 4:06add_action( 'update_option_{'.$userID.'_interview_date}', function(){ echo 'Option changed'; } );
. i did that and nothing happened. if there's another way to achieve this i will be grateful. – WondaTechnologies Commented Aug 4, 2019 at 19:04{
and}
).. but anyway, see my answer. – Sally CJ Commented Aug 5, 2019 at 8:39