Skip Navigation

[Resolved] Stop user_register hook when create user account from dashboard

This support ticket is created 4 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 4 years, 1 month ago.

Assisted by: Waqar.

Author
Posts
#1888141

Dear Sir/Madam,

I have a form for frontend user to do the registration and then insert a new custom post automatically.

When I manually add a user from dashboard, it create the custom post also, refer to below code, how can I only do the custom post creation if I do the registration from the form?

function auto_apply_competition($user_id){
    
    if (!$user_id>0 && !empty($_POST['gf-competition-id']))
        return;

    $post_title = sprintf("%s - %s", get_the_title($_POST['gf-competition-id']), $_POST['gf-competition-id']);

    $new_post = array(
        'ID' => '',
        'post_author' => $user_id, 
        'post_content' => $post_content, 
        'post_title' => $post_title,
        'post_status' => 'publish',
        'post_type' => 'vwcm-application',
        'meta_input' => array(
            'wpcf-competition-id' => $_POST['gf-competition-id'],
            'wpcf-apply-level'=> $_POST['gf-apply-level'],
            'wpcf-order-status' => "unpaid",
            'wpcf-order-number' => '',
            'wpcf-upload-receipt' => $_POST['gf-upload-receipt']
        )
    );

    $post_id = wp_insert_post($new_post, 1);

    wp_update_post( array(
        'ID' => $post_id,
        'post_name' => $post_id,
        'post_title' => sprintf("%s - %s (%s)", get_the_title($_POST['gf-competition-id']), $_POST['gf-competition-id'], $post_id)
    ));
}        
add_action( 'user_register', 'auto_apply_competition', 10, 1 );
#1888701

Hi,

Thank you for contacting us and I'd be happy to assist.

The "user_register" is a generic WordPress hook, which is why it is executed whenever a new user is added, through the admin area or the front-end Toolset form.

If your goal is to create the automatic post, only when the user is added through the front-end Toolset form, you can instead use the "cred_submit_complete" hook from Toolset Forms API:
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete )

Example:


add_action('cred_submit_complete', 'auto_apply_competition',10,2);
function auto_apply_competition($user_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12345)
    {
        // rest of the code to add a new post
    }
}

Note: Please replace "12345" with the actual Toolset form's ID.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1892899

Dear Waqar,

Thanks for your reply. May I know whether the hook cred_submit_complete is only applied to post_form or user_form?

#1893549

Thanks for writing back and the "cred_submit_complete" hook is applied to both, post and user forms.