Solved

Wordpress PHP Syntax Help

Posted on 2014-03-23
8
279 Views
Last Modified: 2014-04-08
I have a Wordpress installation, and the plugin "Easy Digital Download".
I am trying to create a custom function but for some reason it won't work the way I have written it.

The custom function is to validate two fields. If the first field is set the second field must be set. The following two examples work, but the third one does not.  It just stops the page.

Example one (Works)
// CUSTOM VALIDATION

function rob_edd_validate_custom_fields() {

	if(isset($_POST['item_one'])) {
		echo "HELLO";
		edd_set_error( 'empty_color_description', __('You have chosen to change Item #1 ', 'rob_edd') );
	}
}
add_action('edd_checkout_error_checks', 'rob_edd_validate_custom_fields');

Open in new window


EXAMPLE TWO (Works)
// CUSTOM VALIDATION

function rob_edd_validate_custom_fields() {

	if(!isset($_POST['item_two'])) {
		echo "HELLO";
		edd_set_error( 'empty_color_description', __('You have chosen to change Item #2 ', 'rob_edd') );
	}
}
add_action('edd_checkout_error_checks', 'rob_edd_validate_custom_fields');

Open in new window


EXAMPLE THREE (Not Working) it just stops @HELLO.
[code]
// CUSTOM VALIDATION

function rob_edd_validate_custom_fields() {

	if(isset($_POST['item_one']) && $_POST['item_two'] == '') {
        echo"HELLO";
		edd_set_error( 'empty_color_description', __('You have chosen to change Item #1 ', 'rob_edd') );
	}
}
add_action('edd_checkout_error_checks', 'rob_edd_validate_custom_fields');

Open in new window

Any ideas? Also, let me know if you need any additional information.
0
Comment
Question by:rgranlund
  • 3
  • 3
  • 2
8 Comments
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39948896
Any other debugging info?  Warnings, error messages?

Can you turn on WordPress's debug mode and try again.
0
 
LVL 7

Author Comment

by:rgranlund
ID: 39948921
I turned it on and actually made sure php error reporting code is everywhere. It just skips over the second condition, like it is not there.  As long as the first condition is met, it does not matter if the second is true or false, it just runs.
0
 
LVL 15

Expert Comment

by:eemit
ID: 39948988
What is wrong with:
if(isset($_POST['item_one']) && !isset($_POST['item_two'])) {
0
 
LVL 7

Author Comment

by:rgranlund
ID: 39949001
For some reason it is stripping the text value from the input.  So, if I have this:
<input type="text" name="item_two" value="" />
And I type in "Testing" it does not pass that along: POST

However, if I write the following:
<input type="text" name="item_two" value="Testing" />
And I type in Blah Blah Blah, it POSTS "Testing"What the heck am I missing?

But radio Buttons and Check boxes work fine.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 15

Accepted Solution

by:
eemit earned 500 total points
ID: 39949041
Try:
if(isset($_POST['item_one']) && empty($_POST['item_two'])) {
0
 
LVL 7

Author Comment

by:rgranlund
ID: 39949060
Wordpress is stripping out anything I place in the text box. No matter what I put in there it deletes it.  Is there a sanitize setting or something?
0
 
LVL 15

Expert Comment

by:eemit
ID: 39949172
I'm not sure, but I think you should do the following:
Change:
add_action('edd_checkout_error_checks', 'rob_edd_validate_custom_fields');
To:
add_action('edd_checkout_error_checks', 'rob_edd_validate_custom_fields', 10, 2);

Change:
function rob_edd_validate_custom_fields() {
To:
function rob_edd_validate_custom_fields($valid_data, $passed_post) {

In function rob_edd_validate_custom_fields use $passed_post instead of $_POST.

do_action is defined with 2 arguments:
// Allow themes and plugins to hook to errors
do_action( 'edd_checkout_error_checks', $valid_data, $_POST );
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39949175
What happens if you remove the value attribute from item_two completely?

<input type="text" name="item_two" />
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now