[wp-hackers] Variable Scope

DD32 wordpress at dd32.id.au
Tue Mar 11 01:49:14 GMT 2008


This inside a plugin works for me:

<?php
/*
Plugin Name: plugin globals
*/

$variable = get_option('siteurl');
 
function dummy() {
    global $variable;
    var_dump($variable);
}

?>

Keep in mind, That if $variable holds a false value (ie. it couldnt find that option) nothing would be echo'd.

Also, If its for use in a activation hook, I think you need to globalise it like this first: (as plugins are activated within a function)

<?php
/*
Plugin Name: plugin globals
*/

global $variable;
$variable = get_option('siteurl');
 
function dummy() {
    global $variable;
    var_dump($variable);
}

?>


On Tue, 11 Mar 2008 12:39:30 +1100, Chris Poteet <cpoteet at siolon.com> wrote:

> I want to have a wider scope for variables in my plugin, but for some
> reason it's not working.  For instance:
>
> $variable = get_option('variabletest');
>
> function dummy() {
>     global $variable;
>     echo $variable;
> }
>
> Neither of those work.  Am I doing something wrong (I must be).
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
> 





More information about the wp-hackers mailing list