Admin URL to my plugin's page - wordpress

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"] )

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.

Is it possible to customize the logout page or create a custom logout page in Wordpress?

At the moment if the user navigates to the default logout page it looks like this:
This is not consistent with the theme of my website so I would like this content inside my own custom page. Can this be done?
I have a plugin installed My Theme Plugin which is designed to let me specify a logout page but I do not know how to construct it.
Since this may help others, I'm adding my comments as an answer.
You need to initially use the logout_url filter - https://developer.wordpress.org/reference/hooks/logout_url/
This will allow you to set up a page when a user clicks on the logout link. Next, you simply create the page however you need to (basic WordPress page, special template, etc.).
On that page you would use wp_logout_url() to set the link for the Are you sure you want to logout text. e.g.
Logout
This would redirect the user to the home page after they've logged out.
Edit: shortcode to add to content:
function wpso58817718_add_logout_link( $atts ){
return 'Logout';
}
add_shortcode( 'logout_link', 'wpso58817718_add_logout_link' );
Then you can do [logout_link]
You'll have to update the end URL wp_logout_url( home_url() ) if you don't want it to go to the home page.
You can use below code to redirect user to your specific url after logout
add_action('wp_logout','ps_redirect_after_logout');
function ps_redirect_after_logout(){
wp_redirect( 'your url here' );
exit();
}

How to disable template files in single page wordpress theme

I am creating a single page Wordpress site and I would like disable/delete/remove all other pages so that they just return a 404.
So if the user tries to go to an archive page or a single post page it does not work. I only want one working page on the entire site.
You can simply force all pages to be redirected to the front page by putting the following code in your functions.php:
function redirect_to_homepage() {
if ( ! is_front_page() ) {
wp_redirect( home_url( '/' ), 302 );
exit;
}
}
add_action( 'template_redirect', 'redirect_to_homepage' );
Just a quick note: is_front_page() checks if the user is trying to access the page that is set for the front page in Settings > Reading. If no such page is set, use is_home() instead.
Reference:
http://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect
http://codex.wordpress.org/Function_Reference/is_front_page
http://codex.wordpress.org/Function_Reference/is_home
Instructions for what I think you want to have done. This will remove all other pages/posts, and make the page you did not delete into the home page of your website.
Log into your site
In the admin panel go to pages
Select all of the pages except the one you want to keep as the home
In the "Bulk Actions" select move to trash and click apply
Go into the trash section of pages
Select all of the pages
In the "Bulk Actions" select move to delete permanently and click apply
Repeat steps 2-7 for the "posts"
Go to the settings reading page in the admin panel
Check off static page
In the front page drop down select the page you did not delete
Click save at the bottom of the 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>

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