Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

Wordpress PHP Syntax Help

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
rgranlund
Asked:
rgranlund
  • 3
  • 3
  • 2
1 Solution
 
Jason C. LevineNo oneCommented:
Any other debugging info?  Warnings, error messages?

Can you turn on WordPress's debug mode and try again.
0
 
rgranlundAuthor Commented:
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
 
eemitCommented:
What is wrong with:
if(isset($_POST['item_one']) && !isset($_POST['item_two'])) {
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
rgranlundAuthor Commented:
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
 
eemitCommented:
Try:
if(isset($_POST['item_one']) && empty($_POST['item_two'])) {
0
 
rgranlundAuthor Commented:
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
 
eemitCommented:
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
 
Jason C. LevineNo oneCommented:
What happens if you remove the value attribute from item_two completely?

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now