В коде должен быть порядок.
Глобальный 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' );