New! TypeRocket v5 is now available. See docs.
Access all of Typerocket. Get Pro.
( v4 )
Admin pages are located in the WordPress admin in the main sidebar navigation. Admin pages can be used for any purpose. Commonly they are for building custom functionality that requires it\'s own administrative section.
To register an "Admin Page" in WordPress you only need one line of code.
tr_page('Api', 'view', 'APIs Page');
This one line of code adds the "Admin Page" to the WordPress admin, sets all the correct labels in the navigation and applicable places, and implements the required WordPress hooks. This would normally take many many lines of code.
Note: By default, anyone who is logged in with the capability
administrator can view a page. You can change this later.
tr_page() function takes 4 arguments:
$resource- A string set to the resource or section the page belongs to.
$action- A string set to the action the page is responsible for.
$title- A string set to the title of the page and menu.
$settings- Array menu, capability, position, view, slug.
Take a look at creating an admin page.
$settings = ['capability' => 'administrator']; $seat_index = tr_page('Seat', 'index', 'Plane Seats', $settings);
To set an icon for the admin page use the
To set a parent page use the
$settings = ['capability' => 'editor']; $add_seat = tr_page('Seat', 'add', 'Add Seat', $settings); $add_seat->setParent($seat_index);
Note: By default, child pages will inherit the parent pages permission or capability settings unless the child has a capability set.
You can set child page with the
$settings = ['capability' => 'administrator']; $add_seat = tr_page('Seat', 'add', 'Add Seat', $settings); $seat_index->addPage($add_seat);
$settings = ['capability' => 'administrator']; $add_seat = tr_page('Seat', 'add', 'Add Seat', $settings); $seat_index->apply($add_seat);
If you have a page with the action "add" you can use the
addNewButton() to have an add button appear at the top of related admin pages.
This will add an "Add New" button to the top of the seat index page and automatically find the page with the "add" action and link the button to it.
To add a page to the admin bar use the
adminBar() method. By default, this will place items under the "New" dropdown.
adminBar() method takes 3 arguments:
$id- The ID to use in the admin bar for the menu.
$title- The title of the admin bar link.
$parent_id- The ID of the parent item in the admin bar.
To have an admin page use the controller of its resource use the
useController() method. For example, if the add seat page calls the
useController() method it will use the
SeatController and call the
add method on it.
This works because when the page was created it was set to the "Seat" resource and assigned the "add" action. So, it calls
add() on the
If you're are using a controller for your pages you need to create a controller. To make a seat controller and model for the "Seat" resource use the TypeRocket Galaxy CLI.
php galaxy make:model -c base Seat
Note: Pages work best with controllers so reach for them.
To take full advantage of the controller you can map HTTP request methods to specific class functions on the controller. You will need to map actions that are not
show since only these are mapped automatically.
Note: This feature is available when a page has called
$settings = ['capability' => 'administrator']; $seat_ticket = tr_page('Seat', 'ticket', 'Tickets', $settings); $seat_ticket->useController(); $seat_ticket->mapAction('GET', 'ticket'); $seat_ticket->mapAction('POST', 'create_ticket');
When using admin pages your options for controllers are somewhat limited, due to the pattern WordPress uses for making routes within the
mapAction method allows you to take more control over how the admin routes HTTP requests to your controller class.
There are times when you want to add a page as a child page but not have it appear in the admin menu. To accomplish this use the
// create page $settings = ['capability' => 'administrator']; $view_seat = tr_page('Seat', 'view', 'View Seat', $settings); // remove menu $view_seat->removeMenu(); // add as child page $seat->apply($view_seat);
There are 5 methods for dealing with arguments. Arguments are used when the page is being registered. The available arguments are:
These methods are:
getArguments()returns the full array of arguments.
setArguments()takes an array of arguments.
getArgument()return an argument by its key.
setArgument()sets an argument by a key.
removeArgument()removes an argument by its key.
Take a look at using all the methods without affecting the already set values.
$args = $book->getArguments(); $args = array_merge( $args, [ 'position' => 20 ] ); $book->setArguments( $args ); $public = $book->getArgument( 'position' ); $book->removeArgument( 'position' ); $book->setArgument( 'position', 20 );
A parent page is placed in the menu based on its
position argument. By default, the position is
99. You see a list of menu positions in the WordPress developer reference guide.
capability setting to a value from the WordPress capability levels. This will restrict access to the page based on the permissions level.
You can also set the values to a role level. This is many times the simplest and most effective method. The role levels are:
By default, the
inherit_capability setting is set to true. If a page is a child page and it has this setting it will inherit the parent pages
capability setting if the child page has no capability set.
menu setting controls the menu title. This is not the page title.
view_file setting controls what file will be used as the page's template.
slug setting controller the admin pages URL "page" parameter. For example, a slug with the value of "my_page" will appear in the URL as
Found a typo? Something is wrong in this documentation? Fork and edit it!