On updating an option page to get a single 'Settings Updated' message, even when multiple plugins use CMB2 Options pages.
Depending on how many plugins are loaded with CMB2 option pages you can get multiple 'Settings Updated' message
Not applicable
cmb2/includes/CMB2_Options_hookup.php
line 109 onwards
if ( ! empty( $_GET['settings-updated'] ) ) {
if ( 'true' === $_GET['settings-updated'] ) {
add_settings_error( "{$this->option_key}-notices", '', __( 'Settings updated.', 'cmb2' ), 'updated' );
} else {
add_settings_error( "{$this->option_key}-notices", '', __( 'Nothing to update.', 'cmb2' ), 'notice-warning' );
}
}
checks settings-updates for every option page, so if multiple options pages this is true multiple times.
Adding a check against the page stops this e.g.
if ( ( ! empty( $_GET['settings-updated'] ) ) && ($this->option_key == $_GET['page'] ) ){
if ( 'true' === $_GET['settings-updated'] ) {
add_settings_error( "{$this->option_key}-notices", '', __( 'Settings updated.', 'cmb2' ), 'updated' );
} else {
add_settings_error( "{$this->option_key}-notices", '', __( 'Nothing to update.', 'cmb2' ), 'notice-warning' );
}
}
I haven't yet, but happy to create a pull request if this makes sense
Good catch. I have noticed this too, but haven't had the time to dig in. Your suggestion looks like a good one. I'll dig into it.
The problem is slightly worse than explained, as any options page will show double notices, not just CMB2 as the if statement catches them all. I'm running my code in two of my plugins - so far so good.
If I'm reading the new code correctly, the updated message now can be overridden by a callback? If so that is neat, as I was trying hard to achieve that too, as often a setting change performs a function ( like clearing data ) on update / save.
Yes, that is now possible! See the example functions file for a demonstration: https://github.com/CMB2/CMB2/blob/trunk/example-functions.php#L687-L720