I am trying to make a post request from my admin settings page for a new plugin.
The following code takes me to a blank page after submit (URL is: http://192.168.1.33:3000/wptest2/wp-admin/admin-post.php).
No var dump, no redirect to google. No console errors and I don't see any apache errors in the log either. I suspect the form is not submiting to the correct destination.
<form
action="<?php echo
esc_url(admin_url('admin-post.php'));
?>"
method="post"
id="newCouponForm"
enctype="multipart/form-data"
>
<input
type="hidden"
name="action"
value="handleNewCoupon"
>
<button type="submit">Create New Coupon</button>
</form>
// other form fields removed
// in my main plugin file
function setupCouponTargetImageUpload() {
?>
<script>
console.log(`=====test=====`); // doesn't log
</script>
<?php
$couponType = selectCouponType();
var_dump($couponType);
echo '=====$couponType====='; // doesn't echo
if($couponType === 'image') {
insertImageCoupon();
} else if ($couponType === 'text') {
insertTextCoupon();
}
wp_redirect(''); // no redirect
exit; // removing this has no effect either
}
add_action('admin_post_handleNewCoupon', 'setupCouponTargetImageUpload');
Here is the request to admin-post in Chrome's Network tab. Can anyone spot the issue with this request?
Request URL: http://192.168.1.33:3000/wptest2/wp-admin/admin-post.php
Request Method: POST
Status Code: 200 OK
Remote Address: 192.168.1.33:3000
Referrer Policy: strict-origin-when-cross-origin
cache-control: no-cache, must-revalidate, max-age=0
connection: close
content-type: text/html; charset=UTF-8
date: Sat, 20 Apr 2019 01:45:22 GMT
expires: Wed, 11 Jan 1984 05:00:00 GMT
referrer-policy: strict-origin-when-cross-origin
server: Apache/2.4.29 (Ubuntu)
Transfer-Encoding: chunked
x-frame-options: SAMEORIGIN
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Content-Length: 18372
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarydlJEsUJRkFBYkRVN
Cookie: <removed>
Host: 192.168.1.33:3000
Origin: http://192.168.1.33:3000
Referer: http://192.168.1.33:3000/wptest2/wp-admin/options-general.php?page=fvc-settings
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36
Main Plugin File
<?php
/*
Plugin Name: Frequent Visitor Coupons
Description: Give coupons to visitors who visit your site frequently, or even a specific product page!
*/
add_action('admin_post_handleNewCoupon', 'setupCouponTargetImageUpload');
// handle the new coupon form
function uploadImage() {
// tmp_name is file contents. name is file name
$fileName = basename($_FILES['couponImage']['name']);
var_dump($fileName);
echo '=====$fileName=====';
// take the file named in the POST request and move it to './images'
move_uploaded_file(
$fileName,
plugin_dir_path(__FILE__) . '/images/'
);
// path is user for internal usage (urls for external)
};
function selectCouponType() {
// detect if there is an image being uploaded
if ($_POST['imageCoupon']) {
return 'image';
} else if ($_POST['textCouponTitleField']) {
return 'text';
} else {
// error handling
return print_r('issue in selectCouponType() function');
}
};
function insertImageCoupon() {
global $wpdb;
$fileUrl = plugin_dir_url(__FILE__) . '/images/' . $_FILES['couponImage']['name'];
$insertedCoupon = $wpdb->insert(
"{$wpdb->prefix}frequentVisitorCoupons_coupon", [
'totalHits' => 0,
'isText' => false,
'imageUrl' => $fileUrl
]
);
var_dump($insertedCoupon);
echo '=====$insertedCoupon=====';
};
function insertTextCoupon() {
global $wpdb;
$wpdb->insert("{$wpdb->prefix}frequentVisitorCoupons_coupon", [
'totalHits' => 0,
'isText' => true,
'imageUrl' => null
]);
}
function addNewTarget() {
// target insert query
};
function setupCouponTargetImageUpload() {
// $_POST and $_FILE should be available
echo 'Yay, it works!';
exit;
?>
<script>
console.log(`=====test=====`); // doesn't log
</script>
<?php
// $couponType = selectCouponType();
// var_dump($couponType);
// echo '=====$couponType====='; // doesn't echo
// upload the image URL if needed
// if($couponType === 'image') {
// insertImageCoupon();
// } else if ($couponType === 'text') {
// insertTextCoupon();
// }
// addNewTarget(); // todo work on this next
// wp_redirect(''); // no redirect
// exit;
}
// Add at the top-level..
add_action( 'admin_post_test123', 'testActionHandler');
function testActionHandler () {
echo 'Yay, it works!';
exit;
}
// hooks into admin-menu
require 'adminMenu.php';
// hooks into wp-footer
require 'frontEndRender.php';
I am trying to make a post request from my admin settings page for a new plugin.
The following code takes me to a blank page after submit (URL is: http://192.168.1.33:3000/wptest2/wp-admin/admin-post.php).
No var dump, no redirect to google. No console errors and I don't see any apache errors in the log either. I suspect the form is not submiting to the correct destination.
<form
action="<?php echo
esc_url(admin_url('admin-post.php'));
?>"
method="post"
id="newCouponForm"
enctype="multipart/form-data"
>
<input
type="hidden"
name="action"
value="handleNewCoupon"
>
<button type="submit">Create New Coupon</button>
</form>
// other form fields removed
// in my main plugin file
function setupCouponTargetImageUpload() {
?>
<script>
console.log(`=====test=====`); // doesn't log
</script>
<?php
$couponType = selectCouponType();
var_dump($couponType);
echo '=====$couponType====='; // doesn't echo
if($couponType === 'image') {
insertImageCoupon();
} else if ($couponType === 'text') {
insertTextCoupon();
}
wp_redirect('http://google'); // no redirect
exit; // removing this has no effect either
}
add_action('admin_post_handleNewCoupon', 'setupCouponTargetImageUpload');
Here is the request to admin-post in Chrome's Network tab. Can anyone spot the issue with this request?
Request URL: http://192.168.1.33:3000/wptest2/wp-admin/admin-post.php
Request Method: POST
Status Code: 200 OK
Remote Address: 192.168.1.33:3000
Referrer Policy: strict-origin-when-cross-origin
cache-control: no-cache, must-revalidate, max-age=0
connection: close
content-type: text/html; charset=UTF-8
date: Sat, 20 Apr 2019 01:45:22 GMT
expires: Wed, 11 Jan 1984 05:00:00 GMT
referrer-policy: strict-origin-when-cross-origin
server: Apache/2.4.29 (Ubuntu)
Transfer-Encoding: chunked
x-frame-options: SAMEORIGIN
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Content-Length: 18372
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarydlJEsUJRkFBYkRVN
Cookie: <removed>
Host: 192.168.1.33:3000
Origin: http://192.168.1.33:3000
Referer: http://192.168.1.33:3000/wptest2/wp-admin/options-general.php?page=fvc-settings
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36
Main Plugin File
<?php
/*
Plugin Name: Frequent Visitor Coupons
Description: Give coupons to visitors who visit your site frequently, or even a specific product page!
*/
add_action('admin_post_handleNewCoupon', 'setupCouponTargetImageUpload');
// handle the new coupon form
function uploadImage() {
// tmp_name is file contents. name is file name
$fileName = basename($_FILES['couponImage']['name']);
var_dump($fileName);
echo '=====$fileName=====';
// take the file named in the POST request and move it to './images'
move_uploaded_file(
$fileName,
plugin_dir_path(__FILE__) . '/images/'
);
// path is user for internal usage (urls for external)
};
function selectCouponType() {
// detect if there is an image being uploaded
if ($_POST['imageCoupon']) {
return 'image';
} else if ($_POST['textCouponTitleField']) {
return 'text';
} else {
// error handling
return print_r('issue in selectCouponType() function');
}
};
function insertImageCoupon() {
global $wpdb;
$fileUrl = plugin_dir_url(__FILE__) . '/images/' . $_FILES['couponImage']['name'];
$insertedCoupon = $wpdb->insert(
"{$wpdb->prefix}frequentVisitorCoupons_coupon", [
'totalHits' => 0,
'isText' => false,
'imageUrl' => $fileUrl
]
);
var_dump($insertedCoupon);
echo '=====$insertedCoupon=====';
};
function insertTextCoupon() {
global $wpdb;
$wpdb->insert("{$wpdb->prefix}frequentVisitorCoupons_coupon", [
'totalHits' => 0,
'isText' => true,
'imageUrl' => null
]);
}
function addNewTarget() {
// target insert query
};
function setupCouponTargetImageUpload() {
// $_POST and $_FILE should be available
echo 'Yay, it works!';
exit;
?>
<script>
console.log(`=====test=====`); // doesn't log
</script>
<?php
// $couponType = selectCouponType();
// var_dump($couponType);
// echo '=====$couponType====='; // doesn't echo
// upload the image URL if needed
// if($couponType === 'image') {
// insertImageCoupon();
// } else if ($couponType === 'text') {
// insertTextCoupon();
// }
// addNewTarget(); // todo work on this next
// wp_redirect('http://google'); // no redirect
// exit;
}
// Add at the top-level..
add_action( 'admin_post_test123', 'testActionHandler');
function testActionHandler () {
echo 'Yay, it works!';
exit;
}
// hooks into admin-menu
require 'adminMenu.php';
// hooks into wp-footer
require 'frontEndRender.php';
Share
Improve this question
edited Apr 21, 2019 at 0:19
Sean D
asked Apr 20, 2019 at 2:32
Sean DSean D
3878 silver badges21 bronze badges
1 Answer
Reset to default 1(Revised answer)
I have changed my action to yours but it still doesn't work. Here is my repo for this plugin if you want to see. I'm at a loss at the moment: github/SeanDez/frequentVisitorCoupons
So I tried and tested your plugin, and noticed that you've got two "actions" (i.e. action
fields) on the same form.
<input type="hidden" name="action" value="handleNewCoupon" />
<input type="hidden" name="action" value="fromNewCouponForm">
So by now, I bet you know what happened?
I.e. The posted action is fromNewCouponForm
instead of the handleNewCoupon
(which is the correct one?). And since there's no action hooked to admin_post_fromNewCouponForm
, then you got the blank (wp-admin/admin-post.php
) page.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745568592a4633544.html
评论列表(0条)