wordpress subscriber with contact form 7 - wordpress

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');

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');

Manually authenticate a user for a specific subdomain in Symfony 5

I need to redirect the user after successful registration to their own subdomain (test for this example).
/**
* #Route("/signup", name="app_signup", host="admin.mysymfony.local")
*/
public function signup(
Request $request,
UserPasswordEncoderInterface $passwordEncoder,
LoginFormAuthenticator $authenticator,
GuardAuthenticatorHandler $guardAuthenticatorHandler
): Response
{
$user = new User();
$form = $this->createForm(SignupType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
$user->setPassword($passwordEncoder->encodePassword($user, $user->getPassword()));
$roles = $user->getRoles();
$roles[] = 'ROLE_ADMIN';
$user->setRoles($roles);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
$this->get('session')->set('user_id', $user->getId());
return $guardAuthenticatorHandler->authenticateUserAndHandleSuccess(
$user,
$request,
$authenticator,
'main'
);
}
return $this->render('security/signup.html.twig', [
'form' => $form->createView(),
]);
}
This works fine and the user is redirected to this method after successful authentication:
/**
* #Route("/signup/complete", name="app_signup_complete", host="admin.mysymfony.local")
*/
public function signupComplete(
Request $request,
UserPasswordEncoderInterface $passwordEncoder,
LoginFormAuthenticator $authenticator,
GuardAuthenticatorHandler $guardAuthenticatorHandler
): Response
{
if ($this->getUser() && $this->isGranted('ROLE_ADMIN') ) {
error_log('User authenticated');// this is logged successfully
}
if ( strpos($request->getHost(), 'admin.') == 0 ) {
$host = str_replace('admin.', 'test.', $request->getHost());
$homeUrl = $this->generateUrl('app_home');
$testHomeUrl = $request->getScheme() . '://' . $host. $homeUrl;
return $this->redirect(
$testHomeUrl
);
}
}
This is the method that is called after redirection to the user subdomain:
/**
* #Route("/home", name="app_home")
*/
function index(MessageGenerator $messageGenerator) {
if ( $this->getUser() && $this->isGranted('ROLE_ADMIN')) {
$message = $messageGenerator->getHappyMessage();
$htmlResponse = '<html><body>';
$htmlResponse .= "<p>Lucky message: ".$message. '</p>';
$htmlResponse .= "<p>User id : {$this->getUser()->getId()}."
. '</p>';
$htmlResponse .= "<p>Is granted ROLE_USER : {$this->isGranted('ROLE_USER')}."
. '</p>';
$htmlResponse .= "<p>Is granted ROLE_ADMIN : {$this->isGranted('ROLE_ADMIN')}."
. '</p>';
$htmlResponse .= '</body></html>';
return new Response(
$htmlResponse
);
}
else {
return new Response(var_export($this->get('session')->get('user_id'), true));
}
}
As expected it falls in the else section and the value of user_id passed to the session is not recognized because it is a different subdomain.
All suggestions are welcome and please if there is something that needs clarification let me know.

show a message when updating a custom widget in wordpress backend

I'm developing a custom wordpress widget. The widget needs some data from the user and needs to check this data server-side.
I wrote the code that checks the data inside the function 'update' of the widget. When I press the button save of the widget the function update got called correctly and my validation is executed.
public function update( $new_instance, $old_instance ) {
$instance = array();
foreach ($this->fields as $field) {
$fieldName = $field['name'];
$instance[$fieldName] =
(!empty($new_instance[$fieldName]) strip_tags($new_instance[$fieldName]) : '' );
}
$check = validate($new_instance);
return $instance;
}
What I need is to display a message to the user based on the result of the validation. How can I do this? For what I've seen the function update is called through ajax so I can't use an admin notice.
Is it possible?How can I do that?
Try below code
add_action('admin_notices', 'misha_custom_order_status_notices');
function misha_custom_order_status_notices() {
global $pagenow, $typenow;
if( get_transient( 'fx-admin-notice-panel' )){
echo "<div class=\"updated\"><p>Custom notification comes here</p></div>";
}
}
public function update( $new_instance, $old_instance ) {
$instance = array();
foreach ($this->fields as $field) {
$fieldName = $field['name'];
$instance[$fieldName] =
(!empty($new_instance[$fieldName])?
strip_tags($new_instance[$fieldName]) :
''
);
}
$check = validate($new_instance);
set_transient( 'fx-admin-notice-panel', true, 5 );
return $instance;
}

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.

Hook into the user_change process

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?

Resources