How to pass php variable to wordpress AJAX handler












0














Struggling to pass through some php variables into my ajax handler function in functions.php



Example provided below doesn't work, probably has something to do with the hooks but I can't find any info on how to do this:



/*Unsubscribe*/
$test_variable = "derp";

function user_unsubscribe($test_variable){
echo json_encode($test_variable);
wp_die();
};

add_action('wp_ajax_user_unsubscribe', 'user_unsubscribe');
add_action('wp_ajax_nopriv_user_unsubscribe', 'user_unsubscribe');









share|improve this question






















  • Try: add_action('wp_ajax_user_unsubscribe', 'user_unsubscribe', 10, 1); and add_action('wp_ajax_nopriv_user_unsubscribe', 'user_unsubscribe', 10, 1);
    – zipkundan
    Nov 23 '18 at 7:47










  • Those are set by default, tried it anyway, no luck.
    – intense7
    Nov 23 '18 at 8:06
















0














Struggling to pass through some php variables into my ajax handler function in functions.php



Example provided below doesn't work, probably has something to do with the hooks but I can't find any info on how to do this:



/*Unsubscribe*/
$test_variable = "derp";

function user_unsubscribe($test_variable){
echo json_encode($test_variable);
wp_die();
};

add_action('wp_ajax_user_unsubscribe', 'user_unsubscribe');
add_action('wp_ajax_nopriv_user_unsubscribe', 'user_unsubscribe');









share|improve this question






















  • Try: add_action('wp_ajax_user_unsubscribe', 'user_unsubscribe', 10, 1); and add_action('wp_ajax_nopriv_user_unsubscribe', 'user_unsubscribe', 10, 1);
    – zipkundan
    Nov 23 '18 at 7:47










  • Those are set by default, tried it anyway, no luck.
    – intense7
    Nov 23 '18 at 8:06














0












0








0







Struggling to pass through some php variables into my ajax handler function in functions.php



Example provided below doesn't work, probably has something to do with the hooks but I can't find any info on how to do this:



/*Unsubscribe*/
$test_variable = "derp";

function user_unsubscribe($test_variable){
echo json_encode($test_variable);
wp_die();
};

add_action('wp_ajax_user_unsubscribe', 'user_unsubscribe');
add_action('wp_ajax_nopriv_user_unsubscribe', 'user_unsubscribe');









share|improve this question













Struggling to pass through some php variables into my ajax handler function in functions.php



Example provided below doesn't work, probably has something to do with the hooks but I can't find any info on how to do this:



/*Unsubscribe*/
$test_variable = "derp";

function user_unsubscribe($test_variable){
echo json_encode($test_variable);
wp_die();
};

add_action('wp_ajax_user_unsubscribe', 'user_unsubscribe');
add_action('wp_ajax_nopriv_user_unsubscribe', 'user_unsubscribe');






php ajax wordpress






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 7:29









intense7

133




133












  • Try: add_action('wp_ajax_user_unsubscribe', 'user_unsubscribe', 10, 1); and add_action('wp_ajax_nopriv_user_unsubscribe', 'user_unsubscribe', 10, 1);
    – zipkundan
    Nov 23 '18 at 7:47










  • Those are set by default, tried it anyway, no luck.
    – intense7
    Nov 23 '18 at 8:06


















  • Try: add_action('wp_ajax_user_unsubscribe', 'user_unsubscribe', 10, 1); and add_action('wp_ajax_nopriv_user_unsubscribe', 'user_unsubscribe', 10, 1);
    – zipkundan
    Nov 23 '18 at 7:47










  • Those are set by default, tried it anyway, no luck.
    – intense7
    Nov 23 '18 at 8:06
















Try: add_action('wp_ajax_user_unsubscribe', 'user_unsubscribe', 10, 1); and add_action('wp_ajax_nopriv_user_unsubscribe', 'user_unsubscribe', 10, 1);
– zipkundan
Nov 23 '18 at 7:47




