Submit WordPress posts from the frontend using custom plugin and allow your visitors a way to submit some kind of content of their own, then here’s a way you can create a new post form and display it
In this post, we are going to explain how to insert or add posts from the front end. Inserting post from front end in WordPress is important functionality when we looking guest post, this is very simple and common process.
WordPress allow to users add post or custom post type functionality using WordPress dashboard. Author, Admin and contributor user can be submit post if you looking logged in used don’t use dashboard and add post from front end then you just follow simple these steps.
Submit WordPress posts from the frontend
Step 1. Create a new folder for your plugin
First of you should create a folder and giving it a unique name using lowercase letters and dashes such as submit-post-from-frontend. After create folder need to create the main file for your plugin. So, create a PHP file within your plugin folder and give it the same name such as submit-post-from-frontend.php or index.php file. Add plugin information below into your main plugin file.
<?php /* * Plugin Name: Submit post from frontend * Description: My plugin to explain the submit post from frontend functionality. * Version: 1.0 * Author: Diwakar Academy */
Step 2. Create a form by which a user can submit the post
Allowing the submission of user post from the the front-end of your WordPress site starts with HTML. For us to get the HTML to show up, I am going to use a shortcode in WordPress. Shortcodes are contained in square-brackets, and can often be single tags that pull in another PHP function. That’s what we’ll do here, so our form will appear on a post, page, or other WordPress content type when someone uses [da_frontend_post_form]
.
add_shortcode( 'da_frontend_post_form', 'da_frontend_post_form' ); function da_frontend_post_form() { if(is_user_logged_in()){ $msg = ''; $errors = 0; if (isset($_REQUEST['submit'])) { if (wp_verify_nonce($_REQUEST['post_nonce'], 'post_action_nonce')) { if (empty($_REQUEST['title'])) { $errors++; } if (empty($_REQUEST['content'])) { $errors++; } if (empty($errors)) { $post = array( 'post_title' => wp_strip_all_tags($_POST['title']), 'post_content' => $_POST['content'], 'post_category' => array($_POST['cat']), 'tags_input' => $_POST['post_tags'], 'post_status' => 'draft', // Could be: publish 'post_type' => 'post' // Could be: `page` or your CPT ); $post_id = wp_insert_post($post); update_post_meta($post_id, 'isbn_no', $_POST['post_tags']); if( $post_id){ $msg = '<h5 style="text-align:center">Saved your post successfully! :)</h5>'; if ( $_FILES["image"]["name"]) { $upload = wp_upload_bits( $_FILES["image"]["name"], null, file_get_contents( $_FILES["image"]["tmp_name"] ) ); if ( ! $upload['error'] ) { $filename = $upload['file']; $wp_filetype = wp_check_filetype( $filename, null ); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name( $filename ), 'post_content' => '', 'post_status' => 'inherit' ); $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); if ( ! is_wp_error( $attachment_id ) ) { require_once(ABSPATH . 'wp-admin/includes/image.php'); $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); wp_update_attachment_metadata( $attachment_id, $attachment_data ); set_post_thumbnail( $post_id, $attachment_id ); } } } } echo $msg; unset($_POST['title']); unset($_POST['content']); } } if(!empty($errors)){ echo "<p style='text-align: center; color:red;'>Please fill in all the required fields</p>"; } } ?> <div class="postbox"> <form method="post" enctype="multipart/form-data"> <p><label for="title">Title *</label><br /> <input type="text" value="<?php echo $_POST['title']; ?>" name="title" /> </p> <p> <label for="content">Post Content *</label><br /> <textarea name="content" rows="6"><?php echo $_POST['content']; ?></textarea> </p> <p><label>Select Category</label> <?php wp_dropdown_categories( 'show_option_none=Category&taxonomy=category&hide_empty=0' ); ?> </p> <p><label for="post_tags">Tags</label> <input type="text" value="<?php echo $_POST['post_tags']; ?>" name="post_tags" /></p> <p><label for="post_tags">Feature Image</label> <input type="file" name="image"></p> <?php wp_nonce_field('post_action_nonce', 'post_nonce'); ?> <p><input type="submit" value="Submit" name="submit" /></p> </form> </div> <?php }else{ echo '<h5 style="text-align:center">After Login you can see form</h5>'; } }
If you want to format this form use given css. create css folder with main plugin folder and create a file like style.css
input[type=text], select,textarea { width: 100%; padding: 12px 20px; margin: 8px 0; display: inline-block; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; } input[type=submit] { width: 100%; background-color: #4CAF50; color: white; padding: 14px 20px; margin: 8px 0; border: none; border-radius: 4px; cursor: pointer; } input[type=submit]:hover { background-color: #45a049; } .postbox { border-radius: 5px; background-color: #f2f2f2; padding: 20px; width: 70%; margin: 0 auto; }
after create css file we need to enqueue. following code to enqueue css file
function wpb_adding_styles() { wp_register_style('my_stylesheet', plugin_dir_url( __FILE__ ).'/css/style.css'); wp_enqueue_style('my_stylesheet'); } add_action( 'wp_enqueue_scripts', 'wpb_adding_styles' );
Full Code index.php file
<?php /* * Plugin Name: Submit post from frontend * Description: My plugin to explain the submit post from frontend functionality. * Version: 1.0 * Author: Diwakar Academy */ add_shortcode( 'da_frontend_post_form', 'da_frontend_post_form' ); function da_frontend_post_form() { if(is_user_logged_in()){ $msg = ''; $errors = 0; if (isset($_REQUEST['submit'])) { if (wp_verify_nonce($_REQUEST['post_nonce'], 'post_action_nonce')) { if (empty($_REQUEST['title'])) { $errors++; } if (empty($_REQUEST['content'])) { $errors++; } if (empty($errors)) { $post = array( 'post_title' => wp_strip_all_tags($_POST['title']), 'post_content' => $_POST['content'], 'post_category' => array($_POST['cat']), 'tags_input' => $_POST['post_tags'], 'post_status' => 'draft', // Could be: publish 'post_type' => 'post' // Could be: `page` or your CPT ); $post_id = wp_insert_post($post); if( $post_id){ $msg = '<h5 style="text-align:center">Saved your post successfully! :)</h5>'; if ( $_FILES["image"]["name"]) { $upload = wp_upload_bits( $_FILES["image"]["name"], null, file_get_contents( $_FILES["image"]["tmp_name"] ) ); if ( ! $upload['error'] ) { $filename = $upload['file']; $wp_filetype = wp_check_filetype( $filename, null ); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name( $filename ), 'post_content' => '', 'post_status' => 'inherit' ); $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); if ( ! is_wp_error( $attachment_id ) ) { require_once(ABSPATH . 'wp-admin/includes/image.php'); $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); wp_update_attachment_metadata( $attachment_id, $attachment_data ); set_post_thumbnail( $post_id, $attachment_id ); } } } } echo $msg; unset($_POST['title']); unset($_POST['content']); } } if(!empty($errors)){ echo "<p style='text-align: center; color:red;'>Please fill in all the required fields</p>"; } } ?> <div class="postbox"> <form method="post" enctype="multipart/form-data"> <p><label for="title">Title *</label><br /> <input type="text" value="<?php echo $_POST['title']; ?>" name="title" /> </p> <p> <label for="content">Post Content *</label><br /> <textarea name="content" rows="6"><?php echo $_POST['content']; ?></textarea> </p> <p><label>Select Category</label> <?php wp_dropdown_categories( 'show_option_none=Category&taxonomy=category&hide_empty=0' ); ?> </p> <p><label for="post_tags">Tags</label> <input type="text" value="<?php echo $_POST['post_tags']; ?>" name="post_tags" /></p> <p><label for="post_tags">Feature Image</label> <input type="file" name="image"></p> <?php wp_nonce_field('post_action_nonce', 'post_nonce'); ?> <p><input type="submit" value="Submit" name="submit" /></p> </form> </div> <?php }else{ echo '<h5 style="text-align:center">After Login you can see form</h5>'; } } function wpb_adding_styles() { wp_register_style('my_stylesheet', plugin_dir_url( __FILE__ ).'/css/style.css'); wp_enqueue_style('my_stylesheet'); } add_action( 'wp_enqueue_scripts', 'wpb_adding_styles' );
Leave a Reply