Creating Frontend Post Submissions Using Bricks Builder Theme

Are you looking to enable frontend post submissions on your WordPress site? This tutorial will guide you through the process of creating a form that allows users to submit posts directly from the frontend. We’ll be using the Bricks Builder theme, a powerful and versatile tool for building beautiful websites.

Prerequisites

Before we begin, make sure you have the Bricks Builder theme installed and activated on your WordPress site.

Step-by-Step Guide

  1. Set Up Your Form Start by creating a form in Bricks Builder. Ensure your form has the following fields:
    • Title (field ID: form-field-152603)
    • Category (field ID: form-field-495c45)
    • Content (field ID: form-field-45d299)
  2. Add Custom Action to Handle Form Submission Next, we’ll add a custom action to handle the form submission and create a new post. Add the following code to your theme’s functions.php file:
function my_form_custom_action( $form ) {  
   $fields = $form->get_fields();
   $settings = $form->get_settings();
   
   // Retrieve form field values
   $post_title = isset($fields['form-field-152603']) ? sanitize_text_field($fields['form-field-152603']) : '';
   $post_content = isset($fields['form-field-45d299']) ? sanitize_textarea_field($fields['form-field-45d299']) : '';
   $category_name = isset($fields['form-field-495c45']) ? sanitize_text_field($fields['form-field-495c45']) : '';
   
   // Get category ID by category name
   $category_id = get_cat_ID($category_name);
   
   // If category does not exist, create it and get the new ID
   if ($category_id == 0) {
       $category_id = wp_create_category($category_name);
   }
   
   // Prepare post data
   $post_data = array(
       'post_title'    => $post_title,
       'post_content'  => $post_content,
       'post_status'   => 'publish',
       'post_category' => array( $category_id )
   );
   
   // Insert the post into the database
   $post_id = wp_insert_post( $post_data );
   
   if ( is_wp_error( $post_id ) ) {
       $form->set_result([
           'action' => 'my_custom_action',
           'type'   => 'danger', //or info or success
           'message' => esc_html__('Oh my custom action failed', 'bricks'),
       ]);
   } else {
       $form->set_result([
           'action' => 'my_custom_action',
           'type'   => 'success', //or info or danger
           'message' => esc_html__('Post created successfully!', 'bricks'),
       ]);
   }
}
add_action( 'bricks/form/custom_action', 'my_form_custom_action', 10, 1 );

Explanation

  • Form Field IDs: The code retrieves the values of the form fields form-field-152603, form-field-495c45, and form-field-45d299.
  • Category Handling: It converts the category name to a category ID, creating a new category if it doesn’t exist.
  • Post Creation: The wp_insert_post function creates a new post with the provided title, content, and category, and publishes it immediately.
  • Error Handling: It checks for errors and sets the appropriate result message.

Conclusion

By following this tutorial, you can easily set up frontend post submissions on your WordPress site using the Bricks Builder theme. This functionality can be especially useful for guest blogging, user-generated content, or community-driven websites. Happy building!

Remember, the Bricks Builder theme is necessary for this functionality, as it provides the tools and flexibility required to create and manage your forms and custom actions. Enjoy the enhanced interactivity and user engagement on your site!

Leave the first comment