WordPress add a new page to admin section - 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?

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'
);

Since WP natively supports URLs like wp-admin/admin.php?page=<your_page_handle> you can do sub pages with something like:
wp-admin/admin.php?page=yourpage
wp-admin/admin.php?page=yourpage&sub=2
wp-admin/admin.php?page=yourpage&sub=3
Then in the code that handles wp-admin/admin.php?page=<your_page_handle> you just look at the $_GET and pull up the main page or a sub-page as needed.
I've definitely seen plugins where the admin page has a little row of links across the top linking the various sub-pages.

Related

How would I structure this url functionality in wordpress

What I'm trying to do in Wordpress is import movie data from the movie database API, and display it inside a wordpress site. I'm just having trouble figuring out what would be the best way to set this up in the backend with a clean url structure.
I created a template file, and its going to have a GET variable with the movies title in it. I would want my domain to look like
mydomain.com/movie/TITLEGOESHERE
how could i set this up through wordpress, so the page I created works that way.
This would not be pulling post data from inside the wordpress. Would htaccess work here?
EDIT:
The way im thinking of it right now with a dirty url is, mydomain.com/pagewithAPIcode/?movie=MOVIETITLE, that way I can get the MOVIE TITLE as a GET variable, and then pass it to the api to grab the external movie info. And then with htaccess, rewrite it so it looks nicer
In the following code, a query variable mvtitle is added in which the movie title will be available. A rewrite rule is also created which for addresses matching the movie/{some-title} scheme displays the page movie (post type page). To read value of the mvtitle use get_query_var() function.
add_filter( 'query_vars', 'so59386348_query_vars' );
add_filter( 'page_rewrite_rules', 'so59386348_page_rewrites' );
function so59386348_query_vars( $vars )
{
$vars[] = "mvtitle";
return $vars;
}
function so59386348_page_rewrites( $rewrite_rules )
{
$page_slug = 'movie';
$rewrite_rules["$page_slug/([^/]+){1}/?$"] = 'index.php?pagename='.$page_slug.'&post_type=page&mvtitle=$matches[1]';
return $rewrite_rules;
}
$title = get_query_var( 'mvtitle', FALSE );
echo 'Movie title: ' . $title;
Please follow below instructions:
Log in to your WordPress website.
When you're logged in, you will be in your 'Dashboard'.
Click on 'Settings'.
On the left-hand side, you will see a menu. In that menu, click on 'Settings'.
Click on 'Permalinks'.
The 'Settings' menu will expand providing you additional options. Click on 'Permalinks'.
Select 'Post name'.
Click 'Save changes'.
I hope it would help you out.
Open dashboard
Search for settings
Click to permalinks in settings
Look for Permalinks common settings
in Custom structure use the url like %category%/%postname% and save
Enjoy!!
First of all open the admin panel, Go to settings->permalinks and in the custom settings, change the custom structure such as:
%custom_name%/%postname%
You can change the custom name with category or any other static name you want.

Custom Plugin - Posting data from main page to another page within your plugin

I created a new custom plugin, that adds a new admin menu to show a list of events. When you click any given event, I want to send you to another page in my plugin that will then render information about this event. I can keep it simple and just use a query string parameter, so don't need to do a form POST, but I would be interested in that as well.
There are two pages:
/my-plugin/reservation-management.php
/my-plugin/reservation-management-details.php
My setup in the base page (reservation-management.php):
add_action( 'admin_menu', 'addReservationManagementMenuItem' );
function addReservationManagementMenuItem(){
add_menu_page('Reservation Management', 'Reservation Management', 'manage_options', 'reservation_management_slug', 'reservation_management_building_function','',3);
}
Inside my function to build out the first screen, I render some clickable links. To simplify:
function reservation_management_building_function(){
if(!current_user_can('manage_options')){
?>
<h2>Clickable Link</h2>
<?php echo("<a href='reservation-management-details.php?id=$id'>Event</a>"); ?>
<?php
}
}
?>
I just simplified the code, removed some loop logic, etc, but it works and renders out in a loop all of the events with a url of reservation-details.php?id=x where x is the unique post id of each event.
The thing is, this just sends me to a page not found. I even tried using things like get_admin_url() etc
I think I'm missing a fundamental step in how a custom plugin can post from one page to another all while still being within wp-admin.
How can I use an href to safely send the admin user to another admin page within my plugin directory?
Thanks!
Has the page been declared in the plugin? It sounds like you'd need to add a submenu page to your plugin as well.
Example (edited):
add_action( 'admin_menu', 'addReservationManagementMenuItem' );
function addReservationManagementMenuItem(){
add_menu_page('Reservation Management', 'Reservation Management', 'manage_options', 'reservation_management_slug', 'reservation_management_building_function','',3);
//add any relative submenu pages here
//you can have as many submenu pages as you need
//please keep in mind that add_submenu_page() requires a different set
//of parameters passed to it
//if you want the submenu page viewed, and your function exists on your
//plugin definition page
add_submenu_page('reservation-management-details.php', 'Reservation Management Details', 'Reservation Management Details', 'manage_options', 'reservation_management_details_slug');
//if you don't want your submenu page accessible in the submenu
//use null as the first parameter to remove it's inclusion from the submenu
//specify the php file used
add_submenu_page(null, 'Reservation Management Details', 'Reservation Management Details', 'manage_options', 'reservation-management-details.php');
}
In the above example, you'd be able to post to your new page in your plugin, because you've now defined it. But often times, I find that I don't want anyone being able to get to a page I've designated as '_POST' only, so my first parameter is then set to null.
Edit:
The only thing that I can't remember off the top of my head is what that POST url needs to be. Please let me know if you need assistance with that, and I'll work it out for you.
Please let me know if you have any questions.

