0

I am writing a WordPress plugin that will display a devotional image. One of the options I would like to include is image width. I am trying to wrap the image and some text in a div so I can style it and let the user chose some different styling options. However, just trying to set the div width using the variable is not working. It says there is a syntax error.

Any help would be appreciated! Thanks, Nick.

$imageWidth = get_option('devotional_imageWidth','100%');   
    
$content = "<h2>Devotionally Images</h2>";

<div style="width:$imageWidth;">
    print '<img src="' .$show_file. '" alt="Image Title Here">';
    $content .= "devotional by Devotional.ly";          
</div>

    return $content;
}
add_shortcode('devotionally','devotionally_function');

1 Answer 1

0

There are invalid PHP codes in your sample. Try to use valid PHP code like below.

$imageWidth = get_option('devotional_imageWidth','100%');   
    
$content = "<h2>Devotionally Images</h2>";

$content .= '<div style="width:' . $imageWidth. ';">';
$content .= '<img src="' .$show_file. '" alt="Image Title Here">';
$content .= "devotional by Devotional.ly";          
$content .= '</div>';

    return $content;
2
  • Thank you so much, Hector! So each line of HTML needs to be contained within a $content?
    – Nick L
    Commented Sep 3, 2020 at 12:17
  • @Nick-L it is about PHP syntax. You can't write plain text or HTML out of single or double quotes. By using quotes you let PHP recognize HTML and texts as string.
    – Hector
    Commented Sep 3, 2020 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.