Jump to content

PHP conditional statement to check value of plugin


tracedef

Recommended Posts

We have a wordpress plugin that enables users to plugin in values in the wordpress dashboard that are then populated in our theme template using the following tag

<?php if( function_exists('lp_tag') ) { echo lp_tag('phone');  } else {}?>

. In this example it will echo the phone number, other tags are 'address', 'hours', etc. This enables us to hard code the plugin call into the template but enable users to control the values without having to touch any code via the dashboard.

 

One of the tags is 'id_number". Our goal is to check to see if "id_number" has a value that is is greater then 0. Our goal is to put together the PHP conditional statement that does the following: if 'id_number >' 0 then do "various code" else do "various code".

 

Something along the lines of the following, but obviously with some corrections as we're definitely a bit off with what we have come up with:

 

?php if ( lp_tags('id_number') > 0 ); ?>
<link rel="stylesheet" type="text/css" media="screen" href="style1.css" />
<?php else ?>
<link rel="stylesheet" type="text/css" href="style2.css" />
<?php endif; ?>

 

Any feedback on the correct way to write this statement is greatly appreciated!

You were close; I corrected your code:

<?php if ( lp_tags('id_number') > 0 ): ?>
<link rel="stylesheet" type="text/css" media="screen" href="style1.css" />
<?php else: ?>
<link rel="stylesheet" type="text/css" href="style2.css" />
<?php endif; ?>

 

You placed a semicolin instead of a colin after the first if, and you needed a colin after the else also.

if ( lp_tags('id_number') > 0 ) {
    echo '<link rel="stylesheet" type="text/css" media="screen" href="style1.css" />';
} else {
    echo '<link rel="stylesheet" type="text/css" href="style2.css" />';
}

 

It's more preferable to use conditional blocks, As you'd be able to spot obvious errors easier and it is more clean.Your ELSE statement was wrong.

If I want to insert a PHP call in the "else" part of the statement the following works, but it probably isn't the best or pretiest way to accomplish this, any feedback on a more correct way to insert a php call in the else statement?

 

<?php if ( lp_tag('loansifter_id') > 0 ) {
    echo 'YES';
} else { if (function_exists('insert_cform')) { 
                    
	   insert_cform('3'); 
	        
            } } ?>

 

 

Thank you Thorpe! Finally, if I wanted to insert div's, would the following be ok in terms of best practices by simply placing it within the echo call? It works, but not sure if it is proper... thank you again for all the help!

 

<?php
if (lp_tag('loansifter_id')) {
  echo '<div>random text and html formatting</div>';
} else if (function_exists('insert_cform')) { 
  insert_cform(3); 
}
?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.