Wordpress - Plugin - Administration -? - wordpress

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>

Related

Wordpress Client Dashboard Menu Page

I want to add a menu page on Admin as well as User Dashboard, I have developed a plugin and it is working fine on admin dashboard but it is not showing on Client/User Dashboard. May be it is due to "role" property. But I want this on client side also. Can anyone help me please,thanks in advance!
Basic purpose is that the client have to offer some products to share or download on the website form his client/user dashboard. so I need to add this functionality using a plugin and this needs to add on client dashboard to work.
function wpdocs_register_my_custom_menu_page() {
add_menu_page(
__( 'Custom Menu Title', 'oceanwp' ), 'Offers','manage_options','ump-date-updater/ump-date-updater.php', '', 'dashicons-tickets',6);
}
add_action( 'admin_menu', 'wpdocs_register_my_custom_menu_page');
According to WP Codex the $capability parameter determines whether the menu item is shown to a user. See here for more info on roles and capabilities, and how to create custom capabilities.
// run this function on your plugin activation code
function setup_capabilities()
{
$role = get_role( 'author' ); // set this to the actual role of "User"
$role->add_cap('see_my_custom_menu_item');
}
and replace manage_options in your code with see_my_custom_menu_item

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 hiding categories view from edit posts

I have created a edit link on my wordpress where subscribers can edit their posts. When they click this it takes them back to the admin portal to edit post. I want to make sure they can't see the categories widget on the right side how do I remove that from a user seeing this?
<?php edit_post_link(__("Edit Post"), ''); ?>
I ahve this un the function.php file but need to know how to make it just for subscribers.
function wpse60590_remove_metaboxes() { if() remove_meta_box( 'categorydiv' , 'post' , 'normal' ); remove_meta_box( 'tagsdiv-post_tag' , 'post' , 'normal' ); } add_action( 'admin_menu' , 'wpse60590_remove_metaboxes' );
You need to modify the capabilities of these users' role to prevent them from working with categories. The capability you need to disable for their role should be "manage_categories".
Just make sure all of the users you wish to limit are in the same role (e.g. "Contributor" or something).
In my experience the easiest way to manage capabilities for roles is the Members Plugin.
Once installed, go to Users -> Roles -> Select the role you wish to change. Find "manage_categories", uncheck it, and save.
If you are using a custom post type, we may have to add some settings where you register the taxonomy to specify the ability to assign a category to a post.

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

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.

Resources