How to insert php code $_SESSION in shortcode?

时间:2018-11-13 07:54:10

标签: php wordpress

I'm trying to bring a menu with a choice of users to a WordPress page using wordcode, but I don't fully understand how to insert PHP code so that it runs inside the wordcode so that I can display this menu in any part of the topic for that I wrote like this.

add_shortcode ('T1','A2'); // шорд код
function A2(){

   $S1='session_start()';
    $R1='
    ' . $S1 . '
    <form method="POST" >
        <p>Option filter</p>
        Option 1: <input type="radio" name="p1" value="1"' . <?php  if ($_SESSION["p1"] == 1){echo "checked"; } ?> . ' />
        Option 2: <input type="radio" name="p1" value="2"' . <?php  if ($_SESSION["p1"] == 2){echo "checked"; } ?> . ' /> 
        Option 3: <input type="radio" name="p1" value="3"' . <?php  if ($_SESSION["p1"] == 3){echo "checked"; } ?> . '/>  
       <p><input type="submit" value="User"></p>
   </form>
   ';

   return  $R1;
}

but it caused an error

Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\wor\wp-content\plugins\GOG11\GOG11.php on line 19

after deleting php, it gives an error:

Parse error: syntax error, unexpected 'if' (T_IF) in C:\xampp\htdocs\wor\wp-content\plugins\GOG11\GOG11.php on line 19

And I cannot start session_start(), it is executed as plain text and if I delete ''; gives an error message:

Warning: session_start(): Cannot start session when headers already sent in C:\xampp\htdocs\wor\wp-content\plugins\GOG11\GOG11.php on line 15

Is there a way to fix this?

I tried to rewrite the code:

  session_start();


 add_shortcode ('T1','A2'); // шорд код
function A2(){




    $R1='

    <form method="POST" >
        <p>Option filter</p>
        Option 1: <input type="radio" name="p1" value="1"' . ($_SESSION["p1"] == 1 ? 'checked' : '') . ' />
        Option 2: <input type="radio" name="p1" value="2"' . ($_SESSION["p1"] == 3 ? 'checked' : '') . ' /> 
        Option 3: <input type="radio" name="p1" value="3"' . ($_SESSION["p1"] == 2 ? 'checked' : '') . '/>  
       <p><input type="submit" value="User"></p>
   </form>
   ';

   return  $R1;
}     

but now when I send something, 3 is always displayed

4 个答案:

答案 0 :(得分:1)

您不想在此处插入PHP的开始和结束标记,因为您已经在PHP上下文中。 基本上,根据会话内容,您尝试连接两个字符串。您可以使用以下任何一种三元运算符(基本上是内联if-else)来做到这一点:

$R1 = ' ...some text... ' . ($_SESSION["p1"] == 1 ? 'checked' : '') . ' ...some more text... ';

(类似于“如果会话p1等于1,则检查字符串,否则追加空字符串”)

或者您也可以像这样拆分内容:

$R1 = ' ...some text...';

if( $_SESSION["p1"] == 1 ){
    $R1 .= 'checked';
}

$1 .= ' ...some more text...';

答案 1 :(得分:1)

似乎存在字符串连接问题。

add_shortcode ('T1','A2'); // шорд код
 function A2(){
  $p1 = ($_SESSION["p1"] == 1)?"checked":"";
  $p2 = ($_SESSION["p2"] == 2)?"checked":"";
  $p3 = ($_SESSION["p3"] == 3)?"checked":"";

$R1 =''. $S1 .'
 <form method="POST" >
 <p>Option filter</p>
Option 1: <input type="radio" name="p1" value="1" '.$p1.' />
Option 2: <input type="radio" name="p1" value="2" '.$p2.' /> 
Option 3: <input type="radio" name="p1" value="3" '.$p3.'/>  
   <p><input type="submit" value="User"></p>
</form>';

 $S1='session_start()';
     return  $R1;
 }

答案 2 :(得分:1)

您没有说错误是什么,但是首先,您需要在使用$ _SESSION之前启动会话,而不是之后,还需要使用isset()检查密钥是否存在于会话中

  add_shortcode ('T1','A2'); // шорд код
 function A2(){
     $S1 = '';
     if ( ! session_id() ) {
         $S1 = session_start();
     }
$R1=' 
' . $S1 . '


 <form method="POST" >
 <p>Option filter</p>

Option 1: <input type="radio" name="p1" value="1"' . <?php  if ( isset( $_SESSION['p1'] ) && $_SESSION["p1"] == 1){echo "checked"; } ?> . ' />
Option 2: <input type="radio" name="p1" value="2"' . <?php  if ( isset( $_SESSION['p1'] ) && $_SESSION["p1"] == 2){echo "checked"; } ?> . ' /> 
Option 3: <input type="radio" name="p1" value="3"' . <?php  if ( isset( $_SESSION['p1'] ) && $_SESSION["p1"] == 3){echo "checked"; } ?> . '/>  

   <p><input type="submit" value="User"></p>
</form>
 ';

     return  $R1;

 }

答案 3 :(得分:0)

我想我很感谢所有帮助。

  session_start();
  if ($_POST['p1']){    
$_SESSION['p1'] = $_POST['p1'];
}

 add_shortcode ('T1','A2');  
function A2(){




    $R1='

    <form method="POST" >
        <p>Option filter</p>
        Option 1: <input type="radio" name="p1" value="1"' . ($_SESSION["p1"] == 1 ? 'checked' : '') . ' />
        Option 2: <input type="radio" name="p1" value="2"' . ($_SESSION["p1"] == 2 ? 'checked' : '') . ' /> 
        Option 3: <input type="radio" name="p1" value="3"' . ($_SESSION["p1"] == 3 ? 'checked' : '') . '/>  
       <p><input type="submit" value="User"></p>
   </form>
   ';

   return  $R1;
}