Calculated Fields Form Blog

Tips and cases of use for a successful WordPress website with calculated forms.

Blog / Creating new posts

Creating new posts

The "Calculated Fields Form" is a very versatile plugin. It allows to use the information collected by the form in multiple process. In this occasion I'll to describe how to use this information to create new posts on the website.


I'll try to describe through this article how it is possible to use the information submitted by the form to generate new posts on the website (Posts as a general concept, the pages are posts, and WooCommerce products too)

To create new posts with the information submitted by the form, I'll uses the "cp_calculatedfieldsf_insert_in_database.php" file, distributed with the Developer and Platinum versions of the plugin.

The "cp_calculatedfieldsf_insert_in_database.php" file is included in the plugin to allow the information submitted by the form to be stored in third party databases or tables. However, since the code in this file is evaluated each time a form is submitted, you can include as its content, any code you need to run.

First I'll describe some WordPress functions, necessary to create new posts and identify the current user (the author of publication). And then, I will describe how these functions should be called from the "cp_calculatedfieldsf_insert_in_database.php" file.

WordPress functions

wp_insert_post(array $postarr, bool $wp_error = false)

The wp_insert_post function requires at least one parameter: $postarr, an array with the post's attributes. More information here: https://developer.wordpress.org/reference/functions/wp_insert_post/

I'll describe here only the attributes to use in this publication:

  • post_title: The title of the post.
  • post_content: The text of the post.
  • post_status: Determines the status of the post. If it is public (publish) or not (draft)
  • post_author: The id of the WordPress user, author of the post.
  • post_type: Indicates the type of post being created: post, page, product, etc.

There are other possible attributes that you can know visiting the previous link.

get_current_user_id()

Returns the id of the logged user. If the user is anonymous, this function returns 0. More information: https://developer.wordpress.org/reference/functions/get_current_user_id/

The form

For this article, I've created a form (the form's id=1) with two basic fields, a text field (fieldname1) for the title of post, and a text area field (fieldname2) for its content.

Form

The "cp_calculatedfieldsf_insert_in_database.php" file

As I said at the beginning of this article, the code of this file runs every time a form is submitted.

Open this file with the text editor of your choice, and edit its code as follows:

<?php
if($params['formid'] == 1)
{
    $author = get_current_user_id();
    if($author !== 0) // It is a logged user
    {
        wp_insert_post(
            array(
                'post_author' => $author,
                'post_title' => sanitize_text_field($params['fieldname1']),
                'post_content' => wp_kses_post($params['fieldname2']),
                'post_type' => 'post',
                'post_status' => 'draft'
            )
        );
    }
}

Describing the code

The $params variable is a matrix with the fields submitted by the form and their values, and other attributes, such as form's id, the submission's id, submission's date, etc.

The first line of code is a conditional statement to verify if the submitted form is the correct for creating new posts:

if($params['formid'] == 1)  

The second step in the process, requires obtaining the user's ID to know if he/she is a logged user or not (if the id is 0 the user is anonymous, in whose case the post wouldn't be created):

$author = get_current_user_id();
if($author !== 0) // It is a logged user
{
    //...
}

The final step in the process calls the WordPress function: wp_insert_post to generate the new post, whose attributes are filled with the values of the $params attribute:

array(
    'post_author' => $author,
    'post_title' => sanitize_text_field($params['fieldname1']),
    'post_content' => wp_kses_post($params['fieldname2']),
    'post_type' => 'post',
    'post_status' => 'draft'
)

Some considerations in the previous code.

The post_status is defined as 'draft' to allow the editor to check it before publishing.

The post_title is being filled with the information entered through the text field: fieldname1, and the post_content with the information entered in the text area field (fieldname2).

The plugin sanitizes the information collected by the form, but it's not so bad sanitize the information yourself, using the functions available in WordPress:

sanitize_text_field

wp_kses_post

The list of sanitization functions is available in the following link:

https://codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data