Hook into the user_change process - wordpress

I have a database with user information from a another project. Now I want to link this user data with my new wordpress installation. I want, if a user is about to login and has no Wordpress profile yet, but a profile is in that other database already, then wordpress needs to create a profile based on the data, that already exists in the other database.
I'm already done with the login and it works fine...
add_filter('authenticate', 'check_login', 40, 3);
function check_login($user, $username, $password) {
$user = get_user_by( 'login', $username );
$hash = kbHash($password);
if ( $user ) {
if (!wp_check_password( $hash, $user->data->user_pass, $user->ID) ) {
$user = NULL;
}
} elseif ($kbCredentials = isKanuboxUser($username)) {
if ($kbCredentials['hash'] == $hash)) {
$user_id = wp_create_user( $kbCredentials['username'], $kbCredentials['hash'], $kbCredentials['mail'] );
$user = get_userdata( $user_id );
} else {
$user = NULL;
}
} else {
$user = NULL;
}
return $user;
}
function kbHash($password) {
//TODO: additional hashing from other project
$hash = $password;
return $hash;
}
Now, the problem is, if some user edits his password, I need to do this hashing too and update it in the original database. Are there appropriate hooks inside wordpress?
I only found this: add_action( 'profile_update', 'some_function' );
But in that function I am not able to add my hashing to the plain text password before the wp-hashing is applied.
How to solve this?

Related

Only view uername

I want to view this code only with USERNAME, however the public display name taking over. how to only display USERNAME?
function display_current_user_display_name () {
$user = wp_get_current_user();
$display_name = $user->user_login;
return $user->display_name;
}
add_shortcode('current_user_display_name', 'display_current_user_display_name');
You are returning $user->display_name;. You need to return $display_name. Try the below code.
function display_current_user_display_name () {
$user = wp_get_current_user();
$display_name = $user->user_login;
return $display_name;
}
add_shortcode('current_user_display_name', 'display_current_user_display_name');
Shorter code:
function display_current_user_display_name () {
$user = wp_get_current_user();
return $user->user_login;
}
add_shortcode('current_user_display_name', 'display_current_user_display_name');

wordpress subscriber with contact form 7

Does anyone know if with contact form 7 it is possible to have a checkbox that when it is activated by the visitor, this visitor is registered as a wordpress user, as a subscriber?
After the comment of Howard E, the question changes to...
How can be used wpcf7_before_send_mail for register a new user with the data of the form?
something like this, but I don't know if it is completely correct...
add_action( 'wpcf7_before_send_mail', 'register_user' );
function register_user($cf7) {
$form_id = $cf7->id();
if ($form_id == 300 || $form_id == 301 || $form_id == 302) {
$submission = WPCF7_Submission :: get_instance();
}
if ($submission) {
if (empty($posted_data)) { return; }
$accept = $posted_data['acceptance-register-yes']; //acceptance check to be registered
if (empty($accept)) { return; }
$email = $posted_data['your-email'];
$name1 = $posted_data['your-name'];
$name2 = $posted_data['your-last-name'];
$name = ''; //here will go function to delete spaces and generate user name from $name1 + $name2
$pass = ''; //here will go function to random password
function create_user($n,$p,$e){
if (!username_exists($n) && !email_exists($e)) {
$user_id = wp_create_user($n, $p, $e);
$user = new WP_User($user_id);
$user->set_role( 'subscriber' );
}
}
create_user($name,$pass,$email);
}
}
many thanks
You can create a user and hook into wpcf7_before_send_mail like this.
function wp_create_custom_account(){
// Get the WPCF7 Submission instance
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$posted_data = $submission->get_posted_data();
// Get the posted variables
$username = isset($posted_data['username'])?$posted_data['username']:'';
$password = isset($posted_data['password'])?$posted_data['password']:'';
$eml = isset($posted_data['eml'])?$posted_data['eml']:'';
// This can be radio or checkbox. Adjust your code accordingly
$radio = isset($posted_data['radio'][0])?$posted_data['radio'][0]:'';
if ($radio) {
$user = $username;
$pass = $password;
$email = eml;
if ( !username_exists( $user ) && !email_exists( $email ) ) {
$user_id = wp_create_user( $user, $pass, $email );
$user = new WP_User( $user_id );
$user->set_role( 'subscriber' );
}
}
}
}
add_action('wpcf7_before_send_mail','wp_create_custom_account');

php - Symfony: Edit information of current user login with another user and reload page show error

