Wrong username and displayname after user registraton WooCommerce - wordpress

When people are buying a product through the WooCommerce extension, I have enabled that they can create an account on checkout. I don't let them choose their own username and password.
Now I see that WP uses some part of the emailaddress to create a username, and the display name is based on the first name filled in on checkout.
Now I want to change that:
username must be the complete emailaddress [SOLVED];
display name must be the first name + last name [UNSOLVED].
I tried this: for display name with my 'no knowledge' of PHP:
add_filter('pre_user_display_name', 'wsis_pre_user_display_name');
function wsis_pre_user_display_name($display_name) {
$first = get_user_field("billing_first_name");
$last = get_user_field("billing_last_name");
$display_name = $first . $last;
return $display_name;
}
This filter is mentioned in their codex: http://codex.wordpress.org/Function_Reference/wp_update_user. But no examples and my code didn't work. Anybody can help?
In user.php wp-includes folder I found the filter, now get it working :-).
/**
* Filter a user's display name before the user is created or updated.
*
* #since 2.0.3
*
* #param string $display_name The user's display name.
*/
$display_name = apply_filters( 'pre_user_display_name', $display_name );
if ( empty($description) )
$description = '';
Second try did something! It left the display name completely empty:
add_filter('pre_user_display_name', 'pre_user_display_name');
function pre_user_display_name($display_name) {
$first = get_the_author_meta('first_name');
$last = get_the_author_meta('last_name');
$display_name = $first . $last;
return $display_name;
}
Am I on the right track?

To set the username as complete email address, you can add following code in functions.php file of your theme.
add_filter('woocommerce_new_customer_data','change_username');
function change_username($user_data)
{
return array(
'user_login' => $user_data['user_email'],
'user_pass' => $user_data['user_pass'],
'user_email' => $user_data['user_email'],
'role' => 'customer'
);
}

Here is how to set the display name to first name + last name.
// Sets the display name to first name and last name
add_filter( 'pre_user_display_name' , 'default_display_name' );
function default_display_name($name) {
if ( isset( $_POST['billing_first_name'] ) ) {
$firstname = sanitize_text_field( $_POST['billing_first_name'] );
}
if ( isset( $_POST['billing_last_name'] ) ) {
$lastname = sanitize_text_field( $_POST['billing_last_name'] );
}
$name = $firstname . ' ' . $lastname;
return $name;
}
Source : http://geektamin.com/blog/533/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead/

Related

WooCommerce - register second user in woocommerce_created_customer hook

I'm coming across an issue when trying to register a second user account once someone registers as a customer via WooCommerce. I have added the following woocommerce_created_customer hook:
add_action('woocommerce_created_customer', function($customer_id)
{
if(isset($_POST['second_user_first_name']) && isset($_POST['second_user_last_name']))
{
$createSecondUserId = wp_create_user(strtolower($_POST['second_user_first_name'].'-'.$_POST['second_user_last_name']).'-'.$customer_id, wp_generate_password(), 'test#test.com');
if(is_wp_error($createSecondUserId))
{
$errors = $createSecondUserId->errors;
print_r($errors);
die();
}
}
});
However I get the following error when submitting a new WooCommerce registration:
Array ( [existing_user_login] => Array ( [0] => Sorry, that username already exists! ) )
It's strange as I'm setting a random username within the wp_create_user function, so the usernames should not clash. Has anyone got any ideas?
You can use username_exists() to determines that the given username exists.
add_action( 'woocommerce_created_customer', function($customer_id){
if( isset( $_POST['second_user_first_name'] ) && isset( $_POST['second_user_last_name'] ) ) {
if( !username_exists( strtolower( $_POST['second_user_first_name'].'-'.$_POST['second_user_last_name'] ).'-'.$customer_id ) ){
$createSecondUserId = wp_create_user( strtolower( $_POST['second_user_first_name'].'-'.$_POST['second_user_last_name'] ).'-'.$customer_id, wp_generate_password(), 'test#test.com' );
if(is_wp_error($createSecondUserId)){
$errors = $createSecondUserId->errors;
print_r($errors);
die();
}
}
}
});
If the username already exists you can create a new one by adding a progressive numeric suffix. In this way you will be sure that the username of the second account will always be unique.
Note that if you run multiple tests with your current code you need to
make sure you remove the user with the email address test#test.com
otherwise you will get an error: [existing_user_email] => Sorry, that email address is already used!.
add_action('woocommerce_created_customer', function( $customer_id ) {
if ( isset($_POST['second_user_first_name']) && isset($_POST['second_user_last_name']) ) {
// create the username based on the form data
$username = strtolower( $_POST['second_user_first_name'] . '-' . $_POST['second_user_last_name'] ) . '-' . $customer_id;
// if the username already exists it creates a unique one
if ( username_exists($username) ) {
$i = 0;
while ( username_exists($username) ) {
$username = $username . '-' . ++$i;
}
}
// create the second user
$createSecondUserId = wp_create_user( $username, wp_generate_password(), 'test#test.com' );
}
});
The code has been tested and works. Add it to your active theme's functions.php.

