How to change the links in top menu based on login status? - wordpress

Can someone please answere the question how can I change the top menu item's link based on login status? e.g. if you're not logged it, you'll see /register link, if you're logged in you'll get # link of even (if this item is root) you will see the list of subpages for that menu?

If you are hard coding the links yourself, do something like this.
if(is_user_logged_in() ){
echo 'home';
}else{
echo 'register';
}

I did the same thing in my site like this.
step 1: create of two menu's position
You have to create two menu position like this
function register_my_menus(){
register_nav_menus(
array(
'new_user_navigation'=>__('New User Navigation'),
'registered_user_navigation'=>__('Registered Navigation'),
)
);
}
add_action( 'init','register_my_menus' );
Place the above code in function.php file.
now this will create two navigation areas in your admin panel menu section. From ware you can select the menu to be shown under these navigation areas in front panel.
Step 2: Select the menu from admin panel.
Now select the menu to be shown in particulate menu position.
Step 3: Edit the code in header.php file.
Now by using #In God I Trust code you can let the WordPress decide which menu to be displayed when the user in logged in like this.
if(is_user_logged_in() ){
if ( has_nav_menu( 'registered_user_navigation' ) ){
wp_nav_menu( array('theme_location' => 'registered_user_navigation',
'menu_item' => 'object_id'
) );
}
}else{
if ( has_nav_menu( 'new_user_navigation' ) ){
wp_nav_menu( array('theme_location' => 'new_user_navigation',
'menu_item' => 'object_id'
) );
}
}

Related

Wordpress menus shows changed items only to logged in user

