Simple If/Then PHP Problem

Hello, I'm just getting started with PHP, as I need to understand it to work on my Wordpress site. I'm currently struggling with formatting this simple bit of PHP.

I have a custom field in Wordpress. First, I need this code to check if there is a value entered into that custom field. If there is, then display the following code that starts with $urlToEncode which generates a QR Code from the URL entered into the Custom Field.

If no value has been entered, then nothing should be displayed.

I think I am close, but I believe my formatting is wrong. Any suggestions would be greatly appreciated

Thank you

<?php
$customfield_name = get_post_meta($post->ID, '_ct_text_4e8275b150493');
if ($customfield_name == '') { ?>
Custom Field is Empty
<?php } else {
$urlToEncode = get_post_meta( $post->ID, '_ct_text_4e8275b150493');
generateQRwithGoogle($urlToEncode);
function generateQRwithGoogle($chl,$widhtHeight ='150',$EC_level='L',$margin='0') {
$url = urlencode($url);
echo '<img src="http://chart.apis.google.com/chart?chs='.$widhtHeight.                'x'.$widhtHeight.'&cht=qr&chld='.$EC_level.'|'.$margin.
'&chl='.$chl.'" alt="QR code" widhtHeight="'.$size.
'" widhtHeight="'.$size.'"/>';
}
?>
  • AC

    David,

    The page is loading again thanks to your help. But where the QR Code usually appears, it gives me this error:

    Fatal error: Call to undefined function generateqrwithgoogle() in /home5/apowerin/public_html/smartdepartment/wp-content/wptouch-data/themes/sdpt1/iphone/single-mobile_coupons.php on line 37

    I think I'm close. Just missing something small.

    Thanks again for your help :slight_smile:

  • Andrey

    Hi smartdept,
    It's should be like this:

    <?php
    function generateQRwithGoogle($chl,$widhtHeight ='150',$EC_level='L',$margin='0') {
    $url = urlencode($url);
    echo '<img src="http://chart.apis.google.com/chart?chs='.$widhtHeight.                'x'.$widhtHeight.'&cht=qr&chld='.$EC_level.'|'.$margin.
    '&chl='.$chl.'" alt="QR code" widhtHeight="'.$size.
    '" widhtHeight="'.$size.'"/>';
    }
    
    $customfield_name = get_post_meta($post->ID, '_ct_text_4e8275b150493');
    if ($customfield_name == '') { ?>
    Custom Field is Empty
    <?php } else {
    $urlToEncode = get_post_meta( $post->ID, '_ct_text_4e8275b150493');
    generateQRwithGoogle($urlToEncode);
    }
    ?>

    You can't declare the function in 'If' or in other blocks

  • AC

    Thank you both Andrey and David! Really appreciate your help. I found one small error in Andrey's code because the QR Code kept spitting out the word "array," so I will post the corrected version right here for everyone else to use. This is exactly what I'm using right now in a page template and it works.

    SOLVED: Use this code in a page template with CustomPress or any Post Type to turn a Custom Field value into a QR Code using the Google Charts API (Very Cool). If no value has been entered into the Field, a QR Code will not be displayed:

    Be sure to replace _ct_text_4ebd9e8da4b60 with your own Custom Field ID (In CustomPress, just look in the Custom Fields tab for this)

    <?php
    function generateQRwithGoogle($chl,$widhtHeight ='150',$EC_level='L',$margin='0') {
    $url = urlencode($url);
    echo '<img src="http://chart.apis.google.com/chart?chs='.$widhtHeight.'x'.$widhtHeight.'&cht=qr&chld='.$EC_level.'|'.$margin.
    '&chl='.$chl.'" alt="QR code" widhtHeight="'.$size.
    '" widhtHeight="'.$size.'"/>';
    }
    
    $customfield_name = get_post_meta($post->ID, '_ct_text_4ebd9e8da4b60', true);
    if ($customfield_name == '') { 
    
    } else {
    $urlToEncode = get_post_meta( $post->ID, '_ct_text_4ebd9e8da4b60', true);
    generateQRwithGoogle($urlToEncode);
    }
    ?>

    Cheers!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.