how to change login/logout menu links dynamically in drupal 7? - drupal

I have created a menu in drupal 7 with Sign in link. It remains unchanged even when a user is logged in. How to change that to logout for authenticated users?

I had to use 2 menus to overcome that. One menu for authenticated users and the other for anonymous. Then I user menu_block contrib module to print out both menus in the same place based on user login status.
Hope you find this helpful... Muhammad.

The reason why it remained unchanged maybe because you gave an absolute url for sign in.
In order to create Sign In and Logout link in the same menu, please follow these steps.
For Sign In
Menu link title : "Sign In"
Path: "user/login"
For Logout
Menu link title: "Logout"
Path: "user/logout"
Now it should work like expected. Sign In link would be shown to anonymous users and Logout link would be shown to authenticated users.
For creating such menu items programmatically,
$item = array(
'link_path' => 'user/login',
'link_title' => t('Sign In'),
);
menu_link_save($item);
$item = array(
'link_path' => 'user/logout',
'link_title' => t('Logout'),
);
menu_link_save($item);
There are more options available that can be passed to menu_link_save, please check out this link to learn more about it.

By default core menu comes with user menu. It contains /user for the current logged user, but it doesn't show for anonymous access. to accomplish this you could:
Go to the user menu: admin/structure/menu/manage/user-menu.
Add a new link with the path: user/login.
Add the title you need.
Save
Look at hook_menu implementation on user module for more details, but essentially it shows up only for anonymous users.
Hope this helps

Related

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();
}

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

Drupal custom theme visibility to all users

I'm creating a custom drupal 7 theme
<?php print theme('links', array('links' => menu_navigation_links('menu-headermenu'), 'attributes' => array('class'=> array('links', 'headermenu')) )); ?>
Above is the code I'm using in my theme to show a menu I created using the Admin section.
My problem is the menu is getting displayed only for the Admin.
I want it to be shown to anonymous as well as registered users.
Also, I've tried changing the role permission by ticking 'Administer Block' and 'Administer menus and menu items' to all the three kind of users. Nothing helps.
Please help me..
I'm not 100% sure this will answer your question, because it side steps the code above - but, you could try enabling the Menu Block module, then using it to create a block based on your desired menu. Then you can place it any block region, and control it via the normal block controls.
The only trick of it is you might need to add a region to your template if it doesn't exist in the place that you want, but that's easy.

Adding wordpress menu to custom role

I am adding a menu to wordpress using the following:
add_action('admin_menu','menu');
function menu(){
add_menu_page('Test Admin Panel','Test Admin Panel','TestAdmin','test-plugin'.'- top-level','mnuTopLevel');
add_submenu_page( 'test-plugin'.'-top-level', 'Test Results', 'Test Results', 'TestAdmin', 'test-plugin'.'results', 'mnuResults' );
The menu is shown when logging in with the administrator role but not when logging in with a custom role (created with the User Role Editor).
Can you help me on this one please ?
Thanks a lot
The third argument of add_menu_page() should be a capability of the user.

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