Make WordPress Core

Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#16577 closed defect (bug) (invalid)

The register_setting function doesn't exist in Network Admin area

Reported by: cgrymala's profile cgrymala Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.1
Component: Plugins Keywords:
Focuses: Cc:

Description

In the 3.0.x branch of WordPress, plugin authors could use the register_setting() function to whitelist either individual site options (the 'options' table) or network options (the 'sitemeta' table). If a network option was registered using the register_setting() function, calling add_site_option() or update_site_option() would invoke the sanitization callback included in the register_setting() function.

However, in the current trunk version of WordPress 3.1, the register_setting() function is not instantiated when working within the Network Admin area. In my case, I've chosen to use add_filter( 'sanitize_option_' . $plugin_name, 'plugin_sanitize_callback' ); instead.

I'm not sure if this is intended behavior or not. If the register_setting() function is supposed to be available when working on settings within the Network Admin area, hopefully it will be easy to fix. If that function is not supposed to be available, please feel free to ignore this ticket.

Either way, the codex entries for the various functions probably should be updated to include definitive information about whether the sanitize callback will be invoked automatically for Network options or not.

Change History (5)

#1 @ryan
12 years ago

I can't reproduce the problem. Can you show how you are registering the setting?

#2 @cgrymala
12 years ago

If you look at the source of the class-extended-super-admins.php file for version 0.4a of the Extended Super Admins plugin (function excerpted below - the function shown below is hooked to the 'init' action), you will see how I was trying to use the register_setting function.

Version 0.5a now uses the add_filter() function if the register_setting() function is not available. Keep in mind that I am talking about using this within the Network Admin area, not the Site Admin area.

		function _init() {
			if( function_exists( 'load_plugin_textdomain' ) )
				load_plugin_textdomain( ESA_TEXT_DOMAIN, false, ESA_PLUGIN_PATH . '/lang/' );
				
			if( is_admin() && isset( $_REQUEST['page'] ) && $_REQUEST['page'] == ESA_OPTIONS_PAGE ) {
				if( function_exists( 'register_setting' ) )
					register_setting( ESA_OPTION_NAME, ESA_OPTION_NAME, array( $this, 'verify_options' ) );
				if( function_exists( 'wp_enqueue_script' ) ) {
					wp_enqueue_script( 'esa_admin_scripts' );
				}
				
				if( function_exists( 'wp_enqueue_style' ) ) {
					wp_enqueue_style( 'esa_admin_styles' );
				}
			}
		}

In earlier versions of the plugin, I was getting a PHP error about the fact that the register_setting() function didn't exist, which is why I added the check to see if the function exists or not.

As I said, in version 0.5a of the plugin (and beyond), I started using the following function to get around the issue.

		function _init() {
			if( function_exists( 'load_plugin_textdomain' ) )
				load_plugin_textdomain( ESA_TEXT_DOMAIN, false, ESA_PLUGIN_PATH . '/lang/' );
				
			if( is_admin() && isset( $_REQUEST['page'] ) && $_REQUEST['page'] == ESA_OPTIONS_PAGE ) {
				if( function_exists( 'register_setting' ) )
					register_setting( ESA_OPTION_NAME, ESA_OPTION_NAME, array( $this, 'verify_options' ) );
				else
					add_filter( 'sanitize_option_' . ESA_OPTION_NAME, array( $this, 'verify_options' ) );
				if( function_exists( 'wp_enqueue_script' ) ) {
					wp_enqueue_script( 'esa_admin_scripts' );
				}
				
				if( function_exists( 'wp_enqueue_style' ) ) {
					wp_enqueue_style( 'esa_admin_styles' );
				}
			}
		}

Just in case it makes a difference, here is how I'm instantiating the class for my plugin (includes a check to see if any of the multi-network plugins are installed, as well):

function instantiate_extended_super_admins() {
	if( !is_multisite() )
		return;
	if( is_multinetwork() ) {
		require_once( 'class-wpmn_super_admins.php' );
		$wpsa = new wpmn_super_admins;
		$wpsa->is_multi_network = true;
		return $wpsa;
	} else {
		require_once( 'class-extended_super_admins.php' );
		$wpsa = new extended_super_admins;
		$wpsa->is_multi_network = false;
		return $wpsa;
	}
}
add_action( 'plugins_loaded', 'instantiate_extended_super_admins' );

Then, within the __construct() method of my class, I have the following call:

			add_action( 'init', array( $this, '_init' ) );

With version 0.4a of the plugin (using just the register_setting() function), everything worked fine in version 3.0.5 of WordPress. However, in version 3.1, the register_setting() function is unavailable.

I hope this helps. Thank you.

#3 follow-up: @nacin
12 years ago

It's only available in the admin. So admin_init, not init. That should solve your problem.

#4 in reply to: ↑ 3 @cgrymala
12 years ago

  • Resolution set to invalid
  • Status changed from new to closed

Replying to nacin:

It's only available in the admin. So admin_init, not init. That should solve your problem.

That seems to have worked. Sorry for erroneously reporting a bug. Thanks for the help.

#5 @nacin
12 years ago

  • Keywords needs-codex 2nd-opinion removed
  • Milestone Awaiting Review deleted

No problem.

Note: See TracTickets for help on using tickets.