Advertisement
bcworkz

WP Admin Form Handler

Dec 9th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.20 KB | None | 0 0
  1. Relevant part of callback for add_suubmenu_page()
  2.  
  3.     <form method="POST" action="<?php echo admin_url('admin-post.php?action=dc_convert'); ?>" novalidate="novalidate">
  4.     <input type="hidden" id="_wpnonce" name="_wpnonce" value="<?php echo wp_create_nonce('dc_convert'); ?>" />
  5.  
  6.     The rest of input fields go here
  7.  
  8.     </form>
  9.  
  10. <?php
  11. // Form submit handler
  12. add_action('admin_post_dc_convert', 'dc_do_conversion');
  13. function dc_do_conversion() {
  14. ?>
  15.     <!DOCTYPE html>
  16.     <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
  17.     <head>
  18.     <meta charset="utf-8" />
  19.     <meta name="viewport" content="width=device-width, initial-scale=1">
  20.     <title>Conversion Result</title>
  21.     </head>
  22.     <body>
  23.     <h1>Conversion Results</h1>
  24. <?php
  25.     if ( ! check_admin_referer('dc_convert')) wp_die('Invalid form data');
  26.     if ( ! current_user_can('manage_options')) wp_die('You do not have proper user capability to do this.');
  27.     if ( ! empty( $_POST )) {
  28.         $source = stripslashes( $_POST['dc-source']);
  29.         if (''== $source) {
  30.             echo 'Content is empty!';
  31.         } else {
  32.  
  33.         // do a bunch of processing
  34.  
  35. echo <<<EOT
  36. $content
  37. EOT;
  38. // EOT; must be at col 0!
  39.  
  40.         } // if empty data
  41.     } // if 'POST'
  42. ?>
  43. </body>
  44. </html>
  45. <?php
  46.     exit;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement