How to insert php code $_SESSION in shortcode?












0















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:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 19




after deleting php, it gives an error:




Parse error: syntax error, unexpected 'if' (T_IF) in
C:xampphtdocsworwp-contentpluginsGOG11GOG11.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:xampphtdocsworwp-contentpluginsGOG11GOG11.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










share|improve this question




















  • 2





    If something "causes an error"; why don't you share the error message with us?

    – Nico Haase
    Nov 13 '18 at 8:14
















0















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:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 19




after deleting php, it gives an error:




Parse error: syntax error, unexpected 'if' (T_IF) in
C:xampphtdocsworwp-contentpluginsGOG11GOG11.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:xampphtdocsworwp-contentpluginsGOG11GOG11.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










share|improve this question




















  • 2





    If something "causes an error"; why don't you share the error message with us?

    – Nico Haase
    Nov 13 '18 at 8:14














0












0








0


1






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:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 19




after deleting php, it gives an error:




Parse error: syntax error, unexpected 'if' (T_IF) in
C:xampphtdocsworwp-contentpluginsGOG11GOG11.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:xampphtdocsworwp-contentpluginsGOG11GOG11.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










share|improve this question
















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:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 19




after deleting php, it gives an error:




Parse error: syntax error, unexpected 'if' (T_IF) in
C:xampphtdocsworwp-contentpluginsGOG11GOG11.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:xampphtdocsworwp-contentpluginsGOG11GOG11.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







php wordpress






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 13:31









Gufran Hasan

3,54941326




3,54941326










asked Nov 13 '18 at 7:54









zeni1agentzeni1agent

356




356








  • 2





    If something "causes an error"; why don't you share the error message with us?

    – Nico Haase
    Nov 13 '18 at 8:14














  • 2





    If something "causes an error"; why don't you share the error message with us?

    – Nico Haase
    Nov 13 '18 at 8:14








2




2





If something "causes an error"; why don't you share the error message with us?

– Nico Haase
Nov 13 '18 at 8:14





If something "causes an error"; why don't you share the error message with us?

– Nico Haase
Nov 13 '18 at 8:14












4 Answers
4






active

oldest

votes


















1














You don't want to insert the PHP opening and closing tags here, since you already are in a PHP context.
Basically, you are trying to concatenate two strings, depending on your session content. You can do this with either a ternary operator (basically an inline if-else) like this:



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


(read like "if session p1 equals one, append string checked, otherwise append empty string")



or you can also split things up like this:



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

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

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





