Solved

Wordpress PHP Syntax Help

Posted on 2014-03-23
8
283 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
updating the date data 12 23
Get Duration of last Status Update 4 31
Server time zone 4 19
using php variable inside javascript 5 15
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 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

18 Experts available now in Live!

Get 1:1 Help Now