Wordpress update user meta when the user submits form (form called with do_shortcode)

81 Views Asked by At

I want to update user meta after the user submits a form. function updateUserType(){update_user_meta works as expected alone, however it does not work when the form is called with do_shortcode.

echo do_shortcode('[contact-form-7 id="031c4e8" title="Dosya"]');
add_action( 'wpcf7_before_send_mail', 'updateUserType');
function updateUserType() {
    $user_idd = get_current_user_id();
    update_user_meta( $user_idd, '_user_type', 'general' ) ;}
1

There are 1 best solutions below

0
Howard E On

Using the CF7 Meta field 'current_user_id' would return the user id whether it's called via do_shortcode or otherwise. You could add a 'subscriber_mode:on' to the extra settings in the form to prevent non-logged in users from completing the form if you wanted to.

This has been tested and works.

add_action( 'wpcf7_before_send_mail', 'dd_update_user_type', 10, 3 );
/**
 * Update user type in user metadata.
 *
 * @param object $contact_form The contact form object.
 * @param bool   $abort        Whether to abort the update or not.
 * @param object $submission   The submission object.
 *
 * @return void
 */
function dd_update_user_type( $contact_form, $abort, $submission ) {
    $user_id = $submission->get_meta( 'current_user_id' );
    update_user_meta( $user_id, '_user_type', 'general' );
}