php line in a css, but the css comes from php
Get the solution ↓↓↓I had the issue, that I want to add a get_template_directory_uri() in a css line. This Css line is located in a boxes.php file.
$box_first = 'linear-gradient(rgba(0, 175, 227, .8), rgba(0, 175, 227, .8)),
url("/wp/wp-content/themes/my_theme/inc/img/boxes/box2.png") center;';
I also tried already to make a variable and put the variable in
$tpl_dir = echo get_template_directory_uri() .
'/inc/img/boxes/box2.png';
$box_first = 'linear-gradient(rgba(0, 175, 227, .8), rgba(0, 175, 227, .8)),
url("$tpl_dir") center;';
, without any success..
Do you have any conclusions for me?
Many thanks and stay healthy, Dennis
Answer
Solution:
Am not quite sure I got your idea. Anyway, I will point out some problems here.
If you want to assign the value returned by the function, you don't have to echo it. So thatecho
can be removed like this:
$tpl_dir = get_template_directory_uri() .
'/inc/img/boxes/box2.png';
Here, the value returned by the function (ie, the url) is appended with the path to the image.
And the second problem here is, when you want to use the value from a variable, inside a string that has single quotes for enclosing the string, try to do concatenation.
Eg:
$box_first = 'linear-gradient(rgba(0, 175, 227, .8), rgba(0, 175, 227, .8)),
url("' . $tpl_dir . '") center;';
Or else, it should be enclosed in double quotes. Then only the variable inside would be parsed.
Eg:
$box_first = "linear-gradient(rgba(0, 175, 227, .8), rgba(0, 175, 227, .8)),
url(\"$tpl_dir\") center;";
Or,
$box_first = "linear-gradient(rgba(0, 175, 227, .8), rgba(0, 175, 227, .8)),
url('$tpl_dir') center;";
Also, if you are going to follow this instead of concatenation, I would suggest enclosing the variable name in curly braces.
Eg:
$box_first = "linear-gradient(rgba(0, 175, 227, .8), rgba(0, 175, 227, .8)),
url(\"{$tpl_dir}\") center;";
Now you can use the$box_first
variable to style the element. Eg:
<div class="abc_something" style="<?php echo $box_first ; ?>">blah blah blah</div>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the payload is invalid.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.