wp_convert_widget_settings

函数
wp_convert_widget_settings ( $base_name, $option_name, $settings )
参数
  • (string) $base_name Root ID for all widgets of this type.
    Required:
  • (string) $option_name Option name for this widget type.
    Required:
  • (array) $settings The array of widget instance settings.
    Required:
返回值
  • (array) The array of widget settings converted to multi-widget format.
定义位置
相关方法
wp_heartbeat_settingswp_color_scheme_settingswp_user_settingswp_get_widget_defaultswp_convert_hr_to_bytes
引入
2.8.0
弃用
-

wp_convert_widget_settings: 这是一个将小工具设置从数组格式转换为字符串格式的函数。它可以用来在数据库中保存小工具的设置,并在以后检索它们。

将小工具设置从单小工具格式转换为多小工具格式。

function wp_convert_widget_settings( $base_name, $option_name, $settings ) {
	// This test may need expanding.
	$single  = false;
	$changed = false;

	if ( empty( $settings ) ) {
		$single = true;
	} else {
		foreach ( array_keys( $settings ) as $number ) {
			if ( 'number' === $number ) {
				continue;
			}
			if ( ! is_numeric( $number ) ) {
				$single = true;
				break;
			}
		}
	}

	if ( $single ) {
		$settings = array( 2 => $settings );

		// If loading from the front page, update sidebar in memory but don't save to options.
		if ( is_admin() ) {
			$sidebars_widgets = get_option( 'sidebars_widgets' );
		} else {
			if ( empty( $GLOBALS['_wp_sidebars_widgets'] ) ) {
				$GLOBALS['_wp_sidebars_widgets'] = get_option( 'sidebars_widgets', array() );
			}
			$sidebars_widgets = &$GLOBALS['_wp_sidebars_widgets'];
		}

		foreach ( (array) $sidebars_widgets as $index => $sidebar ) {
			if ( is_array( $sidebar ) ) {
				foreach ( $sidebar as $i => $name ) {
					if ( $base_name === $name ) {
						$sidebars_widgets[ $index ][ $i ] = "$name-2";
						$changed                          = true;
						break 2;
					}
				}
			}
		}

		if ( is_admin() && $changed ) {
			update_option( 'sidebars_widgets', $sidebars_widgets );
		}
	}

	$settings['_multiwidget'] = 1;
	if ( is_admin() ) {
		update_option( $option_name, $settings );
	}

	return $settings;
}

常见问题

FAQs
查看更多 >