I'm trying to get information from the front end of my Wordpress site, I used ajax to do that, it gave me the ability to use the data in one of my plugin functions, but in my case I want to use this data in more than just one function.

add_action( 'admin_footer', 'my_action_javascript' ); 

function my_action_javascript() { ?>
    <script type="text/javascript" >
    jQuery(document).ready(function($) {

        var data = {
            'action': 'my_action',
            'whatever': 1234

        jQuery.post(ajaxurl, data);
    </script> <?php

add_action( 'wp_ajax_my_action', 'my_action' );

function my_action() {

    $whatever = $_POST['whatever'];


In other words I need to use $_POST['whatever'] in other functions in my plugin not only my_action() function, I tried using PHP global variables like so , but it didn't workout:

function my_action(){
    global $whatever = $_POST['whatever'];
function my_other_function(){
   global $whatever;
       echo $whatever;
  • Is there any reason you can't merge them into 1 function, or use 1 function then call the others from it? It sounds like the structure of your code could be better organised, maybe if you provided some context as to what you're trying to do there may be alternatives suggested you hadn't considered?
    – Tom J Nowell
    May 27, 2017 at 17:06

1 Answer 1


If you want to use a global variable, you have to set it outside any local scope, above your functions. For example:

if (isset($_POST['whatever'])) {
    $variable = $_POST['whatever'];

This will store it in the superglobal array. Now, access it using the $_GLOBALS array:

function my_action(){
   $whatever = $GLOBALS['variable'];

I would however have an easier approach. You can get the variable in one function and call the other one inside it. Take a look at this:

function first_func(){
    $variable = $_POST['whatever'];
    // Do whatever you want here
    echo "First $variable";
    // Now call the second one
function second_func($input){
    echo $input;

Avoiding the globals is a good practice since you don't know what else could interfere with the array.

  • Thanks for you respond, but I can't use isset($_POST['whatever'] outside my_action() function since it is not set only inside it, the second approach is good but I can't use my second function inside the first one.
    – Berzerkfar
    May 27, 2017 at 21:24
  • @Berzerkfar If it is not set only inside it, then why can't you use it outside? by the way, why can't you use $_POST[] twice in both your functions?
    – Johansson
    May 27, 2017 at 21:25
  • Sorry I meant it is set only inside my_action function
    – Berzerkfar
    May 27, 2017 at 21:28
  • because of the action add_action( 'wp_ajax_my_action', 'my_action' ); add_action( 'wp_ajax_<name of the function you want to use the variable>', '<name of the function you want to use the variable>' )
    – Berzerkfar
    May 27, 2017 at 21:31
  • @Berzerkfar When a data is sent using POST or GET method, it's set and accessible everywhere inside the functions.php. You can use $GLOBALS['variable'] = $_POST['whatever']; inside your first function, but your seconds function must run after the first function, or you can't retrieve the global variable.
    – Johansson
    May 27, 2017 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.