WordPress functions.php - Admin html injection and submitting forms

I created a new navigation item on the left for my WP Admin:
add_action( 'admin_menu', 'addManagementMenuItem' );
function addManagementMenuItem(){
add_menu_page('Issue Management', 'Issue Management', 'manage_options', 'issue_management_slug', 'issue_management_building_function','',3);
}
function issue_management_building_function(){
if(!current_user_can('manage_options')){
}
else {
?>
...
...
So where I have the ellipsis ... is where my HTML begins and I write out some information to the page with various php echo statements to print some data out.
What I would like to do is now give the user the ability to enter in a filter and press submit. This would issue a POST to another page which would receive the post data, run some stuff, and spit out something else to the screen. I was just thinking this would take the user away from the WP-ADMIN area entirely (what I want to do is keep the user all within the right pane so it looks like it's natively happening on WordPress under my new admin area)
Something feels wrong about this approach above where I'm putting tons of html into functions.php - what is the way to create pages for a custom admin section where I can do things like post forms and go to multiple pages?
I was thinking the best solution would be to put an iframe in my injected HTML in functions.php, and then the pages can talk to themselves just like normal behind the scenes in WP-admin.
Could anyone point me in the right direction?
thanks!
Considering the user input/_POST features you'd like to add to this, you may want to consider building this functionality out as your own plugin. I've always kept custom functionality limited to non-user interaction in the functions.php file, but anything further would probably be better fit as it's own plugin.
For example, what if you created a plugin directory named nullhypothesis:
add_action( 'admin_menu', 'addManagementMenuItem' );
function addManagementMenuItem(){
add_menu_page('Issue Management', 'Issue Management', 'manage_options', 'nullhypothesis/file_to_do_your_bidding.php', 'issue_management_building_function','',3);
}
It's that fourth parameter that in the documentation mentions that you should include the menu_slug, but it doesn't necessarily need to only be a function - it can also be a file you define.
Then, in your file_to_do_your_bidding.php file (within your plugin), you can add whatever _POST functionality you'd need it to. It could also exist as the 'admin' page that the administrator/whoever interacts with.
Was that what you were looking for?

Admin URL to my plugin's page

My plugin is basically a link display page, for instance if you want to display a page with links to other websites.
In wp-admin I have a menu item on the left side bar added with this code:
function bls_add_menu_page() {
add_menu_page('Custom Links', 'Custom Links', 'manage_options',
'customlinks', 'bsl_admin_page', '', 15);
}
After adding a new link, I want to redirect to my plugin home page in admin. The URL when I click on my plugin menu link is :
localhost/wp-admin/admin.php?page=customlinks
How do I get that URL in Worpdress? Currently I just do this :
wp_redirect('/wp-admin/admin.php?page=customlinks');
but I hope there is a better way of getting my plugin admin URL?
You get the concrete URL to admin.php by using the admin_url function:
admin_url('admin.php'); # http(s)://localhost/wp-admin/admin.php
That function chooses the proper sheme (http/https) based on your Wordpress configuration for you so you do not need to care about it. Same for the path to the admin. The only thing you need to specify is the file name (admin.php).
And in your concrete example you add the page query-info part:
$url = admin_url('admin.php?page=customlinks');
wp_redirect($url);
URL for menu page or options page has 'page' parameter ( page slug defined in add_menu_page() or add_options_page() ). You can always get the current page from $_GET['page'] param, so URL for the options page is:
admin_url( "options-general.php?page=".$_GET["page"] )
, and URL for menu page ( actually it works with options pages also ) is:
admin_url( "admin.php?page=".$_GET["page"] )

Wordpress - Plugin - Administration -?

I'm building a Wordpress plugin and I added a menu page which serves for management of "Vendor" entities that are kinda similar to the existing User entities.
I have a list page similar to Users List, with Edit button for every record and when I click on the "Edit" button I should be taken to the "Edit Vendor" (which does not have its submenu item in the admin menu) page for that vendor. Everything is stored in the "plugins/wp_vendors" folder.
Question: What URL should I use for opening that Edit page? How should a slug be registered for the Edit Vendor page?
PS. Vendor List is added to the admin menu with
add_menu_page('Vendors', 'Vendors', 8, 'C:\wordpress\wp-content\plugins\wp-vendors\vendors-list.php');
And I can open the List page with
http://localhost/wp-admin/admin.php?page=wp-vendors/vendors-list.php
Can anyone help me on this?
Well, first I'd suggest modifying the initial add_menu_page call:
add_menu_page( 'Vendors', 'Vendors', 'manage_options', 'wp-vendors', 'my_admin_page_callback' );
The user level argument is deprecated, so it's better to use capabilities. Also, it's better to use a callback function for the admin page, since having plugin files output data by default can lead to unexpected errors.
Now, there are a few ways you can do what you're asking. You can either register a new submenu:
add_submenu_page( 'wp-vendors', 'Edit Vendor', 'Edit Vendor', 'manage_options', 'wp-vendors-edit', 'my_admin_edit_page_callback' );
And have that function check for a vendor id to edit. If it doesn't exist, redirect them back to the main vendors menu page.
With this method, the url would look like this:
http://localhost/wp-admin.php?page=wp-vendors-edit&vendor=<vendor ID>
The other way is just to use URL arguments and separate your admin page callback with if checks. For example:
if( ( isset($_GET['action']) && $_GET['action'] == 'edit' ) && ( isset($_GET['vendor']) && !empty($_GET['vendor']) ) ){
//You're editing a vendor.
} else {
//You're listing the vendors.
}
Then make the link for editing look like this:
http://localhost/wp-admin.php?page=wp-vendors&action=edit&vendor=<vendor ID>

Resources