Wordpress is_user_logged_in && is_page not working - wordpress

Anyone can advise on why the following is not working, and maybe provide a better alternative?
if ( !is_user_logged_in() && is_page( 'shopping-cart' ) ) {
echo '<div style="position:absolute;bottom:0;width:100%;">Please login in order to proceed with the checkout.</div>';
} else {
echo '<div style="position:absolute;bottom:0;width:100%;">You are logged in.</div>';
}
Basically I am not logged in, and the 'You are logged in' message still appears, on any page. I've added the code to the theme footer, if it matters.
Thank you.

Solved, thanks to Gautam Golakiya.
The page shopping-cart was generated by a plugin but did not exist in the Page section, so after creating a black page with the same slug, the code started to behave as it should. Basically nothing wrong with it.

Related

Remove wp-login completely

First of, I'm new to using Wordpress so please excuse me if I ask something really stupid.
I want to completely remove wp-login and the register function, I don't want users to be able to register and don't want to show the option on my blog.
I did some research on this and all i get are results that explain how to hide the wp-login or change the url, which is not what i want.
Is there a function within wordpress to completly remove this from my blog? Or do i have to remove these pages from my source code?
Any help would be appreciated.
Thank you.
Untick "Anyone Can Register" in "Settings" > "General" - I think that will take care of it. If you insist on taking the form down completely then this function would completely remove the login form, and yet still provide emergency access:
add_filter( 'wp_login_errors', 'my_login_lock_down', 90, 2 );
function my_login_lock_down( $errors, $redirect_to ){
// Get to the login form using: http://example.com/wp-login.php?secretform=secretcode
$secret_key = "secretform";
$secret_password = "secretcode";
if ( !isset( $_GET[ $secret_key ] ) || $_GET[ $secret_key ] != $secret_password ) {
login_header(__('Log In'), '', $errors);
echo "</div>";
do_action( 'login_footer' );
echo "</body></html>";
exit();
}
return $errors;
}

Wordpress Logged out code

I'm fairly new to wordpress and am looking for code to show a login link on the top bar and also on the menu, the 'my account' page to only show for logged in users.
i can only find the coding is_user_logged_in() which is doing the reverse of what i'm looking for.
using the Menu Items Visibility Control plugin, unless there's a better solution out there?
The function you cited should work fine for this use case.
if ( is_user_logged_in() ) {
// Show the My Account link
} else {
// Show the login button
}
Or, if you need to check if a user is NOT logged in:
if ( !is_user_logged_in() ) {
// Do something
}

WooCommerce - Redirecting a User after They've Reset Their Password

I'm setting up a WooCommerce shop for a client and he has requested that the user be redirected back to the login form after the reset password form has been submitted.
Is this possible? Which function controls this?
Thanks in advance.
A much cleaner solution is to redirect using the woocommerce_customer_reset_password action:
function woocommerce_new_pass_redirect( $user ) {
wp_redirect( get_permalink(woocommerce_get_page_id('myaccount')));
exit;
}
add_action( 'woocommerce_customer_reset_password', 'woocommerce_new_pass_redirect' );
This code can be placed in functions.php.
Someone asked me about doing this and I think I'm going to talk them out of it, but here's what I came up with so that you could see the message and then be redirected in 5 seconds. Add a message/link to let them know you'll be redirecting. Plus, this will last through a WooCommerce upgrade if you add the template file to your theme
Add the form-lost-password.php template file to your theme and add the following code:
if ( $_GET[reset] = true && ( 'lost_password' == $args['form']) ) {
$my_account_url = get_site_url() . '/my-account';
echo "<script>window.setTimeout(function(){window.location.replace('$my_account_url')}, 5000)</script>";
}
If you dont want the time delay get rid of the window.setTimeout() and just use window.location.replace('$my_account_url').
I'm just facing the same question. I think the function that controls this is in the file woocommerce/includes/class-wc-form-handler.php. I changed the following line:
wp_redirect( add_query_arg( 'reset', 'true', remove_query_arg( array( 'key', 'login' ) ) ) );
with
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
With that change, you get redirected to the my-account page, where the customer can log in, but of course there are two problems doing that:
The customer gets no message the the password recovery was successful
Doing a WooCommerce update, the file gets overwritten with the original file
Would be great, if someone could come up with a "update-secure" solution.
Best regards
Christoph

WordPress Search goes to post page when you do a blank search

Yea so on my wordpress theme I've put together...
When you click the search button (with the search field empty) it takes you to the blog page. I have assigned the index.php as the blog page and made another page and assigned that as the home page.
Can anyone help? Its not a major problem but it is a little glitch I would like to get rid of.
Thanks.
Terry
I also faced the same problem, it is default given by wordpress.
but luckily I found something which helped me.
Add below in "Functions.php"
function SearchFilter($query) {
// If 's' request variable is set but empty
if (isset($_GET['s']) && empty($_GET['s']) && $query->is_main_query()){
$query->is_search = true;
$query->is_home = false;
}
return $query;}
add_filter('pre_get_posts','SearchFilter');
and then replace below line(line no 15) in search.php
<?php if ( have_posts() && strlen( trim(get_search_query()) ) != 0 ) : ?>
Might be it will help you too
For details read this : Customize empty search wordpress

wordpress login redirect function

I am trying to create my own plugin in wordpress. Everything works great but I want to make a loginredirect check for users who want to access to the plugins page.
Here is my function in my functions.php:
function wpuf_auth_redirect_login() {
$user = wp_get_current_user();
if ( $user->id == 0 ) {
nocache_headers();
wp_redirect(get_option('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
exit();
}
}
And this is from my myplugin.php:
function wpuf_user_edit_profile() {
wpuf_auth_redirect_login(); // if not logged in, redirect to login page
nocache_headers();
wpuf_post_form_style();
wpuf_user_edit_profile_form();
}
add_shortcode('wpuf_editprofile', 'wpuf_user_edit_profile');
It is not working. If I enter the plugins page as a guest in browser it does not redirect. It only shows the template uncomplete. Any help?
Where you say that it 'only shows the template uncomplete', this could indicate that there is a syntax error in your PHP code, and the server has stopped rendering the page at the point of that error.
I would suggest that you check for any errors by turning error reporting on, and seeing what happens from there.

Resources