Change menu if user is connected - WordPress - wordpress

I'm working with WordPress.
I have two different menu.
If the user log in, the menu change.
So the first one is just for users that aren't connected.
Do you know a solution to do that ?
Have I to change some PHP files ?
EDIT
QUESTION
On my website I have a Back-Office. On the BO there is menu-2.
How could I put menu-2 only on the Back-Office ? And have the other menu (menu-1) on the original website ?

You need function is_user_logged_in() .
Shortest path to solve your problem is creating two different menus, one for logged in users and another for "guests".
Go to Appearance » Menus, create two menus logged-in and logged-out.
After creating the menus, add this code in your theme’s functions.php file or a site-specific plugin:
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = 'logged-in';
} else {
$args['menu'] = 'logged-out';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
Reference:
1.https://developer.wordpress.org/reference/functions/is_user_logged_in/

Related

How to remove wordpress date archive pages?

I have developed my own wordpress theme, and learning all kinds of programming stuff, but my priority is the content, not the programming knownledge. I need to know how to remove archive date listing from wordpress?
Question 1: The google search results displayed like this: virmodrosti.com/2017/05/
I don't want any kind of date archive option, how do you disable that?
I also don't use any kind of plugin, and always like to do it on my own.
Question 2: I don't know why older entries doesn't work anymore
virmodrosti.com/zdravje/ this page works fine
virmodrosti.com/zdravje/page/2/ it redirects to 404 error page
I only choose option in wordpress to hide that annoying /category/ with dash . inside editor at permanlinks, Category base. Maybe somehow these stuff is kinda fighting with each other and doesn't work properly.
Thank you.
This is code from Digital Nomad theme I maintain:
function digitalnomad_remove_date_archives() {
//if we are on date archive page
if ( is_date() ) {
// theme sets alternatine archive page with table like list of all posts
$archive_page = get_option( 'digitalnomad_archive_page' );
if ( $archive_page ) {
// redirs to alternatine archive page if configured (good for SEO)
wp_redirect( esc_url( get_page_link( $archive_page ) ) );
die();
} else {
// otherwise error 404 is displayed
global $wp_query;
$wp_query->set_404();
}
}
}
add_action( 'template_redirect', 'digitalnomad_remove_date_archives' );
Use the smart Archive Page Remover wordpress plugin
or visit your theme's functions.php file
then insert this code
/* Register template redirect action callback */
add_action('template_redirect','makes_remove_wp_archives');
/* Remove archive*/
function makes_remove_wp_archives(){
// if we are on category or tag or date or author archive
if(is_category()|| is_tag()||is_author()){
global $wp_query;
$wp_query->set_404();
}
}

WordPress add Login/Logout to menu editor

Ok, so I found this code, which I modified to suit my needs. Btw, I'm using WooCommerce, which explains the "wc" in some of the function calls:
//Add login/logout link to primary menu
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
function add_loginout_link( $items, $args ) {
if (is_user_logged_in() && $args->theme_location == 'primary') {
$items .= '<li>Log Out</li>';
}
elseif (!is_user_logged_in() && $args->theme_location == 'primary') {
$items .= '<li>Log In</li>';
}
return $items;
This adds the login/logout menu items, and they work fine. However, they're stuck at the end of the menu, at the moment. I'd like to be able to edit the position using the editor in wp-admin. The solution I thought of was to maybe just create login and logout pages, and use header location redirects with those lines of code in them to get to the proper URLs, but the issue I see with that is that there will always be a login item and logout, no matter what status the user is currently in. Would there maybe be a way to dynamically add a site-wide CSS rule to hide the opposite menu item, based on the log in status?
Or is there an easier way?
Not the best idea but you can try.
Create in wp-admin menu section new menu item like "Custom Link"
Log Out
http://www.example.com/account/customer-logout/
Log In
http://www.example.com/account/
And add a custom class to a WordPress menu item to manage visibility
For example, you will see "logged-in" class on the body of the page and hide "Log In" link or change it to "Account" with the same link.

Change Wordpress feed <link> for one specific tag

I have an external page that reads a RSS feed for a tag. I can't change anything on the external page, so the challenge is to change the RSS feed on my side to match the requirements.
On the external page the 3 latest posts for a tag are shown, and at the end of the section (note: not after each post but after all 3 posts) there is a "View all" link. This link receives its value from the element in the feed, which is by default set to my blog homepage, e.g. http://myblog.com). For this specific tag the link should be http://myblog.com/tag/myspecialtag.
The requirement is that the "View all" link links to the tag page instead of the homepage.
My idea was to add a condition to the element to change the URL for this specific category. I tried to change the feed template as recommended here: Customizing feeds, but for some reason it doesn't change the template at all. The code I tried is the following:
remove_all_actions( 'do_feed_rss2' );
add_action( 'do_feed_rss2', 'change_feed_rss2', 10, 1 );
function change_feed_rss2( $for_comments ) {
$rss_template = get_template_directory() . '/feeds/feed-custom_rss2.php';
if( file_exists( $rss_template ) )
load_template( $rss_template );
else
do_feed_rss2( $for_comments ); // Call default function
}
Of course I created the custom feed template and stored it in the feeds directory in my theme.
As this didn't work I tried looking into filters/hooks but I couldn't find anything helpful regarding my issue. Any ideas on how to best solve this issue?
I came up with the following solution:
I created a new custom page template custom_rss-feed.php and copied the code from wp-includes/feed-rss.php into it. I assigned this template to a new page. Additionally I added a filter to get_bloginfo_rss like the following:
function alter_bloginfo_rss($value, $key) {
if ( $key === "url" &&
is_page_template("custom_rss-feed.php")
) {
return $value . "/my/custom/url";
}
return $value;
}
add_filter("get_bloginfo_rss", "alter_bloginfo_rss", 10, 2);

How to disable admin bar in wordpress 3.9?

I'm trying to disable admin bar for registered users, whiche are not administrators. I found many solutions like adding
if ( ! current_user_can( 'add_users' ) ) {
show_admin_bar( false ); }
to functions.php, but it totally doesn't work. As I understand it is problem of "the best" version 3.9. I also tried many plugins - there are no working ones... Does any solution exist today?
Have you tried using the filter? Add this to your functions.php replacing your existing code:
function wpse_hide_admin_bar() {
if ( current_user_can( 'manage_settings' ) ) {
return true; // Show for admins
} else {
return false; // Hide for other users
}
}
add_filter( 'show_admin_bar', 'wpse_hide_admin_bar' );
including this line of code in functions.php will disable it altogether,
add_filter( 'show_admin_bar', '__return_false');
Edit: The accepted answer is better than my answer, because that method doesn't disable the admin bar altogether, but it disables it based on the user permissions.
Go to Users > Admin Profile > Toolbar > Deselect "Show Toolbar when viewing site"

Modify User Roles in WordPress?

I have been working on a client's WordPress Website and last day my client want to hide navigation menu and pages from author/contributor categories.
I have searched and tried some of the plugin but didn't get the exact thing. Please let me know what should i use to hide some pages from user and from navigation.
Only Admin can see all the pages and other members should see only 1 section that is allowed to visible for them.
Thank You
use this plugin to manage All roll:
http://wordpress.org/plugins/user-role-editor/
Here is the Complete function for removing each Menu and submenu from wp-admin for another user:
function remove_menus() {
global $menu, $submenu;
$restricted = array(__('Dashboard'), __('Profile'), __('Users'), __('Tools'), __('Comments'), __('Settings'), __('Plugins')); //Here you can also define the name like Pages
end($menu);
while (prev($menu)) {
$value = explode(' ', $menu[key($menu)][0]);
if (in_array($value[0] != NULL ? $value[0] : "", $restricted)) {
unset($menu[key($menu)]);
}
}
unset($menu[5]); // this is just for example
unset($submenu['edit.php'][16]); // this is just for example
}
Now You have to put a conditon for other user i.e:
$thisusername = $current_user->user_login; // this is to get the current user login
if (($thisusername == "user123")) {
add_action('admin_menu', 'remove_menus');
}
Note: You can find many plugins but all of them are not in depth like this code.Well you can try this plugin to manage your user's roles.Capability Manager Plugin

Resources