How to add post from fronted in WordPress Without Plugin

Today we are talking about world most used CMS (WordPress) 90% php developers familiar with WordPress because of WordPress very easy to use and we can develop any type of websites. Basically WordPress using for blog writing and we can manage our blog from back-end but some time user required guest post from your visitors then this is only for you.
If you are trying to figure out on how do I make my WordPress website to be able to add post items from the fronted. There are many plugins providing that feature but if you don’t want to use plugin in your website then this tutorial only for you.

PHP Code for add post from fronted in WordPress

<?php
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "my_post_type")
{
    //store our post vars into variables for later use
    //now would be a good time to run some basic error checking/validation
    //to ensure that data for these values have been set
    $title     = $_POST['title'];
    $content   = $_POST['content'];
    $post_type = 'my_custom_post';
    $custom_field_1 = $_POST['custom_1'];
    $custom_field_2 = $_POST['custom_2'];
    
    //the array of arguements to be inserted with wp_insert_post
    $new_post = array(
        'post_title'    => $title,
        'post_content'  => $content,
        'post_status'   => 'publish',
        'post_type'     => $post_type
    );
    
    //insert the the post into database by passing $new_post to wp_insert_post
    //store our post ID in a variable $pid
    $pid = wp_insert_post($new_post);
    
    //we now use $pid (post id) to help add out post meta data
    add_post_meta($pid, 'meta_key', $custom_field_1, true);
    add_post_meta($pid, 'meta_key', $custom_field_2, true);
}
?>

Html Code for add post from fronted in WordPress

<form method="post" name="front_end" action="" >
    <input type="text" name="title" value="My Post Title" />
    <input type="text" name="content" value="My Post Content" />
    <input type="text" name="custom_1" value="Custom Field 1 Content" />
    <input type="text" name="custom_2" value="Custom Field 2 Content" />
    <span><?php wp_dropdown_categories( 'tab_index=10&taxonomy=category&hide_empty=0' ); ?></span>
    <button type="button">Submit</button>
    <input type="hidden" name="action" value="my_post_type" />
</form>

You can place this code into your website theme template file it will be work for you.

Leave a Reply

Related Posts: You may like

How to track contact form 7 form in salesforce without plugin

Contact Form 7 Redirect to Thank You Page

How to Validation Business Email in Contact Form 7 WordPress

How to Create and get WordPress Custom Field

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Download Instagram Videos & Photos.

Download Now

CURRENCY CONVERTER

Real Time Currency Converter

Convert Now

ROBOST.TXT GEN.

Robots.txt File Creater

Create Now

HTML MINIFIER

Compress yout HTML Code

Minify Now

CSS MINIFIER

Compress yout CSS Code

Minify Now

JAVASCRIPT MINIFIER

Compress yout JavaScript Code

Minify Now

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.