How to change a color (background) with a value generated with php (in function.php)?

Hello there,

Maybe you can give me a hint, or point me in a direction:

Basically I want to change a background color of a column. This color should be calculated, depending on some factors.

I have written a small php-script, which works fine and give me the right hex-codes.
How can I now use the $result to change the color?
My first thought: I wrap the code in a shortcut.
But I can’t use a shortcut in background -> advanced -> custom, and either not in css.

Any ideas, how I can use a php generated hex code to change a color in Pro?

Thank you,
Michael

Thank you. I solved it simple by doing an echo in php to output a “…” and with an add_action.

Hi Michael,

Thanks for reaching out.
We are not clear about how your PHP script is working, but you can use the Dynamic Content tag in the background color option. So if you create a custom Dynamic Content tag which will call the PHP function returning the Hexcode may work for you. To create the Dynamic Content tag, I would suggest you go through the following thread.

Hope it helps.
Thanks

Hey tristup,

thank you. I did it quick & dirty and echo a style.
BUT: I thought about using Dynamic Content and your link is extremely helpfull!

Thank you,
Michael

You are most welcome Michael.

I’ve read (I can look it up) that with wordpress it’s better to use “return” instead of “echo” so maybe try that?

Hi Michael,

It is correct to use the return instead of echo, but still, it depends on the situation.

Thanks

@Jonson: Great hint! Thank you. Never thought about this

It seams, that this is not a wordpress issue. It is a php issue.
What I found (and this makes absolutely sense to me):

Mathias R. Jessen

On Stackoverflow

Consider the following:

<?php
function sayHelloLater(){
    return "Hello";
}

function sayGoodbyeNow(){
    echo "Goodbye";
}

$hello = sayHelloLater(); // "Hello" returned and stored in $hello 
$goodbye = sayGoodbyeNow(); // "Goodbye" is echo'ed and nothing is returned

echo $hello; // "Hello" is echo'ed
echo $goodbye; // nothing is echo'ed
?>

You might expect the output to be:

HelloGoodbye

The actual output is:

GoodbyeHello

The reason is that “Goodbye” is echo’ed (written) as output, as soon as the function is called. “Hello”, on the other hand, is returned to the $hello variable. the $goodbye is actually empty, since the goodbye function does not return anything.

@tristup:
Thank you.

Hi @mickna,

We’re glad that you’re able to understand it now! If you have any other concerns or clarifications regarding our theme features, feel free to open up a new thread.

Thank you.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.