0

Admin side I submit form to admin-post.php. I want to print success message in the bottom of the form. Iam new in wordpress what i did shown below.

admin.php?page=add-products form code

 <form action="<?php echo admin_url('admin-post.php') ?>" method="post">
                <table>
                <input type="hidden" name="action" value="add_product_from_admin">
                <tr><td>Name</td><td><input type="text" name="pr_name" id="pr_name"></td></tr> 
    <tr><td colspan="2" align="center"><input type="submit" name="pr_submit" id="pr_submit" value="Save Products"></td></tr>
                </table>
            </form>


    add_action( 'admin_post_add_product_from_admin', 'add_product_into_data_base' );



function add_product_into_data_base() {
    //some database operations
wp_redirect(admin_url('admin.php?page=add-products&message=success'));
}

2 Answers 2

0

You need to pass the variable and its value in GET method to display the success message which you're already doing.

Now, in your PHP file, display a message based on the GET parameter and its value.

if($_GET['message'] == 'success'){ 
  //display your message 
}
2
0
 <form action="<?php echo admin_url('admin-post.php') ?>" method="post">
   <table>
      <input type="hidden" name="action" value="add_product_from_admin">
      <tr>
         <td>Name</td>
         <td><input type="text" name="pr_name" id="pr_name"></td>
      </tr>
      <tr>
         <td colspan="2" align="center"><input type="submit" name="pr_submit" id="pr_submit" value="Save Products"></td>
      </tr>
   </table>
</form>

Handle form request by admin_post with action name - add_product_from_admin

add_action( 'admin_post_add_product_from_admin', 'add_product_into_data_base' );
    
function add_product_into_data_base() {
        //some database operations
        
        // Set a transient with the success message
        set_transient('product_added_success', 'Product added successfully', 5);
    
        wp_redirect(admin_url('admin.php?page=add-products'));
        exit();
}


// Add admin notice for product added successfully
function display_product_added_notice() {
    $message = get_transient('product_added_success');

    if ($message) {
        ?>
        <div class="notice notice-success is-dismissible">
            <p><?php echo esc_html($message); ?></p>
        </div>
        <?php
        // Delete the transient after displaying the notice
        delete_transient('product_added_success');
    }
}
add_action('admin_notices', 'display_product_added_notice');

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.