0

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.com'); // 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.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

(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.com/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.

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

  2. <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.

  • Looks like there is another issue too. I commented out wp_redirect() and also exit;. After restarting the server and hard refreshing the page I still get the blank page, no console log or var dump. – Sean Dezoysa Apr 20 at 3:12
  • How about changing the action name, from handleNewCoupon to something like wpse_334842 ? Or maybe a super simple function: function setupCouponTargetImageUpload() { echo 'Yay, it works!'; exit; } ? – Sally CJ Apr 20 at 3:16
  • Did the updated answer help you?? Or are you still experiencing the same issue? – Sally CJ Apr 20 at 5:23
  • No, commenting the other functions and just doing a console log doesn't work either. All the debug and debug log show now errors. There must be an issue with routing somewhere – Sean Dezoysa Apr 20 at 17:11
  • Check the "UPDATE 2" part in the answer. That might help. And let me know. – Sally CJ Apr 20 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.