简体   繁体   English

Contact Form 7 在输入中检索 $_SESSION

[英]Contact Form 7 retrive $_SESSION in a input

I would need to insert within an input value, the value coming from a $ _SESSION, searching on google I found this regarding cookies:我需要在一个输入值中插入,该值来自 $ _SESSION,在谷歌上搜索我发现这个关于 cookies:

functions:职能:

function sb_cf7_cookie($atts){
    extract(shortcode_atts(array(
        'key' => -1,
    ), $atts));

    if($key == -1) return '';
    $val = '';

    if( isset( $_COOKIE[$key] ) ){
        $val = $_COOKIE[$key];
    }

    return $val;
}
add_shortcode('SB_CF7_COOKIE', 'sb_cf7_cookie');

in cf7:在 cf7 中:

[dynamichidden field-name "SB_CF7_COOKIE key='COOKIE_NAME'"]

how can make this with $ _SESSION ?如何用 $ _SESSION做这个?

this is my code for $_SESSION这是我的$_SESSION代码

function dd_register_session(){
    if (!session_id()) {
        session_start();
        if($_GET['r']){
            $_SESSION['referrer']=$_GET['r'];
        }
    }
}
add_action('after_setup_theme','dd_register_session', 1);


add_action('wp_logout', 'end_session');
add_action('wp_login', 'end_session');
add_action('end_session_action', 'end_session');

function end_session() {
    session_destroy ();
}

Here's a way to do this by rolling your own form tag.这是一种通过滚动您自己的表单标记来执行此操作的方法。 Add this to your theme functions.php将此添加到您的主题功能中。php

To use this, the form tag name would be the name of the session variable.要使用它,表单标签名称将是 session 变量的名称。 So, if you're looking for $_SESSION['referrer'] then the form tag name should be referrer所以,如果你正在寻找$_SESSION['referrer']那么表单标签名称应该是referrer

So the form tag in the contact form would be [session_var referrer]所以联系表格中的表格标签将是[session_var referrer]

In the email you would put [referrer].在 email 中,您可以输入 [referrer]。

add_action('wpcf7_init', function (){
    wpcf7_add_form_tag( 'session_var' , 'cf7_session_variable_callback', array('name-attr' => true) );
}); 
function cf7_session_variable_callback($tag){
  $tag = new WPCF7_FormTag( $tag );
  $value = (!empty($_SESSION[$tag->name])) ? $_SESSION[$tag->name] : 'not set';
  $output = '<input type="hidden" name="'.$tag->name.'" value="'.$value.'">';
  return $output;
}

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

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