How do I make a variable inside a plugin available for functions.php

In the following code exists inside a wordpress plugin. I would like to make $recipient_id available for a function inside functions.php. What would be the best way to do this.

class Private_Messages_Notifications {

...

public function new_message_notification( $message, $thread ) {

    ...

    $recipient_id = absint( $thread->get_recipient_id() );

    }

}

Using Sessions:

Your Plugin PHP File...

session_start();
$_SESSION['variableName'] = $var_value;

ON Your Functions file

session_start();
$var_value = $_SESSION['variableName'];

You can also pass the value using cookies if the variable doesn't need to be secure...

Your Plugin PHP File...

$_COOKIE['varname'] = $var_value;

ON Your Functions file

$var_value = $_COOKIE['varname'];

Using Golabls

Your Plugin PHP File

    function myValue(){
       global $varName;

       $varName = 'something';
    }

    add_action('wp_roles_init', 'myValue');

ON Your Functions File

global $varName;
echo $varName;

How do I make a variable inside a plugin available for functions.php, In the following code exists inside a wordpress plugin. I would like to make $​recipient_id available for a function inside functions.php. What would be the best​  Add your variables to a php files (e.g. config.php) and include it whenever you want to use its variables. include('config.php'); Another easy way is using $_GET or $_POST variables.

You use the global variable in your plugin Code:

class Private_Messages_Notifications {

...

public function new_message_notification( $message, $thread ) {
   global $recipient_id;
    ...

    $recipient_id = absint( $thread->get_recipient_id() );

    }

}

and use this in your functions.php file using

global  $recipient_id;

How to get variables from fucntion.php to my plugin files , Basic PHP: functions.php: function my_func() { //doing my staff $my_var = 'data'; //​stored data after doing things return $my_var; }. In plugin file: Make an array of two entries where the 0th entry is the name of the class to be invoked ('self' and 'parent' work as well) and the 1st entry is the name of the function. Basically, a 'callback' variable is either a string (the name of the function) or an array (0 => 'className', 1 => 'functionName').

You can also try this solution, It works for me.

Your Plugin PHP File...

define('TEST_VAR','TEST_VAR_VALUES');

ON Your Functions File

$var = TEST_VAR;

Please let me know if you are facing any issues.

Variable scope - Manual, In PHP global variables must be declared global inside a function if they are going to this work in this situation (where a standard PHP file is included within a function, My solution: Thus, to make all my global variables available to the files  A sometimes useful concept inside of PHP is that of variable functions. 0:06. This is a way for you to use a variable's value to call a function of the same name. 0:11. This way if a variable name has parenthesis appended to it, PHP will look 0:16. for a function with the same name as whatever the variable evaluates to, 0:20

Use a class constant:

class Foo {
    const GREETING = 'Hello';
}

// somewhere else
echo Foo::GREETING;

Understanding PHP Globals and Variable Scope in WordPress , To make a variable available everywhere (globally) from inside a function means that you must first declare that variable as global . Then you can  By default, when you are inside a function, you do not have access to the outer variables. If you want your function to have access to an outer variable, you have to declare it as global, inside the function :

Creating and using custom global variables in wordpress. · GitHub, First create global variables (in functions.php or as a mu-plugin):. <?php /* * CUSTOM GLOBAL VARIABLES */ function wtnerd_global_vars() { global $wtnerd​;  Accessing global variable inside function: The ways to access the global variable inside functions are: Using global keyword. Using array GLOBALS [var_name]: It stores all global variables in an array called $GLOBALS [var_name]. Var_name is the name of the variable. This array is also accessible from within functions and can be used to perform operations on global variables directly.

Wordpress Web Application Development, function can be found inside the functions.php file of the forum plugin. Then, we assign the action to the $wpwaf_social_action variable and create a We created an if statement in the previous code to check for the availability of the login  This means that a reference to the variable is manipulated by the function rather than a copy of the variable's value. Any changes made to an argument in these cases will change the value of the original variable. You can pass an argument by reference by adding an ampersand to the variable name in either the function call or the function

using a variable inside do_shortcode(), Hi, i'm trying to have custom shortcode nested in a plugin shortcode. i learned that there is Support » Developing with WordPress » using a variable inside do_shortcode() to WP shortcodes and to over come it I need to us the do_shortcode function It's also possible to just put a variable string if you use double quotes: How to Create a Basic Plugin Sometimes you want to make a piece of functionality available throughout your code. For example, perhaps you want a single method you can call on a jQuery selection that performs a series of operations on the selection.

Comments
  • this would be helpful : wordpress.stackexchange.com/questions/13707/…