FreeTibet
@FreeTibet
dharma supplier

Wordpress Plugin Api: Почему не выводятся все вкладки nav-tabs?

Разбираюсь с Wordpress Plugin Api и хочу вывести вкладки tabs-nav на страницу настроек моего плагина.
Но мой код выводит только одну вкладку из массива. Может кто-нибудь знает, где я не прав?
function add_plugin_page(){
	add_options_page( 'Color Manager', 'Color Manager', 'manage_options', 'cm_settings', 'cm_options_page_output' );
}

function cm_options_page_output(){
	global $cm_active_tab;
	$cm_active_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : 'main'; ?>
	<h2 class="nav-tab-wrapper">
	<?php do_action( 'cm_settings_tab' ); ?>
	</h2>
	<?php do_action( 'cm_settings_content' );
}

function cm_tab(){
	global $cm_tab;
	global $cm_active_tab; ?>
	<a class="nav-tab <?php echo $cm_active_tab == $cm_tab["slug"] || '' ? 'nav-tab-active' : ''; ?>" href="<?php echo admin_url( 'options-general.php?page=cm_settings&tab='.$cm_tab["slug"] ); ?>"><?=$cm_tab["title"]?> </a>
	<?php
}

function cm_render_options_page() {
	global $cm_active_tab;
	global $cm_tab;
	if ( '' || $cm_tab["slug"] != $cm_active_tab )
		return;
	echo "setting section";
}


$less_variables = array (
	array ( 
		"slug" => "main",
		"title" => "Основные",
		"variables" => array (
			"variable1" => "color 1",
			"variable2" => "color 2",
		),
	),
	array ( 
		"slug" => "header",
		"title" => "Хедер",
		"variables" => array (
			"variable1" => "color 1",
			"variable2" => "color 2",
		),
	),
	array ( 
		"slug" => "header1",
		"title" => "Хедер1",
		"variables" => array (
			"variable1" => "color 1",
			"variable2" => "color 2",
		),
	)
);
global $cm_tab;

add_action('admin_menu', 'add_plugin_page');
foreach ($less_variables as $cm_tab) {
	add_action('cm_settings_tab', 'cm_tab') ;
}
add_action( 'cm_settings_content', 'cm_render_options_page' );
  • Вопрос задан
  • 58 просмотров
Пригласить эксперта
Ответы на вопрос 1
HeadOnFire
@HeadOnFire
PHP, Laravel & WordPress Evangelist
В коде должен быть порядок.
Глобальный scope забивать не нужно.

<?php
/*
 * Example plugin.
 *
 * Plugin Name: Example plugin
 * Description: Plugin description.
 * Version: 0.1.0
 * Author: Ihor Vorotnov
 * Author URI: https://ihorvorotnov.com
 * Text Domain: cm
 * Domain Path: /languages
 * License: GPL-2.0+
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/

// Exit if accessed directly
defined( 'ABSPATH' ) or die();

/**
 * Define default configuration for tabs and return it.
 *
 * @param null $key
 *
 * @return array
 */
function cm_config( $key = null ) {

	// Define configuration
	$config = array(
		'main' => array (
			'title'     => 'Основные',
			'variables' => array (
				'variable1' => 'color 1',
				'variable2' => 'color 2',
			),
		),
		'header' => array (
			'title'     => 'Хедер',
			'variables' => array (
				'variable1' => 'color 1',
				'variable2' => 'color 2',
			),
		),
		'header1' => array (
			'title'     => 'Хедер1',
			'variables' => array (
				'variable1' => 'color 1',
				'variable2' => 'color 2',
			),
		),
	);

	// If specific element of the config is requested
	if ( ! is_null( $key ) && array_key_exists( $key, $config ) ) {
		return $config[ $key ];
	}

	// Otherwise return the whole config
	return $config;
}

/**
 * Helper function to determine currently active tab and make sure it exists.
 *
 * @return string
 */
function cm_active_tab() {

	// If the tab slug is passed via GET and it actually exists
	if ( isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], cm_config() ) ) {
		return esc_attr( $_GET['tab'] );
	}

	// Otherwise, the 'main' tab is the default
	return 'main';
}

/**
 * Register options page.
 */
function cm_add_plugin_page() {

	add_options_page(
		'Color Manager',
		'Color Manager',
		'manage_options',
		'cm_settings',
		'cm_options_page_output'
	);
}
add_action( 'admin_menu', 'cm_add_plugin_page' );

/**
 * Render options page HTML.
 */
function cm_options_page_output() {
	?>
	<div class="wrap">
		<h1>Color Manager</h1>

		<h2 class="nav-tab-wrapper">
			<?php do_action( 'cm_settings_tabs' ); ?>
		</h2>
		<?php do_action( 'cm_settings_content' ); ?>
	</div>
	<?php
}

/**
 * Render tabs dynamically.
 */
function cm_render_tabs() {

	// Get the plugin config without globals everywhere
	$config = cm_config();

	// Loop through the elements from config and build tab links
	foreach ( $config as $slug => $element ) {

		// Build URL for current element
		$url = admin_url( 'options-general.php?page=cm_settings&tab=' ) . $slug;
		// Set default css class(es)
		$classes = 'nav-tab';

		// Add css class to active tab
		if ( cm_active_tab() == $slug ) {
			$classes .= ' nav-tab-active';
		}

		// Render the tab link HTML
		printf( '<a href="%s" class="%s">%s</a>',
			$url,
			$classes,
			$element['title']
		);

	}
}
add_action( 'cm_settings_tabs', 'cm_render_tabs' );

/**
 * Render tab content dynamically.
 */
function cm_render_tab_content() {

	// Get currently active tab slug
	$active_tab = cm_active_tab();
	// Get data for this tab from the config
	$data = cm_config( $active_tab );

	var_dump( $data );

	// Load corresponding template part or call the function to render requested content.

	// --------------------------------------------------------------
	// Your code here...
	// --------------------------------------------------------------

}
add_action( 'cm_settings_content', 'cm_render_tab_content' );


5a46194f2130c669082458.jpeg
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы