• Resolved exedesign2

    (@exedesign2)


    Hi there.

    I would like to use contact form 7 as a front-end posting tool.
    I would like to let user create new posts for my custom post type “products” with a featured image. I was able to create new user with the following code bellow, but how can I amend that code to insert new post to my custom post type “products” with a featured image?
    Thanks for looking.
    [code]
    // register user using contactform7
    function create_user_from_registration($cfdata) {
    if (!isset($cfdata->posted_data) && class_exists('WPCF7_Submission')) {
    // Contact Form 7 version 3.9 removed $cfdata->posted_data and now
    // we have to retrieve it from an API
    $submission = WPCF7_Submission::get_instance();
    if ($submission) {
    $formdata = $submission->get_posted_data();
    }
    } elseif (isset($cfdata->posted_data)) {
    // For pre-3.9 versions of Contact Form 7
    $formdata = $cfdata->posted_data;
    } else {
    // We can't retrieve the form data
    return $cfdata;
    }
    // Check this is the user registration form
    if ( $cfdata->title() == 'Register User') {
    $password = wp_generate_password( 12, false );
    $email = $formdata['your-email'];
    $name = $formdata['full-name'];
    // Construct a username from the user's name
    $username = strtolower(str_replace(' ', '', $name));
    $name_parts = explode(' ',$name);
    if ( !email_exists( $email ) ) {
    // Find an unused username
    $username_tocheck = $username;
    $i = 1;
    while ( username_exists( $username_tocheck ) ) {
    $username_tocheck = $username . $i++;
    }
    $username = $username_tocheck;
    // Create the user
    $userdata = array(
    'user_login' => $username,
    'user_pass' => $password,
    'user_email' => $email,
    'nickname' => reset($name_parts),
    'display_name' => $name,
    'first_name' => reset($name_parts),
    'last_name' => end($name_parts),
    'role' => 'subscriber');
    $user_id = wp_insert_user( $userdata );
    if ( !is_wp_error($user_id) ) {
    // Email login details to user
    $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
    $message = "Welcome! Your login details are as follows:" . "\r\n";
    $message .= sprintf(__('Username: %s'), $username) . "\r\n";
    $message .= sprintf(__('Password: %s'), $password) . "\r\n";
    $message .= wp_login_url() . "\r\n";
    wp_mail($email, sprintf(__('[%s] Your username and password'), $blogname), $message);
    }
    }
    }
    return $cfdata;
    }
    add_action('wpcf7_before_send_mail', 'create_user_from_registration', 1);
    [/code]

    so far I manage to get this one

    [code]
    // Create post object
    $my_post = array(
    'post_title' => $formdata['form-title-field'],
    'post_content' => $formdata['form-content-field'];,
    'post_status' => 'publish',
    'post_author' => 1, // ID of the author
    'post_category' => array(8,39) // your category IDs here
    );

    // Insert the post into the database
    wp_insert_post( $my_post );
    [/code]

    https://wordpress.org/plugins/contact-form-7/

Viewing 4 replies - 1 through 4 (of 4 total)
  • hachesilva

    (@hachesilva)

    Ran into this issue myself and managed to solve it. You just basically need to use a try/catch statement, I mean, you MUST use it, at least for me that was the only way to make it work.

    Please take a look at the gist here: https://gist.github.com/hachesilva/82089ee3bd3c1ad177d9

    Thread Starter exedesign2

    (@exedesign2)

    Thank you, but I don;t know what to do with that functions.
    My issue is connecting the code from the link you provided to a custom post type.

    hachesilva

    (@hachesilva)

    You are trying to create a post using the wp_insert_post() function after a user submits a contact form 7 form. Well, that is exactly what my code does: using the wpcf7_before_send_mail hook, it creates a post of type post, in your case it would be the type ‘products’, but the rest of the code is almost the same, just replace my form fields with yours and you are done.

    Thread Starter exedesign2

    (@exedesign2)

    I was able to sort it out due to coding error.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Insert_post using contact form 7’ is closed to new replies.