PHP conditional displaying both outcomes

Hi,
Disclaimer: I’m a little rusty but I think I’m doing this correctly.

I’m using a plug-in to offer vouchers to paid users - the code here instances a download link; @ $download_buttons

$download_buttons = '';
if ( 1 != $voucher->require_like ) {
	$download_buttons .= '<a href="' . gtvouchers_link( $voucher->guid ) . '" class="post_img">' . $download_text . '</a>';
	if ( 1 != $voucher->require_email && 'true' == $options['listing_voucher_preview'] ) {
		$download_buttons .= '<a href="" class="post_img" style="margin-left:20px;" onclick="printPage(\'' . gtvouchers_link( $voucher->guid ) . '\');return false;">' . $print_text . '</a><br />';
	}
} else {
	$download_buttons .= $voucher->liketextcta;
	$download_buttons .= '<div id="fb-root"></div>';
	if ( 'true' == $options['listing_voucher_preview'] ) {
		$download_buttons .= gtv_encrypt_text( '<a href="' . gtvouchers_link( $voucher->guid ) . '" class="post_img">' . $download_text . '</a> <a href="" class="post_img" style="margin-left:20px;" onclick="printPage(\'' . gtvouchers_link( $voucher->guid ) . '\');">' . $print_text . '</a>' );
	} else {
		$download_buttons .= gtv_encrypt_text( '<a href="' . gtvouchers_link( $voucher->guid ) . '" class="post_img">' . $download_text . '</a>' );
	}
	$download_buttons .= '<br />';
}

When I wrap the content like so, covering the generated link, I still have the download link visible, along with the text generated by the else statement.

	$download_buttons = '';
if ( 1 != $voucher->require_like ) {
	$download_buttons .= ' <?php if (current_user_can("access_s2member_level1")){ ?> <a href="' . gtvouchers_link( $voucher->guid ) . '" class="post_img">' . $download_text . '</a>  <?php } else { ?>
<p>YOU MUST UPGRADE</p>
<?php } ?> ';
	if ( 1 != $voucher->require_email && 'true' == $options['listing_voucher_preview'] ) {
		$download_buttons .= '<a href="" class="post_img" style="margin-left:20px;" onclick="printPage(\'' . gtvouchers_link( $voucher->guid ) . '\');return false;">' . $print_text . '</a><br />';
	}
} else {
	$download_buttons .= $voucher->liketextcta;
	$download_buttons .= '<div id="fb-root"></div>';
	if ( 'true' == $options['listing_voucher_preview'] ) {
		$download_buttons .= gtv_encrypt_text( '<a href="' . gtvouchers_link( $voucher->guid ) . '" class="post_img">' . $download_text . '</a> <a href="" class="post_img" style="margin-left:20px;" onclick="printPage(\'' . gtvouchers_link( $voucher->guid ) . '\');">' . $print_text . '</a>' );
	} else {
		$download_buttons .= gtv_encrypt_text( '<a href="' . gtvouchers_link( $voucher->guid ) . '" class="post_img">' . $download_text . '</a>' );
	}
	$download_buttons .= '<br />';
}

I am previewing this with a subscriber level account (0)

Can someone help me understand the hang-up?

Try writing true without enclosing it in quotes.

Try writing true without enclosing it in quotes.

No luck with this

  1. You probably have, but make sure you caught both instances.
  2. You need to move the PHP conditional outside the variable and then set the variable.
  3. If you do need to put a PHP function within a variable, you need to concatenate it in the same way as you have with the PHP variables. You can’t use the <?php and ?> tags.

This must be:

$download_buttons .= (current_user_can("access_s2member_level1"))?'<a href="' . gtvouchers_link( $voucher->guid ) . '" class="post_img">' . $download_text . '</a>':'<p>YOU MUST UPGRADE</p>';

You can not execute a PHP code inside a string, that is a value of a variable.

@KTS915 @krumch Thanks for your help!

I have 1 additional question, (I don’t know if you’re familiar with the GT-vouchers plugin ),

After updating the code to your suggestion, 2 noticeable things happen;

  • The desired behavior is achieved in that tier 0 cannot view download and tier >=1 can :slight_smile:
  • It’s also the case however the content that should be displayed if the user is not logged is no longer being displayed.

I realize this may be an impractical question to ask, but might you have any idea what the interference may be?

UPDATE: 6/10/18
Please let me know if you have any idea about this, if not, I found an acceptable workaround.

Thank you for your help!

I am not familiar with the GT vouchers plugin. If you have found an acceptable workaround, then go with it!