Change default display name to username in Wordpress - php

I am building a WP site with membership and forum plugins.
Upon registration people select their username, however the default display name is their First + Last name. This is not ideal, I need the display name to be the same as username. I added this code but doesn't seem to work:
function change_display_name( $user_id ) {
$info = get_userdata( $user_id );
$args = array(
‘ID’ => $user_id,
‘display_name’ => $info->username
);
wp_update_user( $args );
}
add_action(‘user_register’,’change_display_name’);
Any help greatly appreciated!

You can use
wp_insert_user to register a user.
See this link

Related

WordPress - how to check user meta at login

I'm writing a custom wordpress plugin to manage user registrations. I have created two custom roles, that are named customer and merchant, the merchant role users are created from the admin of the site. I need to assig to each merchant account a custom meta to identify it, but from the wp user admin panel this is not possible.
In the registration form for customers I have a field that will be saved inside the user meta table and will contain a code that will be selected from the user from a dropdown menù and is used to identify the merchants.
<?php
wp_insert_user(
array(
'meta_input' => array('merchant_code' => '001'), // this code is passed from the frontend
'role' => 'customer'
)
);
?>
I'm not sure if is possible with a flter or an hook, but what I need is to check for this code when the merchant users will login, this because I will show to each merchant a different customers list. Any suggestion about?
You can use the user_register action hook to intercept the user registration.
We then generate a pseudo random identifier based on the user registration timestamp and the user id.
We retrieve the userdata through get_userdata() and update them through wp_update_user() (which fetch his arguments from wp_insert_user()).
The following is untested but should work:
<?php
add_action( 'user_register', function ( $user_id ) {
$user = get_userdata( $user_id );
$pseudo_random_identifier = null;
$pseudo_random_identifier .= $user_id;
$pseudo_random_identifier .= str_replace( array( '-', ' ', ':', ), '', $user->user_registered );
$pseudo_random_identifier .= sanitize_title_with_dashes( $pseudo_random_identifier );
$userdata = array (
'ID' => $user_id,
'meta_input' => array(
'merchant_code' => $pseudo_random_identifier,
),
);
wp_update_user( $userdata );
} );
Tho for your information, you can change the user role too.

How do I update custom user meta field at registration with a variable from registration page template in WordPress?

I've created a front end registration form in a separate page template. I've also created a function that redirects a user to a different location after registration. Also ran a test of updating a custom meta from the same function (worked). But for some reason, when trying to pull a value from array that user is being registered with, it doesn't update that meta field. User name, email, and password go through. Variable from inside the function updates custom meta. Value from array in the template, doesn't post. Where am I going wrong with it?
Registration page template:
$user_id = wp_insert_user(
array (
'user_login' => $username,
'user_pass' => $password,
'user_email' => $email,
'gender' => $gendertest
)
);
Functions.php
function auto_login_new_user( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
$testid = "testingid";
$gendertest = $user_id["gender"];
update_user_meta( $user_id, 'gender', $gendertest );
update_user_meta( $user_id, 'clickid', $testid );
wp_redirect( home_url('/welcome') );
exit;
}
add_action( 'user_register', 'auto_login_new_user' );
Thanks in advance!

WordPress - Only Display Posts That Are Associated To Logged In User

In my WordPress Site, I have custom post types setup for Tanks, Locations, and Users using the PODS plugin. Currently I have some PHP that allows me to only show the logged in user a post, if they are listed as the author of it. Which is a good start, but not exactly how I need it to work. Because of the way WordPress stores their Author field, I am only able to have one User associated per Tank/Location instead of many. My question is, how would I go about making it so that WordPress will check to see what Tanks/Locations the current logged in user has associated to them, and only display those? In both the Tanks and Locations PODS, I have already setup a relationship field to my Users POD which allows me to assign each Tank/Location with as many users as necessary. All I need now is to figure out how display that information. If it makes any difference, the storage type for all my PODS is Table Based.
So in case anyone has the same question I did, here is how I ended up solving my issue. The variable 'users.ID' is referring to the relationship field I had in each of my Locations/Tanks.
if(!current_user_can('administrator')){
add_shortcode( 'pods_by_current_user_cpt', 'pods_by_current_user_cpt' );
function pods_by_current_user_cpt( $atts, $content = null ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
$atts['where'] = 'users.ID = ' . (int) $user_id;
return pods_shortcode( $atts, $content );
}
}
else{
add_shortcode( 'pods_by_current_user_cpt', 'pods_by_current_user_cpt' );
function pods_by_current_user_cpt( $atts, $content = null ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
return pods_shortcode( $atts, $content );
}
}

