My goal is to import a csv file with houses for sale and generate realtors as wordpress/buddypress user if they don't exist yet. I want to do so by using a custom function in WP all import. Addtitionally i would like to set the buddypress membertype to "realtor" for internal user
I will map the source field to a custom field in the import config
[create_realtor{realtor_column}]
in my case that is [create_realtor{properties[1]/property[2]/value[1]}]
As it is a user for internal purposes, i don't need the actual realtor contact details, so the user email will be realtor#mydomain.com, pass can generated, email to realtor not needed.
I found that a user can be generated as below source:
if( null == username_exists( $email_address ) ) {
// Generate the password and create the user
$password = wp_generate_password( 12, false );
$user_id = wp_create_user( $email_address, $password, $email_address );
// Set the nickname
wp_update_user(
array(
'ID' => $user_id,
'nickname' => $email_address
)
);
// Set the role
$user = new WP_User( $user_id );
$user->set_role( 'subscriber' );
} // end if
How can i wrap all in a function that will generate the user based on the realtor name in my feed
would it be like below?
function create_realtor($realtor) {
$emailadress = $realtor . '#mydomain.com' ;
if( null == username_exists( $email_address ) ) {
// Generate the password and create the user
$password = wp_generate_password( 12, false );
$user_id = wp_create_user( $email_address, $password, $email_address );
// Set the nickname
wp_update_user(
array(
'ID' => $user_id,
'nicename' => $nicename, //realtor
'nickname' => $email_address //realtor#mydomain.com
)
);
// Set the role
$user = new WP_User( $user_id );
$user->set_role( 'subscriber' );
// Set the member type of user to 'realtor'.
$member_type = bp_set_member_type( $user_id, 'realtor' );
} // end if
}
I tried above, but it results in a message saying "Custom Field Value template is invalid: Unexpected token XPATH, statement was expected."
I am a bit lost here on how to procede
You issue is that you're not calling your custom function correctly. The function call should have round brackets like this:
[create_realtor({properties[1]/property[2]/value[1]})]
Always start simple:
Read through Example 2 below
Get a simple function working first
Then build on it and insert your custom logic
https://www.wpallimport.com/documentation/developers/custom-code/inline-php/
Related
I am trying to log in a user programmatically in bbpress, I tried logging the user in in wordpress with the following code snippet:
$username = "Carlos";
$user = get_user_by('login', $username );
$user_id = $user->ID;
clean_user_cache($user->ID);
wp_clear_auth_cookie();
wp_set_current_user( $user_id, $user->user_login );
wp_set_auth_cookie( $user_id, true, true );
update_user_caches( $user );
This seems to work, when I echo is_user_logged_in() it returns true. But the user is not logged in within the bbpress forum.
I am trying to send a custom WooCommerce mail with user credentials after an order with certain product. To explain further: Someone buys a product
I have to check if the order contains a certain product id
I have to check if the customer is registered as a user already, if not it should create a user
Then it should send a custom mail with those credentials to the customer
I am struggling to get the following code to work. Especially I dont know how I can add my custom email-message with the credentials and a custom login link. Do you know a solution?
add_action( 'woocommerce_thankyou', 'check_order_product_id' );
function check_order_product_id( $order_id ){
$order = wc_get_order( $order_id );
$items = $order->get_items();
$order_email = $order->billing_email;
//create custom mail
function get_custom_email_html( $order, $heading = false, $mailer ) {
$template = 'emails/my-custom-email-i-want-to-send.php';
return wc_get_template_html( $template, array(
'order' => $order,
'email_heading' => $heading,
'sent_to_admin' => false,
'plain_text' => false,
'email' => $mailer
) );
}
// load the mailer class
$mailer = WC()->mailer();
//format the email
$recipient = $order_email;
$subject = __("Deine Login-Daten für Geomap", 'theme_name');
$content = get_custom_email_html( $order, $subject, $mailer );
$headers = "Content-Type: text/html\r\n";
//send the email through wordpress
$mailer->send( $recipient, $subject, $content, $headers );
foreach ( $items as $item_id => $item ) {
$product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
if ( $product_id === XYZ ) {
//get the user email from the order and check if registered already
function email_exists( $order_email ) {
$user = get_user_by( 'email', $order_email );
if ( $user ) {
return $wp_new_user_notification_email;
}
else {
// random password with 12 chars
$random_password = wp_generate_password();
// create new user with email as username & newly created pw
$user_id = wp_create_user( $order_email, $random_password, $order_email );
return $wp_new_user_notification_email;
}
}
}
}
}
Because you mentioned low barrier to entry being very important...
I'd recommend linking to your thank you page and having the registration link there too. You can use SSO (Google/Amazon/etc.) options so that users don't need to register directly through WordPress.
I'm assuming the product ID has some kind of activation code? You can send that, encrypted, as a parameter to the registration page so that it's registered in their session. Once logged in, you have attached their activation code to the user account, and you're done.
Steps:
Customer buys
If they have an account: send them the link, and they're done.
If they don't:
a. Send them an email with a link with the encrypted activation code.
b. They will have to register (through SSO or through WordPress directly).
c. Once registered, you attach their product to their user account, and redirect them to the page they care about.
Hey for the question "how I can add my custom email-message with the credentials and a custom login link",
In your code, there is this line
$template = 'emails/my-custom-email-i-want-to-send.php';
you could add the credentials there, you may pass other arguments as the second parameter of this functionwc_get_template_html
second, the code order will not work for what you are trying to achieve
in this line, you are sending the email
$mailer->send( $recipient, $subject, $content, $headers );
And after that, there is a foreach loop for all the items in the order and creates a function looking if the user exists.
foreach ( $items as $item_id => $item ) {
$product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
if ( $product_id === XYZ ) {
//get the user email from the order and check if registered already
function email_exists( $order_email ) {
This function is not been called.
I will suggest to take out that function from the foreach loop and call it inside if your goal is to check only if the product XYZ exists
and all of it before the email
function check_order_product_id( $order_id ){
$order = wc_get_order( $order_id );
$items = $order->get_items();
$order_email = $order->billing_email;
//create custom mail
function get_custom_email_html( $order, $heading = false, $mailer ) {
$template = 'emails/my-custom-email-i-want-to-send.php';
return wc_get_template_html( $template, array(
'order' => $order,
'email_heading' => $heading,
'sent_to_admin' => false,
'plain_text' => false,
'email' => $mailer
) );
}
function send_custom_email($order_email, $order){
// load the mailer class
$mailer = WC()->mailer();
//format the email
$recipient = $order_email;
$subject = __("Deine Login-Daten für Geomap", 'theme_name');
$content = get_custom_email_html( $order, $subject, $mailer );
$headers = "Content-Type: text/html\r\n";
//send the email through wordpress
$mailer->send( $recipient, $subject, $content, $headers );
}
function email_exists( $order_email ) {
$user = get_user_by( 'email', $order_email );
if ( $user ) {
send_custom_email($order_email, $order);
//not sure where this variable was comming from
return $wp_new_user_notification_email;
}
else {
// random password with 12 chars
$random_password = wp_generate_password();
// create new user with email as username & newly created pw
$user_id = wp_create_user( $order_email, $random_password, $order_email );
//not sure where this variable was comming from
return $wp_new_user_notification_email;
}
}
foreach ( $items as $item_id => $item ) {
$product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
if ( $product_id === XYZ ) {
//get the user email from the order and check if registered already
email_exists( $order_email );
}
}
}
Now as a conclusion, WP_user class doesn't give access to the password for security reasons, and they don't send it by email, you should not either is a very bad practice.
I will suggest you create the account and let WP send the default email and then you send another email with the link to your custom page, but not with the credentials.
That way they will receive two emails one secure and one with the special conditions.
in Wordpress I have installed an app-builder plugin that allows me to send push notifications to the app, It automatically sends push notifications when a email is sent from the website to the email address of the current user that is logged into the app and the plugin allows me to send custom push notifications to different users roles manually - this is fine.
But the problem is - I want to be able to send automatic push notifications to the 'driver' user role every time a new Woocommerce order is received.
Warning - I am a novice (clearly).
The plugin developer provided me with the function that sends the push notification, which is:
wpmobileapp_push($title, $message, $image_url, $page_url, $lang_2letters = 'all', $send_timestamp = '', $user_email = '');
And Im using woocommerce_thankyou so the function runs everytime a customer gets to the 'thank you' page.
So after a bit of investigating I have come up with the following function (which was added into my 'function.php) which 'should' check if a 'driver' user is logged in and should call the php function which sends the push notification to the drivers every time a new woocommerce is sent, but this does not work:
/**
* Add a notification when a new woocommerce order is recieved.
*
*/
add_action('woocommerce_thankyou', 'wpmobileapp_woo_order', 10, 1 );
function wpmobileapp_woo_order($order_id) {
// Check if user is logged in.
if ( is_user_logged_in() ) {
// Get the user ID.
$user_id = get_current_user_id();
// Get the user object.
$user_meta = get_userdata( $user_id );
// If user_id doesn't equal zero.
if ( 0 != $user_id ) {
// Get all the user roles as an array.
$user_roles = $user_meta->roles;
// Check if the role you're interested in, is present in the array.
if ( in_array( 'driver', $user_roles, true ) ) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
$customer_address = $order->get_billing_address();
$user_email = $user_meta->user_email;
$image_url = '';
$link = '';
$title = "new order";
$message = $order . ', ' . $items . ', ' . $customer_address;
wpmobileapp_push($title, $message , $image_url, $link, $lang_2letters = 'all', $send_timestamp = '', $user_email);
}
}
}
}
I have tried many different things to try and make this work myself to get it to send automatic notifications to the driver use role type every-time a new order is placed, but nothing works. Some help would be greatly appreciated.
Okay, the best simple way of handling this is to send a push notification to all logged in driver users. Basically, query all users that have a running session in WordPress with the driver role. Then, iterate over these attempting to send everyone of them a notification. Change the function like so:
/**
* Add a notification when a new woocommerce order is recieved.
*
*/
add_action('woocommerce_thankyou', 'wpmobileapp_woo_order', 10, 1 );
function wpmobileapp_woo_order($order_id) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
$customer_address = $order->get_billing_address();
$user_email = $user_meta->user_email;
$image_url = '';
$link = '';
$title = "new order";
$message = $order . ', ' . $items . ', ' . $customer_address;
// get all users with role 'driver' and an active WP session:
$drivers = get_users([
'role' => 'driver',
'meta_key' => 'session_tokens',
'meta_compare' => 'EXISTS'
]);
// notify each of these by push notification
foreach ($drivers as $driver) {
$driver_email = $driver->user_email;
wpmobileapp_push($title, $message , $image_url, $link, $lang_2letters = 'all', $send_timestamp = '', $driver_email);
error_log("WooCommerce Driver notification: sending push notification to account of $driver_email");
}
}
You can enable WP_DEBUG and WP_DEBUG_LOG and check which driver account should've gotten a push notification. If a log message exists but your test driver user didn't get a notification, probably you need to test this wpmobileapp_push stuff further and contact the developer of https://wordpress.org/plugins/wpappninja/ if it doesn't seem to be working at all.
The thing is: this function only inserts an entry in the plugin's database table. I'm not entirely sure it sends the push notification right away. That's why I said you might've to talk to the plugin developer.
I'm using the strip connect api with wordpress, and I've managed to set up the flow to connect a user, and get their access token and userid. However, I need to save this in a way that's connected to the wordpress user, as I need to be able to come back and reference the code later, in order to allow the correct person to be paid. I wrote added a couple lines with add_user_meta() but my var_dump() comes out null. here's the some of the code:
global $wpdb;
$table = $wpdb->prefix."stripe_connect";
$wpdb->insert($table , array(
'time' => current_time('mysql'),
'access_token' => $token,
'stripe_publishable_key' => $key,
'stripe_user_id' => $userid)
);
$user_ID = get_current_user_id();
add_user_meta($user_ID, ‘stripe_userid’, $userid, $unique);
add_user_meta($user_ID, ‘stripe_token’, $token, $unique);
$stripeuserid = get_user_meta($user_ID, 'stripe_userid', true);
var_dump($stripeuserid);
$response = '<h4>Thank you for connecting with Stripe. This information has been saved in the database and can be viewed in the Admin Panel.</h4>';
I figured it out, it had nothing to do with the way I was adding the data. The problem was with retrieving it.
This fixed it:
$user_ID = get_current_user_id();
update_user_meta($user_ID, ‘stripe_userid’, maybe_serialize( $userid ));
add_user_meta($user_ID, ‘stripe_token’, $token, $unique);
$stripeuserid = get_user_meta($user_ID, 'stripe_userid');
var_dump($stripeuserid);
I've followed this tutorial to implement mailchimp API v2.0 on my website.
It works great, however the double_optin option that I wanted to add and set to false (so that users don't need to validate their registration via e-mail) doesn't seem to be working. It's like if it was not taken into consideration at all, users still need to validate the registration via e-mail.
Is 'double_optin' => false placed at the wrong place? I had a look at mailchimp documentation but my level in programmation is not good enough to identify what is wrong.
Thanks for your help
<?php
$api_key = "12345486-us8";
$list_id = "123";
require('Mailchimp.php');
$Mailchimp = new Mailchimp( $api_key );
$Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
$subscriber = $Mailchimp_Lists->subscribe( $list_id, array( 'email' => htmlentities($_POST['email']),'double_optin' => false ) );
if ( ! empty( $subscriber['leid'] ) ) {
echo "success";
}
else
{
echo "fail";
}
?>
According to this (admittedly unofficial-looking) Mailchip_Lists documentation, you'll want to pass FALSE as the 5th parameter to subscribe().
Example:
$double_optin = FALSE;
$subscriber = $Mailchimp_Lists->subscribe(
$list_id,
array('email' => htmlentities($_POST['email'])),
NULL, // merge_vars
'html', // email_type
$double_optin
);