Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress add a new page to admin section

Tags:

wordpress

I have already developed my plugin for WordPress and I can manage it from admin. I have passed the access to the plugin file using add_submenu_page. The problem is that the plugin is extending and I want to use another file that is linked from the main file. For example I have second_page.php?id=3. When I try to access this link, I get a

You do not have sufficient permissions to access this page.

message. I want to "validate" this page also for using with this script and I don't know how. Ideas?

like image 768
Manny Calavera Avatar asked May 12 '10 22:05

Manny Calavera


People also ask

How do I add a new page in WordPress?

To add a new page to your site, click on Pages in your Dashboard and then click Add New Page. If you would like to edit an existing page, click on the title of the page you'd like to edit. You'll be taken to the Editor where you can add text, images, contact forms, buttons, and any other content for your page.

Can I customize WordPress admin panel?

As we discussed in this article, there are four ways you can customize the WordPress admin dashboard: Replace the logo on the login page. Use a custom admin theme to change the styling of the dashboard. Create custom widgets with helpful resources for your clients.

How do I customize my WordPress admin page?

To customize the WordPress admin toolbar, install and activate the Adminimize plugin. For more details, see our step-by-step guide on how to install a WordPress plugin. Upon activation, go to the Settings » Adminimize page and look for the 'Admin Bar Backend Options' and 'Admin Bar Front End Options' tabs.


1 Answers

When you add a page with add_submenu_page(), the url should be something like:

wp-admin/admin.php?page=<your_page_handle>

Your page is actually loaded from admin.php (typically). You can add parameters to your links by appending something like &id=3 and then have your main plugin page-loading logic determine which file to include based on the parameter.

For instance

if (isset($_GET['id']) && ((int) $_GET['id']) == 3) {
  include 'second_page.php';
} else {
  include 'first_page.php';
}

Edit:

I found a trick that may be easier for you, though I haven't thoroughly tested it. Let's say that you have two pages: my_one and my_two. Just call add_submenu_page twice, and set the second page's parent as the first page. This will cause Wordpress to not add a link to the navigation bar, but you can still access your page by navigating to admin.php?page=my_two.

Example:

    add_submenu_page( 
          'my_toplevel_link'   
        , 'Page Title'
        , 'Link Name'
        , 'administrator'
        , 'my_one' // here's the page handle for page one
        , 'my_one_callback'
    );
    add_submenu_page( 
          'my_one'  // set the parent to your first page and it wont appear
        , 'Page Title'
        , 'Link Name'  // unused
        , 'administrator'
        , 'my_two'
        , 'my_two_callback'
    );
like image 117
Fletcher Moore Avatar answered Nov 18 '22 01:11

Fletcher Moore