AppThemes Docs

Adding Fields to Pay2Post Forms

You can use Pay2Post to collect data from users. But, what if the data you are looking for isn’t included by default? Its easy to add and save fields in Pay2Post with just a couple hooks.

Displaying Your New Field

Pay2Post calls pay_setup_form right before it starts to display the form. This gives you a chance to prepare your fields to be inserted and check if the form being displayed is for the correct post type.

add_action( 'pay_setup_form', 'my_form_setup' );
function my_form_setup( $post_type ){
    if( $post_type == 'post' ){
        // This form is for a post, so do something
    }else{
        // This form isn't for a post, so don't do anything
    }
}

Once you’ve determined that the form is for the correct post type, you’ll need to hook into pay_display_form. That will be where you actually display your inputs.

add_action( 'pay_setup_form', 'my_form_setup' );
function my_form_setup( $post_type ){
    if( $post_type == 'post' ){
        add_action( 'pay_display_form', 'my_form_display' );
    }
}
function my_form_display( $post_type ){
    echo '<div class="pay-form-field pay-group">';
    echo '<label for="my-new-field">' . __( 'My New Field' ) . '</label>';
    echo '<input type="text" name="my-new-field" id="my-new-field">';
    echo '</div>';
}

Now you should see your new field in your form, all ready to have data entered into it. You can get more sophisticated with your setup by checking things like the user’s permissions or what the post type supports. For instance, Pay2Post checks if the post type being displayed supports the ‘title’ feature before displaying a title input box. This lets it work across a variety of post types.

Saving Your New Field

After you’ve displayed a field, you’ll need to validate and save the data. This is simple using the pay_validate_form and pay_process_form filters. You should set up these filters when you set up your display hook.

add_action( 'pay_setup_form', 'my_form_setup' );
function my_form_setup( $post_type ){
    if( $post_type == 'post' ){
        add_action( 'pay_display_form', 'my_form_display' );
        add_filter( 'pay_validate_form', 'my_form_validator' );
        add_filter( 'pay_process_form', 'my_form_processor' );
    }
}

When pay_validate_form is called, it passes an empty WP_Error object as its only argument. Every function that hooks into this filter should add an error to this object if the data you’ve received from the user is not valid. To learn more about adding an error to WP_Error, see the Codex. Regardless of a present error, every function that hooks into this filter should return the WP_Error object.

function my_form_validator( $errors ){
    if( empty( $_POST['my-new-field'] ) ){
        $errors->add( 'bad-new-field', __( 'You must enter something into My New Field' ) );
    }
    return $errors;
}

If there are errors, the form will be re-displayed with error messages displayed at the top of the page. If no errors have occurred after the pay_validate_form has been called, Pay2Post will continue to pay_process_form. With pay_process_form, Pay2Post passes through the post arguments that will be given to wp_insert_post. This let’s you change things like the post’s status, tags, category, title, and other parts.

function my_form_processor( $post ){
    $post['post_author'] = $_POST['my-new-field'];
    return $post;
}

If you need to preform that requires the created post’s ID, like adding meta data, you can use the pay_process_post action to receive the create post’s object.

function my_form_post_processor( $post ){
    add_post_meta( $post->ID, 'my-new-field', 'some-data' );
}
Your rating: none
Rating: 5 - 1 vote