bd_make_post_form_action.php

CWPAction - Create Post Form Action Class

The CWPAction class extends the Breakdance form actions, enabling the creation of new WordPress posts upon form submission. It captures the title and content from form fields and creates a new post with the current user as the author, saving it as a draft.

<?php // 1. Create an action class to represent your field class CWPAction extends \Breakdance\Forms\Actions\Action { public static function name() { return '[CODEWP] Create Post Action'; } // The slug method returns a string to uniquely identify the form action public static function slug() { return 'create_post_form_action'; } // The run method is triggered when the form is submitted public function run($form, $settings, $extra) { try { $this->createPost($extra['fields']['title'], $extra['fields']['content']); // Return success message after creating a new post return ['type' => 'success', 'message' => 'New post created successfully']; } catch (Exception $e) { // Return error message in case of any exceptions return ['type' => 'error', 'message' => 'Error creating post: ' . $e->getMessage()]; } } // Function to create a new post private function createPost($title, $content) { // setting the post data $post_data = array( 'post_title' => wp_strip_all_tags($title), 'post_content' => wp_kses_post($content), 'post_status' => 'draft', 'post_author' => get_current_user_id(), 'post_type' => 'post' ); // insert the post wp_insert_post($post_data); } } // 2. Register the action class with Breakdance // Use the WordPress 'init' action to register the action class, ensuring the Breakdance is installed and available add_action('init', function () { if (!function_exists('\Breakdance\Forms\Actions\registerAction') || !class_exists('\Breakdance\Forms\Actions\Action')) { return; } // Register the action with Breakdance \Breakdance\Forms\Actions\registerAction(new CWPAction()); });

Frequently Asked Questions

CWPAction is a custom WordPress class that allows the creation of posts through form submissions.