Skip to content

Multiple update messages when multiple instances of option pages #1049

@alanef

Description

@alanef

Expected Behavior:

On updating an option page to get a single 'Settings Updated' message, even when multiple plugins use CMB2 Options pages.

Actual Behavior:

Depending on how many plugins are loaded with CMB2 option pages you can get multiple 'Settings Updated' message

Steps to reproduce (I have confirmed I can reproduce this issue on the trunk branch):

  1. create a 2 plugins with different option pages
  2. activate just one / activate two
  3. trace code with debugger

CMB2 Field Registration Code:

Not applicable

Proposed fix:

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' );
			}
		}

Pull request

I haven't yet, but happy to create a pull request if this makes sense

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions