duandianzhong8315 2013-07-17 10:47
浏览 132

在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:

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.

  • 写回答

1条回答 默认 最新

  • dongpo7467 2013-07-17 11:09
    关注

    Read this tutorial on wordpress, this tutorial also shows how to set and get option.

    http://codex.wordpress.org/Creating_Options_Pages

    I suggest you to use Option framework http://leemason.github.io/

    This option framework is easy to use and very flexible.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。