向admin-post.php发出POST请求

时间:2019-04-20 作者:Sean D

我正在尝试从我的管理设置页面上发布一个新插件的请求。

提交后,以下代码将我带到一个空白页(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\';

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

(修改后的答案)

我已将我的操作更改为你的操作,但仍然不起作用。如果你想看的话,这是我对这个插件的回复。我现在不知所措:github.com/SeanDez/frequentVisitorCoupons

因此,我尝试并测试了您的插件,并注意到您有两个“操作”(即。action 字段)相同form.

  1. <input type="hidden" name="action" value="handleNewCoupon" />

  2. <input type="hidden" name="action" value="fromNewCouponForm">

    一、 e.发布的操作是fromNewCouponForm 而不是handleNewCoupon (哪个是正确的?)。既然没有什么行动admin_post_fromNewCouponForm, 然后你得到了空白(wp-admin/admin-post.php) 页

相关推荐

Testing Plugins for Multisite

我最近发布了一个WordPress插件,它在单个站点上非常有效。我被告知该插件在多站点安装上不能正常工作,我理解其中的一些原因。我已经更新了代码,现在需要一种方法来测试更新后的代码,然后才能转到实时客户的多站点安装。我有一个用于测试的WordPress安装程序的单站点安装,但需要在多站点安装上进行测试。根据我所能找到的唯一方法是在网络上至少有两个站点来安装整个多站点安装,以测试我的插件。设置WordPress的整个多站点安装是插件开发人员的唯一/首选方式,还是有更快的测试环境可用。