WordPress Post Insert Using Ajax Page Without Refresh

WordPress Post Insert Using Ajax Page Without Refresh

Hi, Today Discussed WordPress Post Insert Using Ajax Page Without Refresh WordPress more then plugin used and then today we discussed blog posting contact form what ever posting insert database ajax without refresh stored db.

WordPress Complete content form-process.php wordpress all file is path wp-content/theme/_____
that the find functions for function.php or other file function wp_insert_post() is not defined, because the WordPress core was not loaded.

WordPress Post Insert Using Ajax Page Without Refresh
WordPress Post Insert Using Ajax Page Without Refresh

add_action( 'admin_ajax_your_form_action', 'wpse_126886_ajax_handler' );

function wpse_126886_ajax_handler() {

    // maybe check some permissions here, depending on your app
    if ( ! current_user_can( 'edit_posts' ) )
        exit;

    $post_data = array();
    //handle your form data here by accessing $_POST

    $new_post_ID = wp_insert_post( $post_data );

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

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

    exit; // important
}
your_form_action slug is the >> trigger of your Call Back. you have to append this slug to your request parameter named form input name normally get or post method using jquery ajax  passing data parameter or form data using .serialize(). Input type hidden field passing parameter.

<input type="hidden" name="action" value="your_form_slug" />

and then file path wp-admin/admin-ajax.php


$.ajax({
    type: "POST",
    url: "http://www.mysite.co.uk/wp-admin/admin-ajax.php",
    data: dataString,
    error: function(jqXHR, textStatus, errorThrown){                                        
        console.error("The following error occured: " + textStatus, errorThrown);                                                       
    },
    success: function(data) {                                       
        console.log("Hooray, it worked!");                                                                  
    }                              
});






WordPress Post Insert Using Ajax Page Without Refresh Dev2Tricks 5 of 5
WordPress Post Insert Using Ajax Page Without Refresh Hi, Today Discussed WordPress Post Insert Using Ajax Page Without Refresh WordPres...

Share this

Related Posts

Previous
Next Post »

Xolo Offer