PHP Classes
elePHPant
Icontem

WP_Menu: Add menu or submenu page in WordPress

Recommend this page to a friend!
  Info   View files Documentation   View files View files (28)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-10-15 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 54 This week: 3All time: 9,030 This week: 249Up
Version License PHP version Categories
wp_menu 1.0Custom (specified...5PHP 5, Blogs
Description Author

This class can add menu or submenu page in WordPress.

It provides functions to add WordPress menus and submenus with given parameters and optional callback functions to be invoked on different events like running a page, loading styles or scripts.

Innovation Award
PHP Programming Innovation award nominee
November 2017
Nominee
Vote
WordPress pages can often display menus with options to perform several types of tasks.

This class can be used to add sub-menus to WordPress menus, making it easy to create hierarchies of actions that can be performed.

Manuel Lemos
  Performance   Level  
Name: Josantonius is available for providing paid consulting. Contact Josantonius .
Classes: 19 packages by
Country: Spain Spain
Age: ???
All time rank: 231257 in Spain Spain
Week rank: 2 Up1 in Spain Spain Up
Innovation award
Innovation award
Nominee: 8x

Details

PHP WordPress Menu

Latest Stable Version Total Downloads Latest Unstable Version License Travis

Versión en español

Add menu or submenu page in WordPress.

Installation

The preferred way to install this extension is through composer.

To install PHP Wordpress Menu library, simply:

$ composer require Josantonius/WP_Menu

The previous command will only install the necessary files, if you prefer to download the entire source code (including tests, vendor folder, exceptions not used, docs...) you can use:

$ composer require Josantonius/WP_Menu --prefer-source

Or you can also clone the complete repository with Git:

$ git clone https://github.com/Josantonius/WP_Menu.git

Requirements

This library is supported by PHP versions 5.6 or higher and is compatible with HHVM versions 3.0 or higher.

To use this library in HHVM (HipHop Virtual Machine) you will have to activate the scalar types. Add the following line "hhvm.php7.scalar_types = true" in your "/etc/hhvm/php.ini".

Quick Start and Examples

To use this class, simply:

<?php
require __DIR__ . '/vendor/autoload.php';

use Josantonius\WP_Menu\WP_Menu;

Available Methods

Available methods in this library:

add($type, $data, $function, $styles, $scripts)

Add WordPress menu/submenu.

| Atttribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $type | 'menu' or 'submenu' | string | Yes | | --- | Atttribute | key | Description | Type | Required | Default | --- | --- | --- | --- | --- | --- | | $data | | Settings | array | Yes | | | | name | Menu/Submenu name | string | Yes | | | | slug | Menu/Submenu slug | string | Yes | | | | title | Menu/Submenu title | string | No | $data['name'] | | | capability | Capability required | string | No | 'manage_options' | | | icon_url | Only for menus - The URL to the icon to be used for this menu. Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme. This should begin with 'data:image/svg+xml;base64,'. Pass the name of a Dashicons helper class to use a font icon, e.g. 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS. | string | No | '' | | | position | Only for menus - The position in the menu order this one should appear. | int | No | null | | |parent | Only for submenus - The slug name for the parent menu | string | Yes | | --- | Atttribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $function | Function to be called to output | callable | No | false | --- | Atttribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $styles | Function to be called to load page styles | callable | No | false | --- | Atttribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $scripts | Function to be called to load page scripts | callable | No | false |

@return ? Boolean

Usage

Set menu params

$params = [
    'slug'       => 'searchinside-options',
    'name'       => __('Search Inside', 'search-iniside'),
    'title'      => __('Search Inside', 'search-iniside'),
    'capability' => 'manage_options',
    'icon_url'   => '//searchinside-menu-admin.png',
    'position'   => 25,
];

Add menu

Add menu without associated method.

WP_Menu::add(
    'menu', 
    $params
);

Add menu with associated method for output.

WP_Menu::add(
    'menu', 
    $params,
    [$this, 'runPage']
);

Add menu with associated methods for output and styles.

WP_Menu::add(
    'menu', 
    $params,
    [$instance1, 'runPage'], 
    [$instance3, 'load_styles']
);

Add menu with associated methods for output, styles and scripts.

WP_Menu::add(
    'menu', 
    $params,
    [$instance1, 'runPage'], 
    [$instance3, 'load_styles'],
    [$instance3, 'load_scripts']
);

Add menu with associated methods for output and scripts.

WP_Menu::add(
    'menu', 
    $params,
    [$instance1, 'runPage'], 
    false,
    [$instance3, 'load_scripts']
);

Set submenu params

$params = [
    'slug'       => 'searchinside-options',
    'parent'     => 'searchinside-options',
    'name'       => __('Options', 'search-iniside'),
    'title'      => __('Options', 'search-iniside'),
    'capability' => 'manage_options',
];

Add submenu

Add submenu without associated method:

WP_Menu::add(
    'submenu', 
    $params
);

Add submenu with associated method for output.

WP_Menu::add(
    'submenu',
    $params, 
    [$this, 'runPage']
);

Add submenu with associated methods for output and styles.

WP_Menu::add(
    'submenu', 
    $params, 
    [$instance1, 'runPage'], 
    [$instance3, 'load_styles']
);

Add submenu with associated methods for output, styles and scripts.

WP_Menu::add(
    'submenu', 
    $params, 
    [$instance1, 'runPage'], 
    [$instance3, 'load_styles'],
    [$instance3, 'load_scripts']
);

Add submenu with associated method for output and scripts.

WP_Menu::add(
    'submenu', 
    $params, 
    [$instance1, 'runPage'], 
    false,
    [$instance3, 'load_scripts']
);

Advanced example

class SampleClass {

    public function __construct() {

        add_action('wp_menu/pre_add_menu_page', [$this, 'beforeAddMenu']);

        add_action('wp_menu/after_add_menu_page', [$this, 'afterAddMenu']);
 
        add_action('wp_menu/pre_add_submenu_page', [$this, 'beforeAddSubmenu']);

        add_action('wp_menu/after_add_submenu_page', [$this, 'afterAddSubmenu']);
    }
    
    public function runPage() {

        echo 'Response from runPage method';
    }

    public function addStyles() {

        echo 'Response from addStyles method';
    }

    public function addScripts() {

        echo 'Response from addScripts method';
    }

    public function beforeAddMenu() {

        echo 'Response from wp_menu/pre_add_menu_page action';
    }

    public function afterAddMenu($hook_suffix) {

        echo 'Response from wp_menu/after_add_menu_page action';

        echo 'Hook suffix: ' . $hook_suffix;
    }

    public function beforeAddSubmenu() {

        echo 'Response from wp_menu/pre_add_submenu_page action';
    }

    public function afterAddSubmenu($hook_suffix) {

        echo 'Response from wp_menu/after_add_submenu_page action';

        echo 'Hook suffix: ' . $hook_suffix;
    }
}

$SampleClass = new SampleClass;

/
 * Add menu
 */ 
$params = [
    'slug'       => 'plugin-options',
    'name'       => __('Plugin Name',  'plugin-slug'),
    'title'      => __('Plugin Title', 'plugin-slug'),
    'capability' => 'manage_options',
    'icon_url'   => '//searchinside-menu-admin.png',
    'position'   => 25,
];

WP_Menu::add(
    'menu', 
    $params,
    [$SampleClass, 'runPage'], 
    [$SampleClass, 'addStyles'],
    [$SampleClass, 'addScripts']
);

/
 * Add submenu
 */ 
$params = [
    'slug'       => 'sub-plugin-options',
    'parent'     => 'plugin-options',
    'name'       => __('Plugin Name',  'plugin-slug'),
    'title'      => __('Plugin Title', 'plugin-slug'),
    'capability' => 'manage_options',
];

WP_Menu::add(
    'submenu', 
    $params, 
    [$SampleClass, 'runPage'], 
    [$SampleClass, 'addStyles'],
    [$SampleClass, 'addScripts']
);

# When do_action('admin_menu');

    // Response from wp_menu/pre_add_menu_page action
    // Response from wp_menu/pre_add_submenu_page action
    
    // Response from wp_menu/after_add_menu_page action
    // Response from wp_menu/after_add_submenu_page action
    
    // Hook suffix: load-toplevel_page_plugin-options
    // Hook suffix: plugin-name_page_sub-plugin-options
    
# When do_action('toplevel_page_plugin-options');
    
    // Executed only if access the page associated with this menu.

        // Response from runPage method
    
# When do_action('plugin-name_page_sub-plugin-options');
    
    // Executed only if access the page associated with this submenu.

        // Response from runPage method

# When do_action('load-toplevel_page_plugin-options');
    
    // Executed only if access the page associated with this menu.

        // Response from addStyles method
        // Response from addScripts method
    
# When do_action('load-plugin-name_page_sub-plugin-options');
    
    // Executed only if access the page associated with this submenu.

        // Response from addStyles method
        // Response from addScripts method

Action hooks

| Action | Description | Parameters | --- | --- | --- | | wp_menu/pre_add_menu_page | Before adding menu. | | wp_menu/after_add_menu_page | After adding menu. | $page Resulting page's hook_suffix, or false. | wp_menu/pre_add_submenu_page | Before adding submenu. | | wp_menu/after_add_submenu_page | After adding submenu. | $page Resulting page's hook_suffix, or false.

Tests

To run tests simply:

$ git clone https://github.com/Josantonius/WP_Menu.git

$ cd WP_Menu

$ bash bin/install-wp-tests.sh wordpress_test root '' localhost latest

$ phpunit

? TODO

  • [x] Create tests
  • [x] Improve documentation

Contribute

  1. Check for open issues or open a new issue to start a discussion around a bug or feature.
  2. Fork the repository on GitHub to start making your changes.
  3. Write one or more tests for the new feature or that expose the bug.
  4. Make code changes to implement the feature or fix the bug.
  5. Send a pull request to get your changes merged and published.

This is intended for large and long-lived objects.

Repository

All files in this repository were created and uploaded automatically with Reposgit Creator.

License

This project is licensed under MIT license. See the LICENSE file for more info.

Copyright

2017 Josantonius, josantonius.com

If you find it useful, let me know :wink:

You can contact me on Twitter or through my email.

  Files folder image Files  
File Role Description
Files folder imagebin (1 file)
Files folder imagesrc (1 file, 1 directory)
Files folder imagetests (2 files, 1 directory)
Files folder imagevendor (1 file, 1 directory)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONDUCT.md Data Auxiliary data
Accessible without login Plain text file contributors.txt Doc. Documentation
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README-ES.md Doc. Documentation
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file _config.yml Data Auxiliary data

  Files folder image Files  /  bin  
File Role Description
  Accessible without login Plain text file install-wp-tests.sh Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageWP_Menu (1 file)
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  src  /  WP_Menu  
File Role Description
  Plain text file WP_Menu.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageWP_Menu (1 directory)
  Accessible without login Plain text file bootstrap.php Example Example script
  Plain text file sample-plugin.php Class Class source

  Files folder image Files  /  tests  /  WP_Menu  
File Role Description
Files folder imageTest (3 files)

  Files folder image Files  /  tests  /  WP_Menu  /  Test  
File Role Description
  Plain text file FrontEndTest.php Class Class source
  Plain text file MenuTest.php Class Class source
  Plain text file SubMenuTest.php Class Class source

  Files folder image Files  /  vendor  
File Role Description
Files folder imagecomposer (8 files)
  Accessible without login Plain text file autoload.php Aux. Auxiliary script

  Files folder image Files  /  vendor  /  composer  
File Role Description
  Accessible without login Plain text file autoload_classmap.php Aux. Auxiliary script
  Accessible without login Plain text file autoload_namespaces.php Aux. Auxiliary script
  Accessible without login Plain text file autoload_psr4.php Aux. Auxiliary script
  Plain text file autoload_real.php Class Class source
  Plain text file autoload_static.php Class Class source
  Plain text file ClassLoader.php Class Class source
  Accessible without login Plain text file installed.json Data Auxiliary data
  Accessible without login Plain text file LICENSE Lic. License text

 Version Control Unique User Downloads Download Rankings  
 100%
Total:54
This week:3
All time:9,030
This week:249Up