0

I'm using admin-post.php to handle a form submission on the admin side of a WordPress plugin I'm building. I have my form set up in a function like this:

function display_charge_customer_input() {
 global $stripe_options;
  $charge_amount_btn = "Charge $" . $stripe_options['amount_to_charge'];

 ?>
  <h3 class="title"><?php _e('Charge Customer', 'kite_stripe'); ?></h3>
   <form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">
    <input type="hidden" name="action" value="charge_customer_data"/>
      <table class="form-table">
        <tbody>
          <tr valign="top">
            <th scope="row" valign="top">
              <?php _e('Charge Customer', 'kite_stripe'); ?>
            </th>
            <td>
              <input id="charge_customer_input" name="charge_customer_input" type="text" class="regular-text" value="Customer Id"/>
              <label class="description" for="charge_customer_input"><?php _e('Paste the customer id of the person you\'d like to charge.', 'kite_stripe'); ?></label>
            </td>
          </tr>
        </tbody>
      </table>

      <p class="submit_charge">
        <input type="submit" class="button-primary" name = "charge_submit" value="<?php _e($charge_amount_btn, 'kite_domain') ?>" />
      </p>
  </form>
<?php

}

I'm calling the function from another file to have it show up on the admin page. That is all working fine.

Now here's my code that is supposed to handle the form request:

add_action( 'admin_post_charge_customer_data', 'charge_customer_data' );
add_action( 'admin_post_nopriv_charge_customer_data', 'charge_customer_data' );
function charge_customer_data() {
 if (isset($_POST['charge_submit'])){
   $url = "http://localhost:8888/wp-admin/admin.php?page=user-list-table.php";
   wp_redirect($url);
   exit;
 }
}

I'm going to have the form save user input as well but I just wanted to get the redirect working first. I've tried reading everything I could on Stack Overflow and Google searches and I still can't figure out where I'm going wrong. I'm thinking maybe my if(isset($_POST['charge_submit'])) code just isn't working when the submit button isn't clicked but I don't see why it wouldn't.

I'm pretty new to all this so any help would be appreciated. Thank you!

4
  • 1
    the way I debug such things is by var_dumping and dieing. Than you can see what are the values being set and the flow of the code that handles the request. It might be that you set the hooks at a wrong time and the code is not reached at all. Commented Apr 18, 2018 at 16:09
  • That's a good suggestion...my problem though is that I can't figure out a good place to debug. If I var_dump anywhere it's still not showing up because when I hit submit, it takes me right to the admin-post.php white page. I tried testing it within the form/function but it's the same issue.
    – Coltvant
    Commented Apr 18, 2018 at 16:18
  • Ah I figured out that if I put my second function charge_customer_data() into functions.php it will work right. However, I would really like to keep all my functions in my plugin directory...is that possible?
    – Coltvant
    Commented Apr 18, 2018 at 16:31
  • this is why you die after the var_dump. If nothing is printed it means that most likely you didn't get there at all Commented Apr 18, 2018 at 20:54

1 Answer 1

1

So I figured this out...I realized I was adding my charge_customer_data() function within a class and that it was never being called. I created a new file in my plugin directory separate from the class, added the function, and it worked.

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.