WordPress Custom Form – Getting Query Vars, Weird Glitch?

I want to start by saying thank you to the SE community for helping out over the years.
I am trying to create a feature for our website that takes a customer zip code and updates the product inventory based on which of our partner distribution centers will fill the order. The form is simple, just a text box and a submit button. I have been working on it for a couple of weeks, but I can’t get the form to submit properly. I am using CF7 with Constant Contact plugin and Redirect for CF7 together as part of another feature, I am concerned that may be part of the problem.
Here is the form code:

<div id="zipsFormBox">
    <form id="customerZip" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">
        <label for="customerZip">Zip Code</label>
        <input type="text" name="customerZip" id="customerZip" placeholder="Enter Your Zip" required>
        <input type="hidden" name="action" value="zipsForm">
        <button type="submit">Submit</button>
    </form>
</div>

And the result:
Error Page

The query var is there, but as you can see it is giving a 404.
It is supposed to run this (from functions.php):

add_action( 'admin_post_nopriv_zipsForm', 'process_form_data;');
add_action( 'admin_post_zipsForm', 'process_form_data');

…and process with:

function process_form_data() {
        $customerZip = get_query_arg('customerZip');
        if ( isset( $customerZip ) ) {      
            $submit_result = wp_unslash($customerZip);          
            echo("<br><p>" . "You Entered" . " " . $submit_result . " " . "as your zip code." . "</p>");
            wp_die();
        }       

However, when I messed up and did this:


<div id="zipsFormBox">
    <form 
        <label for="customerZip">Zip Code</label>
        <input type="text" name="customerZip" id="customerZip" placeholder="Enter Your Zip" required>
        <input type="hidden" name="action" value="zipsForm">
        <button type="submit">Submit</button>
    </form>
</div>

I get:
Success

The form submits, query var goes to URL and the page reloads. Other than neither form running my PHP function, it does what I expect. I was hoping somebody could help me understand what is going on here. My thought is that the 404 may be coming from the action code in the form tag.
Any help is greatly appreciated!

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

WordPress Custom Form – Getting Query Vars, Weird Glitch?

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.