我正在尝试从我的管理设置页面上发布一个新插件的请求。
提交后,以下代码将我带到一个空白页(URL为:http://192.168.1.33:3000/wptest2/wp-admin/admin-post.php).
没有var转储,没有重定向到google。没有控制台错误,我在日志中也没有看到任何apache错误。我怀疑表单没有提交到正确的目的地。
<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.com\'); // no redirect
exit; // removing this has no effect either
}
add_action(\'admin_post_handleNewCoupon\', \'setupCouponTargetImageUpload\');
这是在Chrome的“网络”选项卡中向管理员发布的请求。有人能发现这个请求的问题吗?
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
主插件文件
<?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.com\'); // 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\';