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!
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.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?