r/PHPhelp • u/DownFromHere • Apr 02 '26
Solved Basic Beginner Question for Form Issue with PHP on WordPress Part 2
UPDATE - SOLVED:
A couple things were needed to fix this in addition to everyone's advice.
I had to copy the original HTML form and place it in <?php ?> brackets in the Wordpress php plugin (CSS and Javascript ToolBox) with the ECHO function, then finish each statement using a semicolon when I needed to use php specific function because the Wordpress clode block doesn't work for PHP.
So I had to write php in two places- my plugin and on a file on my computer. I assumed writing to my computer wasn't successful, but I should have checked with ECHO and PRINT statements.
I had a problem where submission redirected to another page. I fixed this when I left action empty as action="" instead of the name the php file, even though PHP_SELF showed me the file where the PHP actions were performed.
I'm wonder if a function like file_get_content() could help me add the php I wrote locally to the plugin php so it works better.
Strangely enough, I did not need to use the add_action() function for the form to work.
The PHP I wrote in the plugin which is majority HTML
<?php
//$b = htmlspecialchars($_SERVER["PHP_SELF"]); while PHP_SELF helped me find the file location, empty action worked better
$c = wp_nonce_field("form_response","form_nonce");
//echo $a;
echo '<div id="form">';
echo '<section><form id="Form3" method="POST" action="';
//echo $b; while PHP_SELF helped me find the file location, empty action worked better and did not open a new page
echo '"><br>';
echo $c;
echo '<input type="hidden" name="action" value="form_response">
<ol id="form2">
<li><label for="choice1">choice 1 </label><input id="choice1" class="choices" name="choice1" type="text" />
<ul id="choice1Info" class="choicesInfo">
<li>Information about Choice 1</li>
</ul>
</li>
<li><label for="choice2">choice 2</label><input id="choice2" class="choices" name="choice2" type="text" value="';
echo '"/> <ul id="choices2Info" class="choicesInfo"> <li>Information about Choice 2</li> </ul> </li> </ol> <input type="submit" value="Submit" name="submit"/> </form></section><br>;
The PHP I wrote locally below Wordpress' pre-existing code
//my code
if (($_SERVER['REQUEST_METHOD']) == "POST"){
for ($i=1; $i <= 2; $i++) {
$lower_choices = "choice".strval($i);
$upper_Choices ="Choice".strval($i);
$$upper_Choices = $_POST[strval($lower_choices)];
check_empty($$upper_Choices,$i);
}
}
function check_first($input){
$input = sanitize_text_field($input);
$input = trim($input);
$input = stripslashes($input);
$input = htmlspecialchars($input);
}
function nonce_submission($input){
if ( empty($_POST) ||
! wp_verify_nonce( $_POST['form_nonce'], 'form_response') ||
! ctype_alnum(str_replace(' ', '', $input))){
print 'Verification failed. Try again.';
exit;
}
else {
check_first($input);
print "<h1>HELLO WORLD</h1>";
}
}
function check_empty($s,$i){
if (empty($s)){
print "Choice ".strval($i)." needs to be completed!<br>";
} else {
nonce_submission($s);
print ($s);
}
}
---------
Thanks to everyone who gave advice to help me with my form handling issues with PHP under this post earlier this week.
TLDR: I'm trying to make a simple form that echo the input via multiple methods in php to no avail
Unfortunately, I am still encountering issues. Someone recommended I use echo error_reporting(E_ALL); in the php plugin and according to this website, the 4983 code means there's no error but my form still does not work.
Disabling the JavaScript did not help.
A few people recommended coding through the /admin.php files. So I checked a few websites and I followed this article. I put my code in /admin-post.php It didn't work.
I tried to add safety measures but when I interact with my page through View Page Source, wp_nonce_field() and esc_url didn't seem to successfully pass to the HTML.
My updated html code is below:
<form id="form1" method="POST" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>">
<?php wp_nonce_field("it_works","form_nonce"); ?>
<input type="hidden" name="action" value="form_response">
<ol id="form2">
<li><label for="choice1">choice 1 </label><input id="choice1" class="choices" name="choice1" type="text" />
<ul id="choice1Info" class="choicesInfo">
<li>information about choice 1</li>
</ul>
</ol>
<input type="submit" value="Submit"/>
</form>
My php script I added to the php file (<?php ... ?> is omitted)
/*my code*/
function it_works(){
if ( empty($_POST) || ! wp_verify_nonce( $_POST['form_nonce'], 'form_response') ){
print 'Verification failed. Try again.';
exit;
}
else {
$choice1= sanitize_text_field(isset($_POST['choice1'])) ? sanitize_text_field($_POST['choice1']) : 'not registered';
ctype_alnum($choice1) ? echo It works!: die("Input not verified");
// data submitted should traditional alphanumerics, no special characters
}
}
add_action("form_response","it_works");
This has been driving me crazy. I'd appreciate any help. I'm not sure if it's my inexperience or something with WPEngine.
At this point I'm considering adding an extra plug-in just so I can edit its php files directly and see if that will get the form to work but WordPress says that's not recommended because it can break my site but if it doesn't work, can I just delete the plug-in?
7
u/colshrapnel Apr 02 '26
You didn't show much engagement in the other post, so it is safe to assume that any effort put into this one will be wasted as well. Go back and answer questions you were asked there.
As a general recommendation, if something doesn't work, you have to make it SIMPLER, reducing the number of matters to deal with. And your new code adds LOTS of garbage such as WP stuff. Stick to your initial example with raw php.
6
u/equilni Apr 02 '26
You got some horrible advise.
Go back to my comment in the previous post and test the form in a new folder - name the file
index.phpand test. I suspect you have a basic environment issue.Adding WP here doesn't help if you can't get the basics correct.
5
u/Big-Dragonfly-3700 Apr 02 '26
What is your overall goal for doing this? To learn to program using PHP or to learn how to do this in a WP environment? You will need to first learn how to use PHP at all, before trying it in a WP environment.
For either of these, you should be doing this on a localhost development system (where you will have control over the environment that the code runs in), not on a live/public server (where you may not have control over what the web hosting has done.)
-3
u/SaduWasTaken Apr 02 '26
Have you tried banging that entire post into Claude and seeing if it can point you in the right direction?
It eats these kinds of questions for breakfast.
-1
u/AzozzALFiras Apr 04 '26
It seems you are running into several common pitfalls when integrating custom PHP with the WordPress "Hooks" system. The reason your code isn't executing is likely due to how WordPress handles form submissions via
admin-post.php.Here is the "no-nonsense" fix to get your form working:
1. The HTML Fix
In your form, you are using
wp_nonce_field("it_works",...), but in your PHP, you are checking forform_response. These must match. Also, the PHP tags won't execute inside a standard "Custom HTML" block in the Gutenberg editor unless you are using a specific PHP-execution plugin.Corrected Form:
HTML
2. The PHP Fix (The missing "Hook")
When you submit to
admin-post.php, WordPress looks for an action namedadmin_post_{your_action_value}. You missed theadmin_post_prefix in youradd_action.Corrected PHP Script:
PHP
3. Why it’s failing on WPEngine/WordPress:
admin_post_in youradd_action, WordPress has no idea that your functionit_worksis supposed to handle theform_responserequest.admin_post_form_responsewill ignore you. You must add theadmin_post_nopriv_version.functions.phpor use a "Code Snippets" plugin.Regarding your plugin question: Yes, you can create a simple custom plugin. If it breaks the site, you can just delete the folder via FTP or WPEngine's file manager and the site will come back instantly.
Quick Test: Try changing the
actionin your HTML to a simple external URL. If it reaches that URL, your HTML is fine and the issue is 100% theadd_actionnaming in your WordPress PHP.2
u/[deleted] Apr 02 '26
[removed] — view removed comment