How to hide my-account menu on certain pages in Woocommerce? - wordpress

When logged in with Woocommerce, is there a way to only render the dashboard menu when on the index page of the logged in "mode"?
I only want to display it on "dashboard". Not on "orders", "downloads", "addresses" or the other pages.
I don't want to do it with CSS.
I have copied the template files, so i'll post the dashboard and orders templates.
dashboard.php
https://github.com/woocommerce/woocommerce/blob/master/templates/myaccount/dashboard.php
orders.php
https://github.com/woocommerce/woocommerce/blob/master/templates/myaccount/downloads.php

Instead of making changes to template files, you can use the
woocommerce_account_navigation action hook
So you get:
function action_woocommerce_account_navigation () {
// Detect the WC Dashboard page, and if NOT
if ( is_user_logged_in() && is_account_page() && is_wc_endpoint_url() ) {
// Remove navigation
remove_action( 'woocommerce_account_navigation', 'woocommerce_account_navigation' );
}
}
add_action( 'woocommerce_account_navigation', 'action_woocommerce_account_navigation', 1, 0 );

Related

How to redirect user to another page if the user is logged in - WordPress Elementor Page Builder

I have created a registration form using Elementor Page Builder. Now, I want to redirect the user to a different page if he/she is trying to access that registration page after logging in.
Is there any Elementor hook available for that? I know the WordPress function called is_user_logged_in().
function my_logged_in_redirect() {
if ( is_user_logged_in() && is_page( 12 ) )
{
wp_redirect( get_permalink( 32 ) );
die;
}
}
add_action( 'template_redirect', 'my_logged_in_redirect' );
You should get the ids of the page where the form is and the id of the page you want to redirect the user to.
Code goes in your child theme functions.php file
Reference: here
The 'Content Area Not Found' error might appear on Elementor designed sites when you use that snippet and try to edit page of ID 12 (in your example) in certain cases.
To avoid this, add the following code before the if-statement of your snippet:
if ( \Elementor\Plugin::$instance->preview->is_preview_mode() ) {
return;
}

Remove WordPress admin bar on a single page

Is there a way to remove the WordPress admin bar but just on a specific page?
I know that I can remove it or hide it completely by adding an action:
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
I've done that but on my site I have one page that is displayed in an iframe (using colorbox) and I don't ever want the admin bar to show up in that iframe.
Is there a way to hide the admin bar but just for a specific page?
Thanks,
Ben
Your function is a bit complicated, there's a filter for that. Just check for the Page ID and filter it out using the show_admin_bar filter.
function riga_hide_admin_bar(){
if( $post->ID == YOUR_POST_ID ){
return false;
}
}
add_filter( 'show_admin_bar' , 'riga_hide_admin_bar' );
Remove WordPress admin bar on a single page
Yes, you can remove admin bar from a specific page as you wish.
Use add_filter( 'show_admin_bar', '__return_false' ); to hide admin bar from a specific page.
function my_function_admin_bar(){
return false;
}
add_filter( 'show_admin_bar' , 'my_function_admin_bar');
For more details please see below link:
https://codex.wordpress.org/Plugin_API/Filter_Reference/show_admin_bar
Get ID of specific page and add your filter in condition which meet the page ID
`If(get_the_ID()==#736637){
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
}`
For me the easiest way is to add the code in the template of the page I don't want to show. At the top of the template file I just add:
<?php
show_admin_bar(false);

Hide specific dashboard items for user in Wordpress

In my website dashboard i have to hide Media, Comments, Contact menu items for one user who has administrator role. I googled that I have to use remove_cap function, but couldn't find enough information even to start my task.
You can put something like this inside your functions.php. :
add_action( 'admin_menu', 'adjust_the_wp_menu', 999999 );
function adjust_the_wp_menu() {
$user = wp_get_current_user();
if($user && isset($user->user_login) && 'desired_user_login' == $user->user_login)
{
//Remove menu items from admin area
remove_menu_page( 'edit-comments.php' );//For comments
remove_menu_page( 'upload.php' );//For media
//Remove third party plugins admin menu items
remove_menu_page( 'edit.php?post_type=your_custom_post_slug' );//for custom posts
remove_menu_page( 'your_plugin_page_slug' );//for custom plugin's page
// do stuff
}
}
Not sure what is the Contact menu in your Wordpress. I guess it is added by some plugin. You can try to figure out it's slug & use it inside the code above.
Also note, that it will only hide the menu items. Pages itself will be still available if respective url is accessed directly. To prevent this you should manage user roles or put some conditional logic into your functions.php.

Only logged in users should be able to view product in WooCommerce

I want to restrict WooCommerce products, shop page and category pages to logged in users only.
I do not want to achieve it with any plugin.
Please let me know if anybody done it before with any hook/filter/action.
Or I have to make WooCommerce template pages and add condition over there.
If I was to do this I would hook into the init action that WordPress offers.
Then do something like this in your theme's functions.php file:
function woo_check_logged_in()
{
if ( (is_product() || is_shop() ) && is_user_logged_in() )
{
}
else
{
die("You must be logged in to view this page");
}
}
add_action('init', 'woo_check_logged_in');
I haven't tested this but I believe it should get you on the right path without having to use any plugins.

Hide page for logged in Users

I wanna do the following:
If a user is logged out, he can view the page id=10, if a user is logged in and view page id=10 he will be redirected to page id=5. I tried adding the below code into my header, but it didn't work.
add_action( 'init', 'check_redirect_page' );
function check_redirect_page() {
if ( is_user_logged_in() && is_page( 10 ) ) {
wp_redirect( get_permalink( 5 ) );
exit;
}
}
Try using the wp hook rather than init; WordPress won't have got far enough at init to know whether it's dealing with a particular page or not.
add_action( 'wp', 'check_redirect_page' );
From the Conditional Tags documentation:
Warning: You can only use conditional query tags after the
posts_selection action hook in WordPress (the wp action hook is the
first one through which you can use these conditionals). For themes,
this means the conditional tag will never work properly if you are
using it in the body of functions.php, i.e. outside of a function.

Resources