Error in a link in Code block

Not sure that it is a Cwicly error and it is a tiny bug.
Here is the code error of a Code Block:
Notice : Undefined variable: download_pdf in /home/brga7543/smatchcard.com/wp-content/plugins/cwicly/core/includes/cc-ground.php(2113) : eval()'d code on line 19

The code error comes from the "variable $download_pdf . This variable is correctly defined and returns frontend the right path :
<a class="div-c4ca6df button_1" href="' . $download_pdf . '" target="_self">

Environment info

  • WordPress version: 6.0.2
  • Gutenberg Plugin version: not installed
  • Cwicly Plugin version: 1.2.1.4
  • Cwicly Theme version: 1.0.3

Hello @weedor,

I’m not sure I quite understand.
Is this error causing the code block to crash or is it simply returning this as the warning?

If so, I would need to see more of the content of the code block as :

<a class="div-c4ca6df button_1" href="' . $download_pdf . '" target="_self">

will trigger an error if there is nothing more.

It is just a warning message, because the link works fine.
Here is the code in the code block:

<?php
	global $post;
	$user = wp_get_current_user();
    $user_login= './wp-content/uploads/tickets-sent/' . $user->user_login;
	$product_slug = $post->post_name;
   	$scandir = scandir( $user_login );
	foreach( $scandir as $file_name ) {
		$file_name_min = strtolower( $file_name );
		if ( ( substr( $file_name_min, -3 ) == "pdf" ) ) {
			if ( substr( $file_name_min, 0, strlen( $product_slug ) )  == $product_slug ) {
				$download_pdf = '/wp-content/uploads/tickets-sent/' . $user->user_login . '/' . $file_name;
			}
		}
	}
echo '<a class="div-c4ca6df button_1" href="' . $download_pdf . '" target="_self">Link to download</a>';
?>

Reading the code, I see that I can optimize it…
I will but I don’t that will prevent the error message.

Hi @weedor,

You need to give a default value to your variable if pdf link is not found, that is the error here. Otherwise, the variable might be undefined in certain cases.
Moving this out of bugs since the error is user related.
The warning is correct.

2 Likes

That’s great. Thank you @Louis
You are right, that works fine. There is no value in the backend and in the frontend the value is always fulfilled: no error frontend, error backend.