1. Home
  2. WP User Frontend Pro
  3. Developer Docs
  4. Action Hook Field

Action Hook Field

There is an element on the Form Elements called Action Hook. This is a great addition for developers to extend the form as they want. It’s a placeholder for creating their own hook where they could bind their functions and generate their own dynamic element.

Usage:

add_action('HOOK_NAME', 'your_function_name', 10, 3 );
function your_function_name( $form_id, $post_id, $form_settings ) {
    // do what ever you want
}

Parameters:

  • $form_id : (integer) The ID of the form
  • $post_id : NULL or (integer) the ID of the post. When creating a new post, the parameter becomes NULL. When editing a post, you get the edited post ID as the parameter
  • $form_settings : (array) An array of form settings

Example:

Here we have added a new action hook field called my_brand_new_hook and saving our field informations in a meta key named your_meta_key. We are adding the field into the form with the your_function_name function and updating the form value with update_my_brand_new_hook

/**
 * Add the input field to the form
 * 
 * @param int $form_id
 * @param null|int $post_id
 * @param array $form_settings
 */
function your_function_name( $form_id, $post_id, $form_settings ) {
    $value = '';

    if ( $post_id ) {
        $value = get_post_meta( $post_id, 'your_meta_key', true );
    }
    ?>

    <div class="wpuf-label">
        <label>A demo field</label>
    </div>

    <div class="wpuf-fields">
        <input type="text" name="my_custom_field" value="<?php echo esc_attr( $value ); ?>">
    </div>
    <?php
}

add_action( 'my_brand_new_hook', 'your_function_name', 10, 3 );

/**
 * Update the custom field when the form submits
 * 
 * @param type $post_id
 */
function update_my_brand_new_hook( $post_id ) {
    if ( isset( $_POST['my_custom_field'] ) ) {
        update_post_meta( $post_id, 'your_meta_key', $_POST['my_custom_field'] );
    }
}

add_action( 'wpuf_add_post_after_insert', 'update_my_brand_new_hook' );
add_action( 'wpuf_edit_post_after_update', 'update_my_brand_new_hook' );

Was this article helpful to you? Yes 8 No 15

How can we help?