I have two accounts one is client account and another is admin account. Client login to his account to edit profile and submit to admin approve. the problem when admin approve his updating then click submit then in client account reload page and have problem with message too many redirection.
Below is my code:
/**
* #Route("/client/personal-information", name="client_personal_info_page")
* #Template("FrontendBundle:ClientPanel:personal_info.html.twig")
*/
public function clientPersonalInfo(Request $request)
{
$user = $this->getUser();
$form = $this->createForm(ChangePersonalInfoFormType::class);
$form->setData($user);
$form->handleRequest($request);
if ($form->isValid()) {
$user = $form->getData();
$idCardPictureFront = $form->get('idCardPictureFront')->getData();
$idCardPictureBack = $form->get('idCardPictureBack')->getData();
$accountDocument = $form->get('accountDocument')->getData();
if ($idCardPictureFront != null) {
$file = new Media($idCardPictureFront);
$this->persistAndFlush($file);
$user->setIdCardPictureFront($file);
}
if ($idCardPictureBack != null) {
$file = new Media($idCardPictureBack);
$this->persistAndFlush($file);
$user->setIdCardPictureBack($file);
}
if ($accountDocument != null) {
$file = new Media($accountDocument);
$this->persistAndFlush($file);
$user->setAccountDocument($file);
}
if ($form->get('otherCountryCode')->getData() != null) {
$phnoneNumber = $form->get('otherCountryCode')->getData().'-'.
$form->get('phoneNumber')->getData();
$user->setPhoneNumber($phnoneNumber);
}
$user->setStatus(AccountStatus::PENDING_OPENING);
$user->setPendingType(PendingType::ACCOUNT_UPDATE);
$this->addLog(
$this->moduleName,
LogAction::UPDATE,
'User update personal information',
$user->getId()
);
$this->msgSuccess('frontend.client.panel.personal_info_updated');
$this->persistAndFlush($user);
return $this->redirect($request->getUri());
}
return $this->getResponseParameters([
'form' => $form->createView(),
'user' => $user,
]);
}

wordpress - how can I block email sending by role?

I have an educational Wordpress site where students have the role 'child' and adults have the role 'subscriber'. I need to prevent emails being sent to the 'child' users (by Woocommerce - but I think they are sent via the mail function in Wordpress).
Is there a line I can add in functions.php to stop mail being sent to specific roles?
Thanks in advance
Maria
I think it might be possible to adjust the email recipients via a filter in the get_recipient() method.
/**
* get_recipient function.
*
* #return string
*/
public function get_recipient() {
return apply_filters( 'woocommerce_email_recipient_' . $this->id, $this->recipient, $this->object );
}
Let's take for example the new order email. Here's it's trigger() method:
/**
* trigger function.
*
* #access public
* #return void
*/
function trigger( $order_id ) {
if ( $order_id ) {
$this->object = wc_get_order( $order_id );
$this->find['order-date'] = '{order_date}';
$this->find['order-number'] = '{order_number}';
$this->replace['order-date'] = date_i18n( wc_date_format(), strtotime( $this->object->order_date ) );
$this->replace['order-number'] = $this->object->get_order_number();
}
if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
return;
}
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}
Specifically
if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
which says if there is no recipient then the email won't send. Also $this->object = wc_get_order( $order_id ); tells us that the $order object is passed to the get_recipient_$id filter.
The id of the new order email is "customer_completed_order" as shown in the class constructor for the email.
SO, putting that all together we can filter the recipient for new order emails:
add_filter( 'so_29896856_block_emails', 'woocommerce_email_recipient_customer_completed_order', 10, 2 );
function so_29896856_block_emails( $recipient, $order ) {
if( isset( $order->customer_user ) ){
$user = new WP_User( $customer_user );
if ( in_array( 'child', (array) $user->roles ) ) {
$recipient = false;
}
}
return $recipient;
}
However, this assumes that the recipient is a single string (if an array it would kill all the recipients and not just the child... though by the default the new order email is sent to the billing email address.
Also, note that I didn't test this at all, so your mileage may vary.

Symfony2 Login: Authenticate a user by 3 criteria

I'm trying to make a symfony2 login that makes a user provide 3 credentials to login, e.g.
username
regnumber
password.
I'm using a custom entry provider, form the book. in this there is a class
public function loadUserByUsername($username)
{
$q = $this
->createQueryBuilder('u')
->where('u.username = :username')
->setParameter('username', $username)
->getQuery();
try {
// The Query::getSingleResult() method throws an exception
// if there is no record matching the criteria.
$user = $q->getSingleResult();
} catch (NoResultException $e) {
$message = sprintf(
'Unable to find an active User "%s".',
$username
);
throw new UsernameNotFoundException($message, 0, $e);
}
return $user;
}
Is there a way to pass more variables to this method e.g. loadUserByUsername($username, $regnumer)
I could then query for the extra data,
does anyone have any advice?
thanks in advance.

Resources