Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating pages from Ninja form data

I've created a WordPress page with a Ninja form on it that collects miscellaneous data about a product, including some uploaded images. The page with the form is accessible from the main menu by clicking the "Input" item, so the user doesn't need to access the backend to upload their product data.

I now want to put this data into a custom post type called "Listing." There will eventually be thousands of these data sets and so thousands of "Listing" pages, as people come to the site, click Input in the main menu to get to the page with the Ninja form and fill it out.

Could someone tell me how they would go about now building these listing pages from the data the form has collected?

I'm running Ninja's Front-End Post option which supposedly will create a page from the form data. This plugin has some Post creation settings where you can select the post type to create, but this isn't working for me. I would expect the submitted form data to show up under dashboard | Listings, but there's nothing there after submitting the form.

Has anyone gotten this to work?

Thanks for your help.

like image 364
Steve Avatar asked Dec 19 '15 05:12

Steve


1 Answers

I think you can use only Ninja Forms without extensions, and hook directly in 'ninja_forms_after_submission' that fires after submission and allow you to use data submitted and perform actions.

This is a starter codebase to achieve your result, but needs to be customized on your needs and your form structure.

add_action( 'ninja_forms_after_submission', 'create_page_from_ninjaform' );
function create_page_from_ninjaform( $form_data ){

    // your fields data
    $form_fields = $form_data[ 'fields' ];

    // !!! this is an example, it depends form fields in your form
    $title = $form_fields[ 1 ][ 'value' ];
    $content = $form_fields[ 2 ][ 'value' ];
    $sample_meta_field = $form_fields[ 3 ][ 'value' ];

    $new_post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_status' => 'publish',
        'post_type' => 'listing', // be sure this is the post type name
    );

    $new_post_id = wp_insert_post( $new_post );

    update_post_meta( $new_post_id, 'your_meta_key', $sample_meta_field );

}

This code should be copied in functions.php file

Not tested of course.

Good luck ;)

like image 187
FrancescoCarlucci Avatar answered Oct 23 '22 08:10

FrancescoCarlucci