I'm working on a wordpress website where i have a problem. I need to change the main menu and remove some entries, theese entries are removed only for logged in users, if i go to the website as anonymous user i see the original menu with all elements. I'm going crazy.
use this https://wordpress.org/plugins/if-menu/,
or you can use "is_admin" in four functions.php too.
For that, you will need to remove those items from your menu and then please place the following code in your functions.php file, and replace the items and links
function add_login_logout_register_menu( $items, $args ) {
if ( $args->theme_location != 'primary' ) {
return $items;
}
if ( is_user_logged_in() ) {
$items .= '<li class="right">'. __("item1") .'</li>';
$items .= '<li class="right">'. __("item1") .'</li>';
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'add_login_logout_register_menu', 199, 2 );
i already installed "IF MENU" plugin, but it didn't work for my website, so i removed id. after that, i cannot update the "anonymous" menu anymore (it seems to happen mainly in the home page). It seems that IF MENU broken my website

Login and Register in woocommerce site

I use Wordpress 5.2 with Woocommerce 3.6.2. I need when the visitor reaches the site, in the menu bar the visitant see the options "Login" and "Register". Once the user registers or login on the site, the options "Login" and "Register" disappear and the site show the options of "My Account" as a drop-down menu next to the options "Cart", "End Purchase", "Logout"
How can I achieve this? I'm looking for information but I do not give yet with a solution.
You'll need to extend your (child-) themes functions.php with the following:
function so_loginout_menu_links( $items, $args ) {
if ( $args->theme_location == 'primary' && function_exists('is_woocommerce') ) {
if (is_user_logged_in()) {
$items .= '<li>'. __("Log Out") .'</li>';
$items .= '<li>'. __("My Account") .'</li>';
} else {
$items .= '<li>'. __("Log In") .'</li>';
$items .= '<li>'. __("Register") .'</li>';
}
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'so_loginout_menu_links', 10, 2 );
This extends the menu "primary" with two links by filtering it, dependant on whether the user is logged in or not. I've added a check for woocommerce as well, as the account page is woocommerce specific and might throw errors in case it's deactivated.
"primary" in the code above might need to be replaced with "primary-menu", "top" or whatever your themes primary navigation menu is called.
Also make sure that Administration > Settings > General > Membership "Anyone can register" is checked, otherwise the link may not be shown.
Note there's no additional CSS in my example now, so the additional items will not yet be in a dropdown or anything. To get that, inspect your existing menu items and add the according classes and additional top level items into the code above accordingly.

How can I add a ID to a element only on homepage with Wordpress?

I would like to add jetpack infinity scroll to my webpage.
Unfortunately the div which contain all post, not have ID only class have him, so I added ID with php:
if ( is_home() ) {
$homepagepostsid="posts-content";
}else{
$homepagepostsid="";
}
HTML:
<div id="<?php $homepagepostsid ?>" class="loop-container loop-container--wp <?php echo esc_attr($loop_classes); ?>">
</div>
As you can see, I tried to reach something like this in some world:
If you're on home page, php add a id to loop-container loop-container-- div, else the id value is empty.
Unfortunately in somehow this doesn't work, because the infinity scroll trying to load posts to wp-admin area, which loos like this:.:
On the top this text can be found:
"The comments closed"
And the chrome inspector also it confirm that hypothesis , which I mention before..:
.:.:
The page trying to load the posts to wp admin area.
How can I fix this?
My jetpack infinity scroll code in functions.php:
function zilla_infinite_scroll_render() {
}
get_template_part( 'loop_item', 'standard' );
add_theme_support( 'infinite-scroll', array(
'container' => 'posts-content',
'render' => 'zilla_infinite_scroll_render',
'type' => 'scroll',
));
Try adding !is_admin to your condition,
$homepagepostsid = is_home() && !is_admin() ? 'posts-content' : '' ;

List Pages on Admin Menu in WordPress

I am trying to make my clients dashboard as simple as possible. Is there a way to do a wp_list_pages down the left side of the admin menu? Instead of clicking on Pages first.
Yes, it's a matter of using get_pages with add_submenu_page.
add_action('admin_menu', 'admin_pages_so_19817501' );
function admin_pages_so_19817501()
{
$pages = get_pages( array( 'parent' => 0 ) );
foreach( $pages as $p )
{
add_submenu_page(
'edit.php?post_type=page',
$p->post_title,
$p->post_title,
'edit_pages',
'/post.php?post='.$p->ID.'&action=edit',
null
);
}
}
You'll need some jQuery to manipulate the current CSS class from the submenu items.
See: Highlighting a Menu Item by Post Name.

Disable Shop page on Woocommerce to protect categories

Im trying to disable "shop" page in Woocommerce. Basically im creating a shop theme to sell prints and image downloads for a photographer.
Because i need to create private galleries i created a custom post type where i use the woocommerce category shortcode to show products and then i password protect the post type.
This is a workaround for password protecting the woocommerce categories (if someone knows a better one please explain).
The problem is that is someone goes to /shop they will all products, including the "protected ones". So i need to disable the shop page and i need to do it programmatically on my theme functions. Any thoughts?
To disable the shop page, copy over the archive-product.php file from the /wp-content/plugins/woocommerce/templates/archive-product.php and put in /wp-content/themes/{Your Theme}/woocommerce/archive-product.php
Open up the file and overwrite everything in file with the following code below:
<?php
global $wp_query;
$wp_query->set_404();
status_header(404);
get_template_part('404');
Save the file, and now your Shop page is gone and replaced with a 404 Page!
Add this to functions:
function woocommerce_disable_shop_page() {
global $post;
if (is_shop()):
global $wp_query;
$wp_query->set_404();
status_header(404);
endif;
}
add_action( 'wp', 'woocommerce_disable_shop_page' );
Docs: WooCommerce Conditional Functions Documentation
WooCommerce has a filter for the array that it uses to create the Product post type: woocommerce_register_post_type_product.
Rather changing the archive template to force it to redirect, you can completely remove the post type’s archive, but changing the has_archive attribute on the post type on creation.
add_filter('woocommerce_register_post_type_product', function($post_type) {
$post_type['has_archive'] = false;
return $post_type;
});
You should then remove the shop page in the CMS by going to WooCommerce » Settings » Product » Display, and clicking the “x” on the “Shop Page” option.
You might need to flush the permalink cache, which you can do just by clicking the “Update” button in Settings » Permalinks.
*Edit -
Apparently the page setting I suggested below no longer works. If WooCommerce doesn't have a plugin setting to change it, I personally would use a wordpress redirect plugin like Redirection. This way you can automatically redirect them from the undesired shop page to whatever page displays your products. It avoids a 404 issue and keeps everything in tact. It also avoids editing template files which adds complications to non-developers.
Old Answer:
Have you tried Woo settings?
Admin area, left main menu, Woocommerce > Settings
Click the pages tab.
Under Pages setup is "Shop Base Page", on the dropdown, there's a small "x" to right right. Click that to get rid of the page.
If there are links elsewhere that need to be fixed let me know and I'll find the hooks/filters to remedy it.
template_redirect is the last hook before page render so in my use case I ask if the page being viewed is the "shop" page and if it is I redirect to (in my case) a pricing page.
function my__template_redirect(){
if(is_shop()){
wp_redirect(site_url() . '/pricing/', '302');
}
}
add_action('template_redirect', 'my__template_redirect');
The last suggestion didn't work for me with WP 4.6.1 and WooCommerce 2.6.4. Hiding products in the Publish tab works for me.
http://paperhedge.com/hide-products-from-displaying-in-shop-page-woocommerce/
To disable the default shop page and leave the /shop/ slug free for custom pages use this:
function remove_woocommerce_default_shop( $args, $post_type ) {
if (class_exists('WooCommerce')) {
if ( $post_type == "product" ) {
$args['has_archive'] = true;
}
return $args;
}
}
add_filter('register_post_type_args', 'remove_woocommerce_default_shop', 20, 2);
You need to hook a couple (or maybe more) things:
/* hide category from shop pages */
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( YOUR-CATEGORY-SLUG, YOUR-CATEGORY-SLUG-2 ), // Don't display products in the knives category on the shop page
'operator' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
Change, obviusly YOUR-CATEGORY-SLUG, YOUR-CATEGORY-SLUG-2 for your cat or cats to hide from shop pages.
Then, you need to hide them from menues too, right?:
/* hide category from menues */
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
// if category and on the shop page (change it with is_woocommerce() if want to hide from all woo pages)
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( YOUR-CATEGORY-SLUG, YOUR-CATEGORY-SLUG-2 ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
But i don´t think this solve 100% the thing, since then, what about search results?
Try this
Create new page named "Shop"
Go to "woocommerce" > "Settings" > "Product" > "Display tab"
Select shop page named "Shop" then click save changes
Back to "Pages" then delete "Shop" page (keep the page on trash,
don't delete permanently)
This method works fine for me

Resources