How we can change the name of a PDF in ninja forms by using any field value from form submitted

How we can change the PDF name in Ninja forms by the field value from form which is submitted
With the new version of ninja-forms:
function custom_pdf_name( $name, $sub_id ) {
$sub = Ninja_Forms()->form()->get_sub( $sub_id );
$name = $sub->get_field_value('title_1542019981951') . $sub_id;
return $name;
}
add_filter( 'ninja_forms_submission_pdf_name', 'custom_pdf_name', 20, 2 );
function custom_pdf_name( $name, $sub_id ) {
global $ninja_forms_processing;
$form_id= $ninja_forms_processing->get_form_ID();
$value1 = $ninja_forms_processing->get_field_value( 110 );//110 is id of the field we want to attach with the name of PDF
if($form_id=='11'){ //11 is id of the form submitted
$name = 'Form Name -' . $value1;
}
return $name;
}
add_filter( 'ninja_forms_submission_pdf_name', 'custom_pdf_name', 20, 2 );

WooCommerce taxonomy slug change

For some reason after the latest update of WooCommerce, my taxonomy slugs have changed.
They've gone from:
www.mysite.com/genre/dance to www.mysite.com/pa_genre/dance
It may be that in the backend nothing has changed, but the visualization on the frontend has. This sudden change in URL's is affecting not only how my site is linked internally, but also externally and if not fixed rapidly Google will also pick up these 404 pages.
I've looked into WooCommerce files, not sure where to look, but wc-attribute-functions.php shows the following containing references to the 'pa_' bit.
If anyone has an idea to go back to my old URL structure, that would be greatly appreciated.
/**
* Get a product attributes name.
*
* #param mixed $name
* #return string
*/
function wc_attribute_taxonomy_name( $name ) {
return 'pa_' . wc_sanitize_taxonomy_name( $name );
}
/**
* Get a product attributes label.
*
* #param mixed $name
* #return string
*/
function wc_attribute_label( $name ) {
global $wpdb;
if ( taxonomy_is_product_attribute( $name ) ) {
$name = wc_sanitize_taxonomy_name( str_replace( 'pa_', '', $name ) );
$label = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_label FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name ) );
if ( ! $label ) {
$label = ucfirst( $name );
}
} else {
$label = ucwords( str_replace( '-', ' ', $name ) );
}
return apply_filters( 'woocommerce_attribute_label', $label, $name );
}
/**
* Get a product attributes orderby setting.
*
* #param mixed $name
* #return string
*/
function wc_attribute_orderby( $name ) {
global $wpdb;
$name = str_replace( 'pa_', '', sanitize_title( $name ) );
$orderby = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_orderby FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name ) );
return apply_filters( 'woocommerce_attribute_orderby', $orderby, $name );
}
I've had the same problem after Woocommerce 2.3.5 update, a quick fix would be adding a rewrite rule for the attributes.
You can add this to functions.php file in your theme:
add_action('init', 'add_genre_url');
function add_genre_url()
{
add_rewrite_rule(
'^genre/([^/]*)$',
'index.php?pa_genre=$matches[1]',
'top'
);
//flush_rewrite_rules();
}
The flush_rewrite_rules() function must be called at least once after you add the rewrite rule, it's not a great idea to leave it on the init action for performance reasons. Maybe someone can clear this out.

