<?php
/**
* Template Name: Post Insert
*/
get_header(); ?>
<form method="post" id="post_insert" name="front_end"
enctype="multipart/form-data" action="<?php echo 'http://' .
$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>" >
<input type="text" id="post_title" placeholder="My Post Title" />
<input type="text" id="post_content" placeholder="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" />-->
<button type="button" name="submit" id="submit" >Submit</button>
<input type="hidden" name="action" value="post" />
</form>
<?php get_footer(); ?>
Step : 2 Add this code in function.php
function single_post_insert() {
$new_post = array(
'post_title' => $_POST['title'],
'post_content' => $_POST['content'],
'post_status' => 'publish',
'post_type' => 'post'
);
//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);
echo json_encode(array('flag'=>'1'));
die;
}
add_action( 'wp_ajax_single_post', 'single_post_insert' ); // If called from admin panel
add_action( 'wp_ajax_nopriv_single_post', 'single_post_insert' );
Step:3 Add this code in your js file
jQuery('#submit').click(function(e) {
jQuery.ajax({
url:plumb_ajax.ajax_url,
type:'POST',
dataType:"json",
data: {
action:'single_post',
title:jQuery('#post_title').val(),
content:jQuery('#post_content').val()
},
success: function(response){
alert('complate');
}
});
return false;
});