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 Create and get WordPress Custom Field

How to add post from frontend in wordpress without plugin

WordPress Websites Are SEO Friendly Or Not

How to get last insert id in WordPress

Tools:

Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

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.