2011-06-19 51 views
0

我有下面这段代码:当我刷新页面时,为什么我的get_option wordpress函数返回空值?

<?php function wp_copickpage() 
{ color_option_update(); ?> 

<form method="POST" action=""> 
    <?php if (get_option('custom_bg_color') != null) {?> 
     <input type="text" id="color" name="color" value="<?php echo get_option('custom_bg_color'); ?>" /> <?php } 
    else { ?> 
     <input type="text" id="color" name="color" value="<?php echo get_option('custom_bg_color'); ?>" /> 
    <?php } ?> 
    <p><input type="submit" name="search" value="Update Options" class="button" /></p> 
</form> 

<div id="colorpicker"></div> 

<?php echo('Color:'); echo get_option('custom_bg_color'); ?> 

<link rel="stylesheet" type="text/css" href="<?php echo get_bloginfo('template_url');?>/farbtastic.css"> 
<script src="<?php echo get_bloginfo('template_url');?>/jquery-1.4.4.min.js"></script> 
<script type="text/javascript" src="<?php echo get_bloginfo('template_url');?>/farbtastic.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#colorpicker').farbtastic('#color'); 
    }); 
</script> 

<?php }//end of function wp_copickpage 

//save the selected color in a wordpress option 
function color_option_update() 
{ update_option('custom_bg_color', $_POST['color']);} 
?> 

一切工作正常,除非我刷新页面,get_option的(“custom_bg_color”)的值返回null。 但是,如果我按下更新按钮,它将返回所需的值。但是,如果我重新加载页面,get_option('custom_bg_color')的值回到null。

我的update_option有什么问题吗?我在这里错过了什么?

回答

1

它看起来像您所呼叫

color_option_update() 
每次页面加载时间

,这意味着当你第一次加载它被设置为null,除非您更新的页面(因为没有$ _ POST ['颜色']变量,因为没有人提交一个在这个页面加载)。

试试这个:

<?php function wp_copickpage() 
{ 

//heres the changed part 
if($_POST['color'] && $_POST['color'] != null) { 
    color_option_update(); 
} 
?> 

<form method="POST" action=""> 
    <?php if (get_option('custom_bg_color') != null) {?> 
     <input type="text" id="color" name="color" value="<?php echo get_option('custom_bg_color'); ?>" /> <?php } 
    else { ?> 
     <input type="text" id="color" name="color" value="<?php echo get_option('custom_bg_color'); ?>" /> 
    <?php } ?> 
    <p><input type="submit" name="search" value="Update Options" class="button" /></p> 
</form> 

<div id="colorpicker"></div> 

<?php echo('Color:'); echo get_option('custom_bg_color'); ?> 

<link rel="stylesheet" type="text/css" href="<?php echo get_bloginfo('template_url');?>/farbtastic.css"> 
<script src="<?php echo get_bloginfo('template_url');?>/jquery-1.4.4.min.js"></script> 
<script type="text/javascript" src="<?php echo get_bloginfo('template_url');?>/farbtastic.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#colorpicker').farbtastic('#color'); 
    }); 
</script> 

<?php }//end of function wp_copickpage 

//save the selected color in a wordpress option 
function color_option_update() 
{ update_option('custom_bg_color', $_POST['color']);} 
?> 

编辑:去了我过去的答案,以供将来参考的更好的方式来写这将是参数化color_option_update功能,而不是直接使用POST数据。所以

//save the selected color in a wordpress option 
function color_option_update() 
{ update_option('custom_bg_color', $_POST['color']);} 

变为

//save the selected color in a wordpress option 
function color_option_update($color) 
{ update_option('custom_bg_color', $color);} 

然后将此

//heres the changed part 
if($_POST['color'] && $_POST['color'] != null) { 
    color_option_update(); 
} 

成为

//heres the changed part 
if($_POST['color'] && $_POST['color'] != null) { 
    color_option_update($_POST['color']); 
} 
+0

哦,太感谢你了!它现在有效。 从来没有意识到它是如此简单。 – baby

相关问题