Search code examples
phpwordpressconcatenation

Problem with concatenation in path with a variable in PHP Wordpress Plugin


I am using Wordpress plugin for invoices. This plugin has customisable templates you can edit. Inside this template I am fetching variable and then including it in a path that points to desired image that is then added to the template.

<?php 
    $order_id_image = $this->order_number();
    $order_id_image_num = strval($order_id_image);
    echo '<div>'.$order_id_image.'</div>';
    
    $file_path_qr_image_base = 'wp-content/uploads/qr_upn_private/'.$order_id_image_num.".png";
    $base_image = file_get_contents($file_path_qr_image_base);
    echo '<div>'.$file_path_qr_image_base.'</div>';
    echo '<img src="data:image/gif;base64,' . $base_image . '" />';
    
    $order_id_image = $this->order_number();
    $file_path_qr_image_base = 'wp-content/uploads/qr_upn_private/'.$this->order_number().".png";
?>

When testing this outputs this:function output

If I write path to the image static it works $file_path_qr_image_base = 'wp-content/uploads/qr_upn_private/4213.png';

Thank you in advance :)

Solution In my specific case $this->order_number(); was returning null. All problems were getting logged in file /wp-content/debug.log This guided me to correct solution using :

$id = $order->get_id(); //to retrive id
$data = file_get_contents("/home/{your_site}/public_html/wp-content/uploads/folder/".$id.".png");
echo '<img src="data:image/png;base64,' . $data . '" style="height:250px">';

Solution

  • you can try to add site url on your image path

    $file_path_qr_image_base = site_url( "/wp-content/uploads/qr_upn_private/".$this->order_number().".png" , "https" );

    // $file_path_qr_image_base = https://www.example.com/wp-content/uploads/qr_upn_private/4213.png