Best way to pass options from backend to frontend in WordPress

i have a doubt that bothers me. I have added options pages in my WordPress backend. Now i am wondering, what is the best way to pass their parameters to frontend?

For example, let’s say i have a color field in my backend that saves its value to mysql.
If i wanted to assign such a color to a div,i could just get the value from mysql and echo it as a variable in my php file:

Read More
echo '<div style="color: ' . $mycolor . ';">';

But is it considered a good practice? I reckon there must be other prettier and better performing alternatives, such as exporting all the variables in a static css file everytime i save my options page.

Would love to hear suggestions!


Edit. I’m adding this to my original question to help others understand what i’m asking:

Hello! I probably wasn’t clear in my request. I already know how to create an admin-side options page (in fact, i am using Redux, which is based on the framework you suggested). I also know how to retrieve the variables in my front-end with:

$options = get_option('theme_name');
$single_option_value = $options['unique_id_of_the_admin_option_form'];

What i don’t know is the correct way to implement them.

I’ll make an example. Let’s suppose i want to give my user the possibility of setting a custom background for a section.
Admin-side, i have a media uploader form (unique id: div_background_url ).

Right now, in my front end i have the options retrieved like this:

<?php 
$options = get_option('my_theme');
$section_background_url = $options['div_background_url'];
?> 

and then the section echoed, with its custom background in the style tag:

<?php echo '<section style="background-image: url('' . $section_background_url . '');"></section>';

But i reckon that this method could generated i huge number of server requests.
Wouldn’t it be better if all of the options were saved in a css file whenever the admin changes them, and then having this file loaded in my frontend website?

Hopefully it’s a lil clearer now, i’m not a native english speaker so explaining complex stuff sometimes gets tricky.

Related posts

Leave a Reply

1 comment