Skip to content

Instantly share code, notes, and snippets.

@wolfcoder
Created April 27, 2022 09:29
Show Gist options
  • Save wolfcoder/3d0671a6105332888108bef7480c5564 to your computer and use it in GitHub Desktop.
Save wolfcoder/3d0671a6105332888108bef7480c5564 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Our Plugin Menu
Description: Adding Sub menu in admin
Version: 1.0
Author: Bam
Author URI: https://wpenginer.com
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class OurPluginMenu {
function __construct() {
add_action( 'admin_menu', array( $this, 'ourMenu' ) );
}
function ourMenu() {
add_menu_page( 'Our Plugin Menu', 'Our Plugin Menu', 'manage_options', 'our-plugin-menu', array(
$this,
'optionsMainPage'
), 'dashicons-smiley', 2 );
add_submenu_page( 'our-plugin-menu', 'Sub Menu Default Page', 'Sub Menu Default', 'manage_options', 'our-plugin-menu', array(
$this,
'optionsMainPage'
), 1 );
add_submenu_page( 'our-plugin-menu', 'Sub Menu Options', 'Sub Menu', 'manage_options', 'sub-menu-options', array(
$this,
'optionsSubPage'
), 2 );
}
function handleForm() {
if ( wp_verify_nonce( $_POST['ourNonce'], 'saveInput1' ) and current_user_can( 'manage_options' ) ) {
update_option( 'input1', $_POST['input1'] ); ?>
<span>You have updated the input</span>
<?php } else { ?>
<span>You don't have permission</span>
<?php }
}
function optionsMainPage() { ?>
<div class="wrap">
<h1><?php esc_html_e( get_admin_page_title() ); ?></h1>
<?php if ( $_POST['justsubmitted'] ) {
$this->handleForm();
} ?>
<form method="post">
<input type="hidden" name="justsubmitted" value="true">
<?php wp_nonce_field( 'saveInput1', 'ourNonce' ) ?>
<table class="form-table">
<tbody>
<tr>
<th scope="row">
<label for="input1">Input1</label>
</th>
<td><input id="input1" name="input1" type="text"
placeholder="<?php esc_attr_e( get_option( 'input1' ) ); ?>"/></td>
</tr>
</tbody>
</table>
<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary"
value="Save Changes"></p>
</form>
</div>
<?php }
function optionsSubPage() { ?>
Hello World form Option Sub Page
<?php }
}
$ourPluginMenu = new OurPluginMenu();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment