简体   繁体   English

如何在Wordpress中将变量发送到function.php

[英]How to send variables to function.php in wordpress

I want to save cookies using onclick function in my wordpress blog. 我想使用wordpress博客中的onclick功能保存cookie。 So I put this code in function.php 所以我把这段代码放在function.php中

add_action( 'init', 'setting_my_first_cookie' );

function setting_my_first_cookie() {

echo $v_username;
echo "This can be echo";
setcookie('username' , 'Rajeasdasdsdh' , time()+30*24*60*60);
}

On index.php 在index.php上

<?php 
global $v_username;
$v_username="demo"; 
$v_value="asd";
?>

<th><a href="" onclick="setting_my_first_cookie()">Set Cookie</a></th>

I am retriving this cookie using this code: 我使用以下代码检索此Cookie:

<?php
// Accessing an individual cookie value
echo $_COOKIE["username"];
?>

Everything is working but I cant set that cookies using variable. 一切正常,但我无法使用变量设置该Cookie。 I cant pass $v_username to function. 我无法通过$v_username起作用。 How to do it? 怎么做? I want echo or alert $v_username in my function setting_my_first_cookie() 我想在函数$v_username setting_my_first_cookie()回显或警告$v_username

You can use include_once or include at your index.php 您可以使用include_once或在index.php中include

Try this code for index.php : 试试下面的index.php代码:

<?php 
global $v_username;
$v_username="demo"; 
$v_value="asd";
include_once('function.php')    
?>

<th><a href="" onclick= <?php setting_my_first_cookie(); ?> >Set Cookie</a></th>

In your particular case problem is: 在您的特定情况下,问题是:

This hook in which you are hooked add_action( 'init', 'setting_my_first_cookie' ); 该钩子将钩住您的钩子add_action( 'init', 'setting_my_first_cookie' ); will run your function setting_my_first_cookie in specific place in your application, in this case, according Codex, 根据Codex,在这种情况下,将在应用程序中的特定位置运行您的函数setting_my_first_cookie

after WordPress has finished loading but before any headers are sent 在WordPress完成加载之后但在发送任何标头之前

So if you try to debug your application, you will see that setting_my_first_cookie runs before your index.php file. 因此,如果您尝试调试应用程序,则将看到setting_my_first_cookieindex.php文件之前运行。

But in general, using global variables to pass parameters to functions is not good practice. 但是总的来说,使用全局变量将参数传递给函数不是一个好习惯。

And this function will run always, so your cookie will be set always. 而且此功能将始终运行,因此将始终设置您的cookie。

OK I found the solution for me: 确定,我为我找到了解决方案:

I used java script to get PHP variable and set cookies using them. 我使用Java脚本获取PHP变量并使用它们设置cookie。 I added this code in header.php But its also working if we add it in function.php 我在header.php添加了此代码,但是如果我们在function.php添加它,它也可以工作

<script type="text/javascript"> 

function setting_my_first_cookie(variid,variname) {
 var d = new Date();
    d.setTime(d.getTime() + (30*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = variname + "=" + variid + "; " + "expires; path=/; domain=.cinemoids.com";
}
</script>

<script type="text/javascript"> 

function setting_my_first_cookie_delete(variid,variname) {
   document.cookie = variname + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/; domain=.cinemoids.com';
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM