2013-04-14 41 views
0

我在worpdress主题IOS按钮开关实现,当有人点击按钮脚本加载一个PHP文件与文本:开启或关闭加载PHP文件WordPress的

下面是代码:

<script type="text/javascript"> 

$('#1').iphoneSwitch("<?php echo get_user_meta($current_user->ID, '_fbpost_status', true); ?>", 
function() { 
    $('#ajax').load('<?php bloginfo('template_url'); ?>/includes/ajaxswitch/on.php'); 
    }, 
    function() { 
    $('#ajax').load('<?php bloginfo('template_url'); ?>/includes/ajaxswitch/off.php'); 
    }, 
    { 
    switch_on_container_path: '<?php bloginfo('template_url'); ?>/includes/ajaxswitch/iphone_switch_container_off.png' 
    }); 

所以on.php只有“激活”文本和of.php“Stoped”文本。 on和off文件没有包含wordpress变量。我如何包含它们?在db中进行更新。或者你知道更好的方法吗?

+0

AJAX和内联PHP?这似乎矛盾... – elclanrs

+0

我用这个例子。 http://papermashup.com/jquery-iphone-style-ajax-switch/。你推荐我什么? –

+0

检查我的答案。 – elclanrs

回答

0

由于您使用的WordPress,这样做的propper方式是在functions.php发送一个JavaScript变量与wp_localize_script

add_action('wp_enqueue_scripts', 'my_frontend_data'); 
function my_frontend_data() 
{ 
    global $current_user; 
    wp_localize_script('data', 'Data', array(
    'userMeta' => get_user_meta($current_user->ID, '_fbpost_status', true), 
    'templateUrl' => get_template_directory_uri() 
)); 
} 

以上将增加一个JavaScript Data变量,在所有网页访问。那么你可以像这样在前端使用它:

var ajaxUrl = Data.templateUrl +'/includes/ajaxswitch/'; 

$('#1').iphoneSwitch(Data.userMeta, function() { 
    $('#ajax').load(ajaxUrl +'on.php'); 
}, function() { 
    $('#ajax').load(ajaxUrl +'off.php'); 
}, { 
    switch_on_container_path: Data.templateUrl +'iphone_switch_container_off.png'; 
}); 
+1

所以我的jquery/javascript代码是在single.php中,我尝试了你的代码,但是我没有在控制台中定义数据。我阅读wordpress文档,并且在代码中看到没有错误。 –