1

I want to create post using Ajax .

I had followed the process from Insert Post using Ajax But I couldn't make it work.

Here is what I did. Please see what I did wrong as I am just a learner.

EDIT 1

In WordPress page Template

<script type="text/javascript">


$j=jQuery.noConflict(); 
    $j(document).ready(function(){                                              
        $j("#enqform").validate({               
            submitHandler: function () {                 
                dataString = $j("#enqform").serialize();                                
                $.ajax({
                    type: "POST",
    url: "http://localhost/wso/wp-admin/admin-ajax.php",
                  data: dataString,

                });                             
            }                   
        })
    }); 
</script>   

<form action="<?php the_permalink(); ?>" class="contact-box" id="enqform" method="post">
    <label for="firstname">First Name<span class="req">*</span></label>                      
    <input type="text" id="firstname" name="firstname" value="<?php if(isset($_POST['firstname'])) echo $_POST['firstname'];?>" class="required" /> 

    <input type="hidden" name="submitted" id="submitted" value="true" /> 

    <input type="hidden" name="action" value="wp_ajax_admin_enqform" />  
    <input type="hidden" name="nonce" value="<?php echo wp_create_nonce( 'form-nonce' );?>" />                      

        <input type="submit" id="submit-btn" value="Submit" />             
</form> 

In WordPress Function.php

add_action( 'wp_ajax_admin_enqform', 'wpse_126886_ajax_handler' );

function wpse_126886_ajax_handler() {

if(isset($_POST['submitted'])) {    //validations 

    if(trim($_POST['firstname']) === '') {
        $hasError = true;
    } else {
        $firstname = trim($_POST['firstname']);
    }   


    // maybe check some permissions here, depending on your app

 global $wpdb;
    $nonce = $_POST['nonce'];
    if ( ! wp_verify_nonce( $nonce, 'form-nonce' ) ) {
        die( 'Security check' ); 
    } else {            
        $new_post = array(
            'post_title'    => $firstname,
            'post_status'   => 'publish',
            'post_type' => 'post'
        );
        $pid = wp_insert_post($new_post);                           
        if( $pid ) { 
            add_post_meta( $pid, 'cpt_firstname', $firstname, true );                                       
        }


    // send some information back to the javascipt handler
    $response = array(
        'status' => '200',
        'message' => 'OK',
        'new_post_ID' => $pid
    );


    }




    // normally, the script expects a json respone
    header( 'Content-Type: application/json; charset=utf-8' );
    echo json_encode( $response );

    exit; // important
}
}
  • your action hook should be wp_ajax_admin_ajax_enqform, you're missing the wp_ajax_ prefix. – Milo Nov 29 '14 at 17:28
  • i tried this but its not working. – user2506619 Nov 29 '14 at 18:39
  • Did you add a separate hook for wp_ajax_nopriv_admin_ajax_enqform? That will add the hook for users who aren't logged in as well. – Andrew Bartel Nov 30 '14 at 22:17
  • @AndrewBartel . while using the form i always login. then also the from doesn't work. But i will surely use the method for users who aren't logged. thanks in advance. – user2506619 Dec 1 '14 at 8:23
  • Guys i have updated the code as per @milo suggestion. – user2506619 Dec 1 '14 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.