share|improve this answer































    1














    It seems there is string concatenation issue.



    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;
    }





    share|improve this answer































      1














      You didn't say what the error is but first, You need to start the session before using $_SESSION not after, You need also to check if the key exists in the session using 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;

      }





      share|improve this answer
























      • Parse error: syntax error, unexpected '<' in C:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 19 and Warning: session_start(): Cannot start session when headers already sent in C:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 17

        – zeni1agent
        Nov 13 '18 at 12:19





















      0














      I think I figured out thanks for all the help.



        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;
      }





      share|improve this answer
























      • You may want to read the "What should I do when someone answers my question?" article here stackoverflow.com/help/someone-answers

        – Fitzi
        Nov 13 '18 at 13:05











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276250%2fhow-to-insert-php-code-session-in-shortcode%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You don't want to insert the PHP opening and closing tags here, since you already are in a PHP context.
      Basically, you are trying to concatenate two strings, depending on your session content. You can do this with either a ternary operator (basically an inline if-else) like this:



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


      (read like "if session p1 equals one, append string checked, otherwise append empty string")



      or you can also split things up like this:



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

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

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





      share|improve this answer




























        1














        You don't want to insert the PHP opening and closing tags here, since you already are in a PHP context.
        Basically, you are trying to concatenate two strings, depending on your session content. You can do this with either a ternary operator (basically an inline if-else) like this:



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


        (read like "if session p1 equals one, append string checked, otherwise append empty string")



        or you can also split things up like this:



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

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

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





        share|improve this answer


























          1












          1








          1







          You don't want to insert the PHP opening and closing tags here, since you already are in a PHP context.
          Basically, you are trying to concatenate two strings, depending on your session content. You can do this with either a ternary operator (basically an inline if-else) like this:



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


          (read like "if session p1 equals one, append string checked, otherwise append empty string")



          or you can also split things up like this:



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

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

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





          share|improve this answer













          You don't want to insert the PHP opening and closing tags here, since you already are in a PHP context.
          Basically, you are trying to concatenate two strings, depending on your session content. You can do this with either a ternary operator (basically an inline if-else) like this:



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


          (read like "if session p1 equals one, append string checked, otherwise append empty string")



          or you can also split things up like this:



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

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

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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 8:11









          FitziFitzi

          35529




          35529

























              1














              It seems there is string concatenation issue.



              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;
              }





              share|improve this answer




























                1














                It seems there is string concatenation issue.



                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;
                }





                share|improve this answer


























                  1












                  1








                  1







                  It seems there is string concatenation issue.



                  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;
                  }





                  share|improve this answer













                  It seems there is string concatenation issue.



                  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;
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 8:29









                  Gufran HasanGufran Hasan

                  3,54941326




                  3,54941326























                      1














                      You didn't say what the error is but first, You need to start the session before using $_SESSION not after, You need also to check if the key exists in the session using 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;

                      }





                      share|improve this answer
























                      • Parse error: syntax error, unexpected '<' in C:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 19 and Warning: session_start(): Cannot start session when headers already sent in C:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 17

                        – zeni1agent
                        Nov 13 '18 at 12:19


















                      1














                      You didn't say what the error is but first, You need to start the session before using $_SESSION not after, You need also to check if the key exists in the session using 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;

                      }





                      share|improve this answer
























                      • Parse error: syntax error, unexpected '<' in C:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 19 and Warning: session_start(): Cannot start session when headers already sent in C:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 17

                        – zeni1agent
                        Nov 13 '18 at 12:19
















                      1












                      1








                      1







                      You didn't say what the error is but first, You need to start the session before using $_SESSION not after, You need also to check if the key exists in the session using 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;

                      }





                      share|improve this answer













                      You didn't say what the error is but first, You need to start the session before using $_SESSION not after, You need also to check if the key exists in the session using 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;

                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 13 '18 at 10:46









                      Ash0urAsh0ur

                      569128




                      569128













                      • Parse error: syntax error, unexpected '<' in C:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 19 and Warning: session_start(): Cannot start session when headers already sent in C:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 17

                        – zeni1agent
                        Nov 13 '18 at 12:19





















                      • Parse error: syntax error, unexpected '<' in C:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 19 and Warning: session_start(): Cannot start session when headers already sent in C:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 17

                        – zeni1agent
                        Nov 13 '18 at 12:19



















                      Parse error: syntax error, unexpected '<' in C:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 19 and Warning: session_start(): Cannot start session when headers already sent in C:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 17

                      – zeni1agent
                      Nov 13 '18 at 12:19







                      Parse error: syntax error, unexpected '<' in C:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 19 and Warning: session_start(): Cannot start session when headers already sent in C:xampphtdocsworwp-contentpluginsGOG11GOG11.php on line 17

                      – zeni1agent
                      Nov 13 '18 at 12:19













                      0














                      I think I figured out thanks for all the help.



                        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;
                      }





                      share|improve this answer
























                      • You may want to read the "What should I do when someone answers my question?" article here stackoverflow.com/help/someone-answers

                        – Fitzi
                        Nov 13 '18 at 13:05
















                      0














                      I think I figured out thanks for all the help.



                        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;
                      }





                      share|improve this answer
























                      • You may want to read the "What should I do when someone answers my question?" article here stackoverflow.com/help/someone-answers

                        – Fitzi
                        Nov 13 '18 at 13:05














                      0












                      0








                      0







                      I think I figured out thanks for all the help.



                        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;
                      }





                      share|improve this answer













                      I think I figured out thanks for all the help.



                        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;
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 13 '18 at 12:54









                      zeni1agentzeni1agent

                      356




                      356













                      • You may want to read the "What should I do when someone answers my question?" article here stackoverflow.com/help/someone-answers

                        – Fitzi
                        Nov 13 '18 at 13:05



















                      • You may want to read the "What should I do when someone answers my question?" article here stackoverflow.com/help/someone-answers

                        – Fitzi
                        Nov 13 '18 at 13:05

















                      You may want to read the "What should I do when someone answers my question?" article here stackoverflow.com/help/someone-answers

                      – Fitzi
                      Nov 13 '18 at 13:05





                      You may want to read the "What should I do when someone answers my question?" article here stackoverflow.com/help/someone-answers

                      – Fitzi
                      Nov 13 '18 at 13:05


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276250%2fhow-to-insert-php-code-session-in-shortcode%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Full-time equivalent

                      さくらももこ

                      13 indicted, 8 arrested in Calif. drug cartel investigation