Change user profile URL in Buddypress with nickname or userID

I am using this code which is partially working for changing the profile url everywhere in buddypress and wordpress from “http:/mywebsite/user/username” to “http:/mywebsite/user/userid”
function _bp_core_get_user_domain($domain, $user_id, $user_nicename = false, $user_login = false) {
if ( empty( $user_id ) ){
return;
}
if( isset($user_nicename) ){
$user_nicename = bp_core_get_username($user_id);
}
$after_domain = bp_get_members_root_slug() . '/' . $user_id;
$domain = trailingslashit( bp_get_root_domain() . '/' . $after_domain );
$domain = apply_filters( 'bp_core_get_user_domain_pre_cache', $domain, $user_id, $user_nicename, $user_login );
if ( !empty( $domain ) ) {
wp_cache_set( 'bp_user_domain_' . $user_id, $domain, 'bp' );
}
return $domain;
}
add_filter('bp_core_get_user_domain', '_bp_core_get_user_domain', 10, 4);
function _bp_core_get_userid($userid, $username){
if(is_numeric($username)){
$aux = get_userdata( $username );
if( get_userdata( $username ) )
$userid = $username;
}
return $userid;
}
add_filter('bp_core_get_userid', '_bp_core_get_userid', 10, 2);
function _bp_get_activity_parent_content($content){
global $bp;
$user = get_user_by('slug', $bp->displayed_user->fullname); // 'slug' - user_nicename
return preg_replace('/href=\"(.*?)\"/is', 'href="'.bp_core_get_user_domain($user->ID, $bp->displayed_user->fullname).'"', $content);
}
add_filter( 'bp_get_activity_parent_content','_bp_get_activity_parent_content', 10, 1 );
add_filter('bp_core_get_userid_from_nicename', '_bp_core_get_userid', 10, 2);
It is working perfectly for me at the moment BUT not on this little place (see picture):
http://i.imgur.com/4dX0RUB.png
– url change of the author of an activity starting-message is not working in both groups activities and personnal activities
url change of the author of an activity REPLY is working
I don’t know if I am explaining very well what issue I have got but I hope you will understand.
Thank you for your answers
PS : thanks to aSeptik from StackExchange for the code
It's impossible to do that on a fly gracefully. BuddyPress Activity component is developed in such a way, that those text with the user link in activity stream (for site-wide, personal and groups) is stored directly in the database as an action. Just take a look at wp_bp_activity.action field in your DB.
So you should filter and preg_replace it as well. I guess you know that you are penalting yourself with rendering speed loss.

Wordpress user permalink change issue

I'm trying to change the link of the user profile and it seems that I'm too noob.
The current structure of the link is: domain.com/profile/username.
What I want is to be is like: domain.com/username/city where city is taken from wp_postmeta table
I tried something using this function:
add_action('init', 'wpse82004_init');
function wpse82004_init()
{
global $wp_rewrite;
$city = get_user_meta( get_current_user_id(), 'city', TRUE );
$wp_rewrite->author_base = $city;
$wp_rewrite->author_structure = '/%author%' . '/' . $wp_rewrite->author_base;
}
The problem is that is returns the city of the current logged user on all profiles I click.
Any help would be appreciated. Thanks
untested code
add_action('init', 'wpse82004_init');
function wpse82004_init()
{
global $wp_rewrite;
//parse username from url
$url = $_SERVER["REQUEST_URI"];
$username = preg_replace('/^.*profile\/(.*)$/i', '$1', $url);
//get user by username
$user = get_user_by('slug', $username);
//rewrite the city value of anticipated user other than current user
$city = get_user_meta( $user->ID, 'city', TRUE );
$wp_rewrite->author_base = $city;
$wp_rewrite->author_structure = '/%author%' . '/' . $wp_rewrite->author_base;
}

Resources