Solved

Wordpress PHP Syntax Help

Posted on 2014-03-23
8
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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 Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…

627 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