Can I create a function for auto-enrolling WP Roles in a Learndash course?

Currently, LearnDash has a function (to be added to functions.php) that allows you to auto enroll a specific user in a course. I was wondering if a simple function could be added to my theme's function file and change this from user_id to a user ROLE? That way every user in that role is enrolled.
Here is the starting point: (found in the dev section on Learndash)
//* To enroll user to course:
ld_update_course_access($user_id, $course_id, $remove = false);
I have tried this:
//* Add users to course by role
ld_update_course_access($role_name = Subscriber, $course_id = 949, $remove = false);
On the "edit course" page editor I now see "1,0,12,Subscriber" inside the "course access list" but it doesn't actually work. Obviously, that access list is working with users only.
My thought process is creating a function that will:
1) Get user IDs from user role "My-Custom-Role"
2) Return IDs and update course access.
Is something like this possible?
Yep, totally possible. The get_users() function allows you to get a list of users by role. See: https://codex.wordpress.org/Function_Reference/get_users
For example:
$users = get_users( [ 'role__in' => [ 'subscriber', 'author' ] ] );
foreach ( $users as $user ) {
ld_update_course_access( $user->ID, 949, false );
}
I worked with the development team and came up with a different although incomplete solution, so I've marked Linnea's as correct, because it works as asked in the question. This solution goes through their access hook sfwd_lms_has_access, however the "course list" never gets updated so a user is not officially "enrolled" until they start the course. By this I mean, you wont see them enrolled in the course on their profile, but if they start a lesson, it all of a sudden shows up! Thought it might help to post here in case it may help anyone as a starting point.
add_filter( 'sfwd_lms_has_access', function( $return, $post_id, $user_id ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
if(empty($user_id))
return $return;
$course_id = learndash_get_course_id( $post_id );
$allowed_course_ids = array( 949, 1135 );
if( !in_array($course_id, $allowed_course_ids))
return $return;
if(user_can($user_id, "3a_textbook"))
return true;
if(user_can($user_id, "subscriber"))
return true;
return $return;
}, 10, 3 );

Wordpress Contact Form 7 Database Unique ID

I need to create a payment form with redirect to Chronopay payment gate on wordpress. I try to make it with CF7 plugin with database extension.
How can I do next things:
1. Add unique ID and payment status to CF7 database strings. (I have added 2 fields to database and tried to change them with hook on wpcf7_before_send_mail).
function order_set_id($f) {
global $wpdb;
global $table_prefix;
$submit_time = $f->submit_time;
$title = $f->title;
if ('Taxy pick up' == $title ) {
$sql = "SELECT MAX(order_id) as mid FROM `wp_cf7dbplugin_submits`";
$var = $wpdb->get_results($wpdb->prepare($sql));
$neworder_id = $var[0]->mid;
$wpdb->update( 'wp_cf7dbplugin_submits',
array( 'order_id' => 0, 'order_payment' => 0 ),
array( 'submit_time' => $submit_time ),
array( '%s', '%d' ),
array( '%d' )
);
}}
add_action( 'wpcf7_before_send_mail', 'order_set_id');
But it doesn't work.
I need to display a hidden form as payment invoice after submittin a form. How can I do it?
How can I block sending email messages in Contact Form 7?
In the nearly past I wanted something similar. Found this two methods:
http://runastartup.com/integrate-paypal-button-with-contact-form-in-wordpress/
http://www.gobdg.com/blog/code-library/donation-form-using-contact-form-7-paypal/
Idea is on hold for now, but asap I will have free time, will decide which method to use.
Good luck! :)

Categories