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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'); 
	        
            } } ?>

 

 

Link to comment
Share on other sites

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); 
}
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.