Solved

Wordpress PHP Syntax Help

Posted on 2014-03-23
8
289 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article discusses how to create an extensible mechanism for linked drop downs.
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

680 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