I try to redirect my users to a custom page after successfully submitting the lost password form. By default, users are redirected to the "my-account" page.
The method responsible for doing this redirect is called process_lost_password() and is located in plugins/woocommerce/includes/class-wc-form-handler.php and looks like follows:
/**
* Handle lost password form.
*/
public static function process_lost_password() {
if ( isset( $_POST['wc_reset_password'] ) && isset( $_POST['user_login'] ) && isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'lost_password' ) ) {
$success = WC_Shortcode_My_Account::retrieve_password();
// If successful, redirect to my account with query arg set.
if ( $success ) {
wp_redirect( add_query_arg( 'reset-link-sent', 'true', wc_get_account_endpoint_url( 'lost-password' ) ) );
exit;
}
}
}
Unfortunately, this method does not offer any action to hook into.
Is there a way to override this method or change the redirect link in any other way?
Since process_lost_password() is run as an 'wp_load' action
add_action( 'wp_loaded', array( __CLASS__, 'process_lost_password' ), 20 );
You simply need to add your own action with a higher priority
add_action( 'wp_loaded', 'your_handler', 19 );
I RESOLVED the issue by going under
WooCommerce
Emails****
Place your email in the box all the way to the bottom where it states From address because I am the administrator and Remove the admin email address.
I did a couple of dummy accounts and it work fine now all passwords work very well.
I created a page added the end points to /lost-password/ for my permalinks. Checked the custom header and checked the title boxes towards the middle of page and Published the page. That fixed the issue for me.
Related
I am using the plugin force login: https://en-gb.wordpress.org/plugins/wp-force-login/ and need to allow guests to get to the order received page after purchasing.
After checking out, a user which is logged in would be forwarded to this page: [mydomain]/checkout/order-received/[order_id]/?key=[order_key]. I have tried this: Show customer details on WooCommerce Thankyou page even if not registered but could not figure out what to do after I added it.
I currently have this code which allows certain pages to be whitelisted so users that are not logged in can bypass the "force-login" plugin and pay for the relevant product:
add_filter('v_forcelogin_whitelist', 'my_forcelogin_whitelist', 10, 1);
function my_forcelogin_whitelist() {
return array(
home_url( '/cart/' ),
home_url( '/checkout/' ),
home_url( '/cart/?add-to-cart=1465' ),
);
}
I want none logged in users to be forwarded to a page which looks like this after checkout:
[mydomain]/checkout/order-received/5304/?key=wc_order_5cffcfbc96028
For anyone that has this problem this is how I got it working. Since some of the URL's generated are dynamic I needed a work around for those. Using the following code in function.php works for ALL URL's assiciated with woocommerce:
function my_forcelogin_bypass( $bypass ) {
if ( class_exists( 'WooCommerce' ) ) {
if ( is_woocommerce() || is_wc_endpoint_url() ) {
$bypass = true;
}
}
return $bypass;
}
add_filter( 'v_forcelogin_bypass', 'my_forcelogin_bypass' );
WooCommerce Checkout/order-received issue
For the problem of [mydomain]/checkout/order-received/[order_id]/?key=[order_key] it is not loading right or is not showing something or 500 Internal Server Error ?
For Temporary purpose because whenever the plugin will be updated the file will be updated in a woo-commerce plugin?
Open File Zilla
The Visit : /var/www/html/wp-content/plugins/woocommerce/includes directory
Then in the directory open : class-wc-order.php
Find this with ctrl+F : get_checkout_order_received_url()
There Will be two lines of code(Earlier) :
$order_received_url = wc_get_endpoint_url( 'order-received', $this->get_id(), wc_get_checkout_url() );
$order_received_url = add_query_arg( 'key', $this->get_order_key(), $order_received_url );
Change to(Updated) Add a comment in the second line :
$order_received_url = wc_get_endpoint_url( 'order-received', $this->get_id(), wc_get_checkout_url() );
//$order_received_url = add_query_arg( 'key', $this->get_order_key(), $order_received_url );
Save it and update it to the server.
You issue will be resolved, but it's for temporary it will be changes whenever the woocommerce plugin will be updated, so you have to update it again.
Thanks!
I understand that I can use the following code in WordPress to ensure that users who are already logged into my site are not re-directed to the Dashboard when they click the login button on my home page:
<?php
if(is_user_logged_in()){
// redirect to desired page
}
?>
I would be grateful for advice on where I should place this code and which part of it exactly should be overwritten with the URL for the page to which I wish to send logged in users. I should add that the desired page is the same page as I direct users to on logging in.
Many thanks in advance for your kind assistance.
Just take a look here, for more perspective: https://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect
If you want to land your Users, for example to this page example.com/some-page/ after login, you can use this code:
function mysite_login_redirect($redirect_to, $request, $user) {
if (!isset($_GET['loggedout'])) {
return (isset($user->roles) && is_array($user->roles) && in_array('administrator', $user->roles)) ? admin_url() : site_url('/some-page/');
}
}
add_filter('login_redirect', 'mysite_login_redirect', 10, 3);
The algorithm for doing this is simple:
If the user is an administrator, continue to admin_url()
Otherwise, redirect to, say the site_url('/some-page/')
The login_redirect filter will pass three arguments to the function:
$redirect_to
$request
$user
We’re primarily concerned with the third as we can take a look at its roles attribute to determine if it contains the administrator value.
And with that, we are now redirecting non-admin users to the specific page rather then the dashboard.
site_url('/some-page/') will transform to example.com/some-page/
Hope it will help you :)
As I undestood about your question, you are trying to prevent users to visualize the dashboard of WordPress. You can prevent non-admin users to go to the Dashboard on the admin init hook.
add_action( "admin_init", "prevent_dashboard_non_admin" );
function prevent_dashboard_non_admin() {
if ( ! current_user_can('manage_options') && ! defined( 'DOING_AJAX' ) ) {
wp_redirect( home_url() ); // You can change that home_url() to any page URL you are tring to redirect
}
}
You can put this code inside functions.php
Note: This code has not been tested, please leave a comment and I will create the update if you have any issues with it.
UPDATE
function my_is_loggin_page() {
if ( $GLOBALS['pagenow'] === 'wp-login.php' ) {
if( is_user_logged_in() ) {
wp_redirect( "yourpage" );
}
}
}
add_action( "init", "my_is_loggin_page", 12 );
How can I detect if the "myaccount/my-account.php" template is used on the Dashboard.
Currently I use:
<?php
global $wp;
if ( !isset($wp->query_vars['page']) ) {
?>
Back to my Account
<?php } ?>
<div class="myaccount_content">
<?php
do_action( 'woocommerce_account_content' );
?>
</div>
But that feels kind of hacky. Isn't there something like a is_myaccount_dashboard() function?
Update: Detecting specifically the My account "Dashboard" page
<?php
global $wp;
$request = explode( '/', $wp->request );
// If NOT in My account dashboard page
if( ! ( end($request) == 'my-account' && is_account_page() ) ){
?>
Back to my Account Dashboard
<?php
}
?>
<div class="myaccount_content">
<?php
do_action( 'woocommerce_account_content' );
?>
</div>
Tested and works.
Original answer:
Yes of course there is is_account_page() native WooCommerce conditional that returns true on the customer’s account pages.
Here is an example using is_account_page() and is_user_logged_in(). To get the my account link url you can use: get_permalink( get_option('woocommerce_myaccount_page_id') ).
if ( !is_account_page() ) { // User is NOT on my account pages
if ( is_user_logged_in() ) { // Logged in user
// Link to "My Account pages dashboard".
?>
<?php _e( 'My Account', 'woocommerce' ); ?>
<?php }
else { // User is NOT logged in
// Link to "Login / register page".
?>
<?php _e( 'Login / Register', 'woocommerce' ); ?>
<?php
}
}
?>
Reference:
Official WooCommerce Conditional Tags
Display My Account link in a template file
After that you can Override WooCommerce Templates via a Theme using my account templates to fine tune even more WooCommerce behaviors…
To detect the exact page you're in, within the My Account area, (to allow you to determine which template is being used), I don't think Woocommerce provides a way.
I think you'll have to get the current URL, with vanilla PHP, and compare it to the URL of the page that is set to be the Dashboard/My Account Home page.
e.g.
$current_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$dashboard_url = get_permalink( get_option('woocommerce_myaccount_page_id'));
if($dashboard_url == $current_url){
// do your stuff here
}
Woocommerce's is_account_page() conditional function will return true for ALL the My Account sub pages, so can't be used to determine if you're specifically on the Dashboard page.
I had the same question (years later, lol). For people looking at the answer and wondering why it isn't helpful there are endpoint detecting functions available in woocommerce that do exactly what you're looking for. You can read the list of functions available here.
This is taken directly from the woocommerce docs. I am just copying it just incase the link is broken in the future
is_account_page() =>
Returns true on the customer’s account pages.
is_wc_endpoint_url() =>
Returns true when viewing any WooCommerce endpoint
is_wc_endpoint_url( 'order-pay' ) =>
When the endpoint page for order pay is being displayed.
is_wc_endpoint_url( 'order-received' ) =>
When the endpoint page for order received is being displayed.
is_wc_endpoint_url( 'view-order' ) =>
When the endpoint page for view order is being displayed.
is_wc_endpoint_url( 'edit-account' ) =>
When the endpoint page for edit account is being displayed.
is_wc_endpoint_url( 'edit-address' ) =>
When the endpoint page for edit address is being displayed.
is_wc_endpoint_url( 'lost-password' ) =>
When the endpoint page for lost password is being displayed.
is_wc_endpoint_url( 'customer-logout' ) =>
When the endpoint page for customer logout is being displayed.
is_wc_endpoint_url( 'add-payment-method' ) =>
When the endpoint page for add payment method is being displayed.
Actually I found out this condition that seems to work fine in order to detect the WC Dashboard page with native WC code only:
if (is_user_logged_in() && is_account_page() && !is_wc_endpoint_url()) {
echo 'WC Dashboard';
} else {
echo 'no WC Dashboard';
}
<?php if(is_page("account") && !is_wc_endpoint_url()) { ?>
Assuming your account page is at /account/, this will detect your dashboard.
If you were to only do is_page("account"), the conditional would trigger for all account pages. However, because the dashboard isn't considered a WC endpoint like 'view-order' or 'last-password' is, this simple check will do the job.
I also needed to identify the dashboard specifically and found this question but I didn't like any of the answers and WooCommerce still has no built-in tag to do this...
I have 2 issues with the answers, the first is using is_wc_endpoint_url() (not infallible) and the second is comparing URLs (personal taste I guess?)
is_wc_endpoint_url() can return false for endpoints which are
added by a theme or plugin so you can get a false negative. There's
a filter so you can add your own but you can't trust that a plugin
will do that.
Comparing URLs feels hacky to me. You can probably get
trustworthy results and it's pretty straightforward, so it's not
necessarily a bad way to do it. Although I would forgo hardcoding parts of the URL(s) or at least allow for translating.
Now if you think about it, WooCommerce itself knows without fail when to load dashboard.php so I just took that code and refactored it to simply identify the dashboard:
function is_dashboard(){
global $wp;
if( ! empty( $wp->query_vars ) ){
foreach ( $wp->query_vars as $key => $value ) {
// Ignore pagename param.
if ( 'pagename' === $key ) {
continue;
}
if ( has_action( 'woocommerce_account_' . $key . '_endpoint' ) ) {
return false;
}
}
}
return true;
}
For me, this is the most foolproof way to do it, although if done correctly, comparing URLs might be sufficient. You just can't trust is_wc_endpoint_url() for this issue.
Hope this helps anyone still looking for an is_dashboard() or is_account_page('dashboard')
I want to create a route in my wordpress plugin that isn't linked to a page but to an action that sends an email. So i would send a get request like this
example.com/send/email?email=test#test.co.uk
and this would link to an action with the email as a parameter. I'm very new to Wordpress so forgive me if this is a stupid question but I'm really struggling to achieve this or evebn find a good starting point, can anyone help?
A good option in your case would be to use a rewrite endpoint. A rewrite endpoint allows you to add extra query parameters to certain URLs. For instance you can add a gallery endpoint to all posts, that could render a different template showing all images for the given post. More information about add_rewrite_endpoint() can be seen in the Codex.
Below is some code that adds a send endpoint to EP_ROOT(the home page of the site). Note that you'll have to go to Settings > Permalinks after adding this code in order for the rewrite endpoint to start working.
Once we have the rewrite endpoint in place, we hook to the template_redirect action in order to check for the presence of the send query var. If send is not present, then we do nothing.
If send is present, but empty(like for instance if you load the page http://example.com/send/), then we redirect to the home page.
Otherwise we split send into multiple parts at every / and assign that to the $send_parts variable. We then use a switch statement to see what the $send_action(the first part after /send/) and act accordingly.
Right now we're only checking for the email action(if it's not email, we redirect to the home page again). We check if there's an actual email($send_parts[1]) and whether it's a valid email(I have to note that is_email() is not RFC compliant and it might reject valid email addresses, so use with caution). If it's a valid email, we use wp_mail() to send the email, otherwise we redirect to the home page.
Now, since I don't know how you're planning to implement this my code doesn't cover things like authentication(who can send emails - if it's everyone, I can abuse your site and spam users and get your mail server blacklisted - bad :( ), generation of the email Subject and Message(is it going to be dynamic via $_POST variables, is it going to be pre-defined, etc.). Those are specifics that you will have to implement on your own.
Once the code below is placed in an appropriate file(a .php file that gets loaded in the current theme, or an active plugin's file) and you regenerate your Rewrite Rules(by going to Settings > Permalinks), you can go to http://example.com/send/email/your.email#example.com/ and you should receive an email with a subject "Hello" and a message "This is a message".
function so_34002145_add_email_endpoint() {
add_rewrite_endpoint( 'send', EP_ROOT );
}
add_action( 'init', 'so_34002145_add_email_endpoint', 10 );
function so_34002145_handle_send_email() {
$send = get_query_var( 'send', null );
// This is not a /send/ URL
if ( is_null( $send ) ) {
return;
}
// We're missing an action, the URL is /send/
// Take the user to the home page, since this is an incomplete request.
if ( empty( $send ) ) {
wp_redirect( home_url( '/' ) );
exit;
}
$send_parts = explode( '/', $send );
$send_action = $send_parts[0];
switch ( $send_action ) {
case 'email':
$email = ! empty( $send_parts[1] ) ? $send_parts[1] : false;
if ( ! $email || ! is_email( $email ) ) {
// A missing or invalid email address, abort
wp_redirect( home_url( '/' ) );
exit;
}
wp_mail( $email, 'Hello', 'This is a message' );
break;
default:
// This is an unknown action, send to the home page
wp_redirect( home_url( '/' ) );
exit;
break;
}
}
add_action( 'template_redirect', 'so_34002145_handle_send_email', 10 );
I am creating something for a client and I have a Class that I created with a Custom Post Type called 'PuSH Feeds' and when the user adds a new post and publishes it they can then click on one of two buttons that I have in the Custom Meta Box.
One button is for 'Subscribe' and the other for 'Unsubscribe'. I am using the save_post action hook and testing if the $_POST global has that 'pushfeed-subscribe' or 'pushfeed-unsubscribe' and then do what I need to do. However for some reason I have found that once I click on the subscribe on my local machine stops the script because it says it did 100 consecutive calls etc and I end up with loads of duplicate posts with no title.
What would be the best way to avoid this and is there a better hook I can use for these special custom actions I want to activate of subscribing to a feed (which goes into another class and performs the subscribe method)?
This is the markup I have for those two buttons I mentioned with is inside the Metabox
<input type="submit" class="button-secondary" name="pushfeed-subscribe" id="pushfeed-subscribe" value="Subscribe">
<input type="submit" class="button-secondary" name="pushfeed-unsubscribe" id="pushfeed-unsubscribe" value="Unsubscribe">
Then I have this for the action hook:
add_action( 'save_post', array( $this, 'pushfeed_save_post_meta' ) );
The actual hook is like this:
public function pushfeed_save_post_meta( $post_id ) {
// Bail if we're doing an auto save
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['pushfeed-nonce-field'] ) || !wp_verify_nonce( $_POST['pushfeed-nonce-field'], basename( __FILE__ ) ) ) return;
// If Subsctiption ID is empty, generate a random long number and save it
if ( empty( $_POST['pushfeed-subscription-id'] ) ) {
$random_number = substr(number_format(time() * mt_rand(),0,'',''),0,10);
$pushfeed_subscription_id = $random_number . $post_id;
update_post_meta( $post_id, 'pushfeed-subscription-id', $pushfeed_subscription_id );
}
...
if ( isset( $_POST['pushfeed-subscribe'] ) || isset( $_POST['pushfeed-unsubscribe'] ) ) {
$subscription_domain = get_post_meta($post_id, 'pushfeed-domain', true);
$subscription_id = get_post_meta($post_id, 'pushfeed-subscription-id', true);
$subscription_feed_url = get_post_meta($post_id, 'pushfeed-feed-url', true);
$subscription_callback_url = $subscription_domain . '/pushfeed/' . $subscription_id;
$sub = PuSHSubscriber::instance($subscription_domain, $subscription_id, 'PuSHSubscription', new PuSHEnvironment());
if ( isset( $_POST['pushfeed-subscribe'] ) ) {
$sub->subscribe($subscription_feed_url, $subscription_callback_url);
} elseif ( isset( $_POST['pushfeed-unsubscribe'] ) ) {
$sub->unsubscribe($subscription_feed_url, $subscription_callback_url);
}
}
}
I am trying to find out why is it that the post is saving multiple duplicates with no title.
But above all I would like to know if there is a better action hook I can call for these two custom actions.
Update :
Hi everyone. I ended up using an Ajax request using the wordpress admin-ajax.php when clicking a button and then firing of the subscription method. Once that is done the subscription method will do a get request and if returns a 200 code then the method returns true to the Ajax.
The problem is probably caused by your use of a submit button.
Custom Meta Boxes are not intended to include submit buttons. The idea is that they contain form fields that get submitted when you click on the standard "Update" button. You can then save what is submitted in the save_post action hook.
Using a submit other than "Update" may be confusing WordPress and causing your problem.
I suggest that you change your Custom Meta Box to have a checkbox for Subscribe or a radio button for Subscribe/Unsubscribe that you can look at in the action hook.