1

I want to display a success message on updating a form from front-end using wp_update_post at the top of the form.

I use below on edit page

$post_id = wp_update_post($edit_post);

and defined function

function write_here_show_success_messages( $success_msg ) {
    echo '<div class="form-success">';
    echo '<span>'.$success_msg.'</span><br/>';
    echo '</div>';
}

How do I execute this function when the form is updated at the top of the form(or specific location on the page)?

1 Answer 1

2

wp_update_post

The ID of the post if the post is successfully updated in the database. Otherwise returns 0.

So just test again that.

if ( $post_id != 0 ) { // success!
    write_here_show_success_messages();
} 

Or you can use an action.

<?php
/** use action for success message **/
if ( $post_id != 0 ) { // success!
    add_action('form_message', 'write_here_show_success_messages' );
}
?>
<div>
  <?php do_action('form_message'); ?>
  <form>
     <!-- form contents -->
  </form>
</div>
5
  • Thank you for the answer! It works but it shows the message on the top of the HTML page. How do I show this message in the DIV I want it to appear?
    – Ohsik
    Nov 20, 2015 at 23:48
  • Place it within the div where you want to display the message. Be sure to wrap it in php tags. <?php if ( $post_id != 0 ) { //do message } ?> <form> // form content </form> Nov 20, 2015 at 23:52
  • I move entire if ( $post_id != 0 ) { write_here_show_success_messages( 'Update Post' ); } to the top of the Form but it does not work. I must of doing something wrong here..
    – Ohsik
    Nov 20, 2015 at 23:56
  • Try using an action gist.github.com/nunotmp/5cec9f7021c1ecb5a865 Nov 21, 2015 at 0:00
  • No problem. I added the add_action snippet to the answer. Glad to help. Nov 21, 2015 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.