Try: add_action('wp_ajax_user_unsubscribe', 'user_unsubscribe', 10, 1); and add_action('wp_ajax_nopriv_user_unsubscribe', 'user_unsubscribe', 10, 1);
– zipkundan
Nov 23 '18 at 7:47












Those are set by default, tried it anyway, no luck.
– intense7
Nov 23 '18 at 8:06




Those are set by default, tried it anyway, no luck.
– intense7
Nov 23 '18 at 8:06












3 Answers
3






active

oldest

votes


















1














Solved it with this solution: can I pass arguments to my function through add_action?



Working code solution is:



/*Unsubscribe*/
$test_variable = "derp";

function user_unsubscribe($test_variable){
echo json_encode($test_variable);
wp_die();
};

add_action('wp_ajax_user_unsubscribe', function() use ($test_variable){
user_unsubscribe($test_variable);
});

add_action('wp_ajax_nopriv_user_unsubscribe', function() use ($test_variable){
user_unsubscribe($test_variable);
});





share|improve this answer





























    0














    The prefered way to pass variables to ajax is to add them to the request and read them from $_GET or $_POST official documentation



    If you need other variables you'll either have to use globals or call a extra function.



    favorite



    Struggling to pass through some php variables into my ajax handler function in functions.php



    Example provided below doesn't work, probably has something to do with the hooks but I can't find any info on how to do this:



    function user_unsubscribe(){
    $test_variable = get_test_variable();

    echo json_encode($test_variable);
    wp_die();
    };

    add_action('wp_ajax_user_unsubscribe', 'user_unsubscribe');
    add_action('wp_ajax_nopriv_user_unsubscribe', 'user_unsubscribe');

    function get_test_variable() {
    // here get/fetch your variable;

    /*Unsubscribe*/
    $test_variable = "derp";

    return $test_variable;
    }





    share|improve this answer





























      0














      You can pass that PHP variable in ajax data. Please check below files in which I had send the "test_variable" Value to Ajax Function from jQuery.



      Jquery File Code



      jQuery(document).ready(function($) {    
      $('#btn').on('click',function(){
      $.ajax({
      data: {action: 'get_listing_names','test': global.test_variable},
      type: 'post',
      url: global.ajax,
      success: function(data) {
      console.log(data);
      }
      });
      });
      });


      Functions.php file Code.



      <?php
      /**
      * Enqueue scripts and styles.
      *
      * @since 1.0.0
      */
      function ja_global_enqueues() {

      wp_enqueue_script(
      'global',
      get_template_directory_uri() . '/js/global.js',
      array( 'jquery' ),
      '1.0.0',
      true
      );
      wp_localize_script(
      'global',
      'global',
      array(
      'ajax' => admin_url( 'admin-ajax.php' ),
      'test_variable' => 'Test Value',
      )
      );
      }
      add_action( 'wp_enqueue_scripts', 'ja_global_enqueues' );


      add_action('wp_ajax_nopriv_get_listing_names', 'ajax_listings');
      add_action('wp_ajax_get_listing_names', 'ajax_listings');


      function ajax_listings() {

      $test_variable = $_POST['test_variable'];


      wp_send_json_success( $test_variable );

      }





      share|improve this answer





















      • That's fine for information you can share publicly but I have to keep the data I'm passing through private.
        – intense7
        Nov 24 '18 at 8:06











      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%2f53442333%2fhow-to-pass-php-variable-to-wordpress-ajax-handler%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Solved it with this solution: can I pass arguments to my function through add_action?



      Working code solution is:



      /*Unsubscribe*/
      $test_variable = "derp";

      function user_unsubscribe($test_variable){
      echo json_encode($test_variable);
      wp_die();
      };

      add_action('wp_ajax_user_unsubscribe', function() use ($test_variable){
      user_unsubscribe($test_variable);
      });

      add_action('wp_ajax_nopriv_user_unsubscribe', function() use ($test_variable){
      user_unsubscribe($test_variable);
      });





      share|improve this answer


























        1














        Solved it with this solution: can I pass arguments to my function through add_action?



        Working code solution is:



        /*Unsubscribe*/
        $test_variable = "derp";

        function user_unsubscribe($test_variable){
        echo json_encode($test_variable);
        wp_die();
        };

        add_action('wp_ajax_user_unsubscribe', function() use ($test_variable){
        user_unsubscribe($test_variable);
        });

        add_action('wp_ajax_nopriv_user_unsubscribe', function() use ($test_variable){
        user_unsubscribe($test_variable);
        });





        share|improve this answer
























          1












          1








          1






          Solved it with this solution: can I pass arguments to my function through add_action?



          Working code solution is:



          /*Unsubscribe*/
          $test_variable = "derp";

          function user_unsubscribe($test_variable){
          echo json_encode($test_variable);
          wp_die();
          };

          add_action('wp_ajax_user_unsubscribe', function() use ($test_variable){
          user_unsubscribe($test_variable);
          });

          add_action('wp_ajax_nopriv_user_unsubscribe', function() use ($test_variable){
          user_unsubscribe($test_variable);
          });





          share|improve this answer












          Solved it with this solution: can I pass arguments to my function through add_action?



          Working code solution is:



          /*Unsubscribe*/
          $test_variable = "derp";

          function user_unsubscribe($test_variable){
          echo json_encode($test_variable);
          wp_die();
          };

          add_action('wp_ajax_user_unsubscribe', function() use ($test_variable){
          user_unsubscribe($test_variable);
          });

          add_action('wp_ajax_nopriv_user_unsubscribe', function() use ($test_variable){
          user_unsubscribe($test_variable);
          });






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 8:22









          intense7

          133




          133

























              0














              The prefered way to pass variables to ajax is to add them to the request and read them from $_GET or $_POST official documentation



              If you need other variables you'll either have to use globals or call a extra function.



              favorite



              Struggling to pass through some php variables into my ajax handler function in functions.php



              Example provided below doesn't work, probably has something to do with the hooks but I can't find any info on how to do this:



              function user_unsubscribe(){
              $test_variable = get_test_variable();

              echo json_encode($test_variable);
              wp_die();
              };

              add_action('wp_ajax_user_unsubscribe', 'user_unsubscribe');
              add_action('wp_ajax_nopriv_user_unsubscribe', 'user_unsubscribe');

              function get_test_variable() {
              // here get/fetch your variable;

              /*Unsubscribe*/
              $test_variable = "derp";

              return $test_variable;
              }





              share|improve this answer


























                0














                The prefered way to pass variables to ajax is to add them to the request and read them from $_GET or $_POST official documentation



                If you need other variables you'll either have to use globals or call a extra function.



                favorite



                Struggling to pass through some php variables into my ajax handler function in functions.php



                Example provided below doesn't work, probably has something to do with the hooks but I can't find any info on how to do this:



                function user_unsubscribe(){
                $test_variable = get_test_variable();

                echo json_encode($test_variable);
                wp_die();
                };

                add_action('wp_ajax_user_unsubscribe', 'user_unsubscribe');
                add_action('wp_ajax_nopriv_user_unsubscribe', 'user_unsubscribe');

                function get_test_variable() {
                // here get/fetch your variable;

                /*Unsubscribe*/
                $test_variable = "derp";

                return $test_variable;
                }





                share|improve this answer
























                  0












                  0








                  0






                  The prefered way to pass variables to ajax is to add them to the request and read them from $_GET or $_POST official documentation



                  If you need other variables you'll either have to use globals or call a extra function.



                  favorite



                  Struggling to pass through some php variables into my ajax handler function in functions.php



                  Example provided below doesn't work, probably has something to do with the hooks but I can't find any info on how to do this:



                  function user_unsubscribe(){
                  $test_variable = get_test_variable();

                  echo json_encode($test_variable);
                  wp_die();
                  };

                  add_action('wp_ajax_user_unsubscribe', 'user_unsubscribe');
                  add_action('wp_ajax_nopriv_user_unsubscribe', 'user_unsubscribe');

                  function get_test_variable() {
                  // here get/fetch your variable;

                  /*Unsubscribe*/
                  $test_variable = "derp";

                  return $test_variable;
                  }





                  share|improve this answer












                  The prefered way to pass variables to ajax is to add them to the request and read them from $_GET or $_POST official documentation



                  If you need other variables you'll either have to use globals or call a extra function.



                  favorite



                  Struggling to pass through some php variables into my ajax handler function in functions.php



                  Example provided below doesn't work, probably has something to do with the hooks but I can't find any info on how to do this:



                  function user_unsubscribe(){
                  $test_variable = get_test_variable();

                  echo json_encode($test_variable);
                  wp_die();
                  };

                  add_action('wp_ajax_user_unsubscribe', 'user_unsubscribe');
                  add_action('wp_ajax_nopriv_user_unsubscribe', 'user_unsubscribe');

                  function get_test_variable() {
                  // here get/fetch your variable;

                  /*Unsubscribe*/
                  $test_variable = "derp";

                  return $test_variable;
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 8:08









                  janw

                  5,32241939




                  5,32241939























                      0














                      You can pass that PHP variable in ajax data. Please check below files in which I had send the "test_variable" Value to Ajax Function from jQuery.



                      Jquery File Code



                      jQuery(document).ready(function($) {    
                      $('#btn').on('click',function(){
                      $.ajax({
                      data: {action: 'get_listing_names','test': global.test_variable},
                      type: 'post',
                      url: global.ajax,
                      success: function(data) {
                      console.log(data);
                      }
                      });
                      });
                      });


                      Functions.php file Code.



                      <?php
                      /**
                      * Enqueue scripts and styles.
                      *
                      * @since 1.0.0
                      */
                      function ja_global_enqueues() {

                      wp_enqueue_script(
                      'global',
                      get_template_directory_uri() . '/js/global.js',
                      array( 'jquery' ),
                      '1.0.0',
                      true
                      );
                      wp_localize_script(
                      'global',
                      'global',
                      array(
                      'ajax' => admin_url( 'admin-ajax.php' ),
                      'test_variable' => 'Test Value',
                      )
                      );
                      }
                      add_action( 'wp_enqueue_scripts', 'ja_global_enqueues' );


                      add_action('wp_ajax_nopriv_get_listing_names', 'ajax_listings');
                      add_action('wp_ajax_get_listing_names', 'ajax_listings');


                      function ajax_listings() {

                      $test_variable = $_POST['test_variable'];


                      wp_send_json_success( $test_variable );

                      }





                      share|improve this answer





















                      • That's fine for information you can share publicly but I have to keep the data I'm passing through private.
                        – intense7
                        Nov 24 '18 at 8:06
















                      0














                      You can pass that PHP variable in ajax data. Please check below files in which I had send the "test_variable" Value to Ajax Function from jQuery.



                      Jquery File Code



                      jQuery(document).ready(function($) {    
                      $('#btn').on('click',function(){
                      $.ajax({
                      data: {action: 'get_listing_names','test': global.test_variable},
                      type: 'post',
                      url: global.ajax,
                      success: function(data) {
                      console.log(data);
                      }
                      });
                      });
                      });


                      Functions.php file Code.



                      <?php
                      /**
                      * Enqueue scripts and styles.
                      *
                      * @since 1.0.0
                      */
                      function ja_global_enqueues() {

                      wp_enqueue_script(
                      'global',
                      get_template_directory_uri() . '/js/global.js',
                      array( 'jquery' ),
                      '1.0.0',
                      true
                      );
                      wp_localize_script(
                      'global',
                      'global',
                      array(
                      'ajax' => admin_url( 'admin-ajax.php' ),
                      'test_variable' => 'Test Value',
                      )
                      );
                      }
                      add_action( 'wp_enqueue_scripts', 'ja_global_enqueues' );


                      add_action('wp_ajax_nopriv_get_listing_names', 'ajax_listings');
                      add_action('wp_ajax_get_listing_names', 'ajax_listings');


                      function ajax_listings() {

                      $test_variable = $_POST['test_variable'];


                      wp_send_json_success( $test_variable );

                      }





                      share|improve this answer





















                      • That's fine for information you can share publicly but I have to keep the data I'm passing through private.
                        – intense7
                        Nov 24 '18 at 8:06














                      0












                      0








                      0






                      You can pass that PHP variable in ajax data. Please check below files in which I had send the "test_variable" Value to Ajax Function from jQuery.



                      Jquery File Code



                      jQuery(document).ready(function($) {    
                      $('#btn').on('click',function(){
                      $.ajax({
                      data: {action: 'get_listing_names','test': global.test_variable},
                      type: 'post',
                      url: global.ajax,
                      success: function(data) {
                      console.log(data);
                      }
                      });
                      });
                      });


                      Functions.php file Code.



                      <?php
                      /**
                      * Enqueue scripts and styles.
                      *
                      * @since 1.0.0
                      */
                      function ja_global_enqueues() {

                      wp_enqueue_script(
                      'global',
                      get_template_directory_uri() . '/js/global.js',
                      array( 'jquery' ),
                      '1.0.0',
                      true
                      );
                      wp_localize_script(
                      'global',
                      'global',
                      array(
                      'ajax' => admin_url( 'admin-ajax.php' ),
                      'test_variable' => 'Test Value',
                      )
                      );
                      }
                      add_action( 'wp_enqueue_scripts', 'ja_global_enqueues' );


                      add_action('wp_ajax_nopriv_get_listing_names', 'ajax_listings');
                      add_action('wp_ajax_get_listing_names', 'ajax_listings');


                      function ajax_listings() {

                      $test_variable = $_POST['test_variable'];


                      wp_send_json_success( $test_variable );

                      }





                      share|improve this answer












                      You can pass that PHP variable in ajax data. Please check below files in which I had send the "test_variable" Value to Ajax Function from jQuery.



                      Jquery File Code



                      jQuery(document).ready(function($) {    
                      $('#btn').on('click',function(){
                      $.ajax({
                      data: {action: 'get_listing_names','test': global.test_variable},
                      type: 'post',
                      url: global.ajax,
                      success: function(data) {
                      console.log(data);
                      }
                      });
                      });
                      });


                      Functions.php file Code.



                      <?php
                      /**
                      * Enqueue scripts and styles.
                      *
                      * @since 1.0.0
                      */
                      function ja_global_enqueues() {

                      wp_enqueue_script(
                      'global',
                      get_template_directory_uri() . '/js/global.js',
                      array( 'jquery' ),
                      '1.0.0',
                      true
                      );
                      wp_localize_script(
                      'global',
                      'global',
                      array(
                      'ajax' => admin_url( 'admin-ajax.php' ),
                      'test_variable' => 'Test Value',
                      )
                      );
                      }
                      add_action( 'wp_enqueue_scripts', 'ja_global_enqueues' );


                      add_action('wp_ajax_nopriv_get_listing_names', 'ajax_listings');
                      add_action('wp_ajax_get_listing_names', 'ajax_listings');


                      function ajax_listings() {

                      $test_variable = $_POST['test_variable'];


                      wp_send_json_success( $test_variable );

                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 23 '18 at 12:03









                      Jaydeep Jagani

                      541




                      541












                      • That's fine for information you can share publicly but I have to keep the data I'm passing through private.
                        – intense7
                        Nov 24 '18 at 8:06


















                      • That's fine for information you can share publicly but I have to keep the data I'm passing through private.
                        – intense7
                        Nov 24 '18 at 8:06
















                      That's fine for information you can share publicly but I have to keep the data I'm passing through private.
                      – intense7
                      Nov 24 '18 at 8:06




                      That's fine for information you can share publicly but I have to keep the data I'm passing through private.
                      – intense7
                      Nov 24 '18 at 8:06


















                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53442333%2fhow-to-pass-php-variable-to-wordpress-ajax-handler%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

                      Trompette piccolo

                      Slow SSRS Report in dynamic grouping and multiple parameters

                      Simon Yates (cyclisme)