Skip to content

Instantly share code, notes, and snippets.

@alanef
Last active January 21, 2020 17:49
Show Gist options
  • Select an option

  • Save alanef/80bc7347f5a5d30b32c6c2287966149e to your computer and use it in GitHub Desktop.

Select an option

Save alanef/80bc7347f5a5d30b32c6c2287966149e to your computer and use it in GitHub Desktop.
Settings tabs
/* markyp at the top of my pages calls display tabs */
//snip
<div class="fs-settings-meta-box-wrap">
<form id="fs-smb-form" method="post" action="options.php">
<?php settings_fields( $this->option_group ); // options group
?>
<?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
<?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?>
<?php $this->display_tabs(); ?>
<div id="poststuff">
// snip
/* this function sits an a admin page class that I extend with each settings page with individual setting page stuff
note the pages have to be consistently named and I'm spliting them up
in this case the same function handles settings page tabs and report page tabs
*/
public function display_tabs() {
$split = explode("-", $_GET['page']);
$page_type= $split[count($split)-1];
$tabs = Utilities::get_instance()->get_settings_page_tabs( $page_type );
if ( count($tabs) < 2 ) {
return;
}
?>
<h2 class="nav-tab-wrapper">
<?php foreach ( $tabs as $key => $tab ) {
$active='';
if ( preg_match('#'.$_GET['page'] .'$#',$tab['href'])) {
$active=' nav-tab-active';
}
echo '<a href="' . $tab['href'] . '" class="nav-tab'. $active . '">' . $tab['title'] . '</a>';
// <a href="#" class="nav-tab nav-tab-active">Social Options</a>
}
?>
</h2>
<?php
}
/* cut down version of my utilities singleton class showing the register tabs
you could simply use a global to store if you like just this suited my code
each settings page registers itself stating its url so i display can determin if active
e.g. Utilities::get_instance()->register_settings_page_tab( __( 'Firewall (WAF)', 'fullworks-security' ), 'report', admin_url( 'admin.php?page=fullworks-security-block-report' ), 1 );
*/
class Utilities {
/**
* @var
*/
protected static $instance;
protected $settings_page_tabs;
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function __construct() { }
public function register_settings_page_tab( $title, $page, $href, $position ) {
$this->settings_page_tabs[ $page ][ $position ] = array( 'title' => $title, 'href' => $href );
}
public function get_settings_page_tabs( $page ) {
$tabs = $this->settings_page_tabs[ $page ];
ksort( $tabs );
return $tabs;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment