Naz
Naz

Reputation: 910

Options panel global variable?

I am creating a WordPress options panel based on http://en.bainternet.info/2012/my-options-panel

In the article it states to call the stored options to use

//get the data to an array
$data = get_option('demo_options');
//access each field by its id
echo $data['text_field_id'];

Which works, the only problem I have is that if I want to call some information for say the header.php and the footer.php or any other page I have to include the line

$data = get_option('demo_options');

at the top of every page otherwise I can not call the data, which seems repetitive.

I have tried to create a global variable in the functions.php file like;

global $data
$data = get_option('demo_options');

but that doesn't work.

Does anyone know how I can resolve this so I don't need to add the line to the top of every page?

Thanks

Upvotes: 0

Views: 805

Answers (1)

brasofilo
brasofilo

Reputation: 26075

In functions.php:

$data = get_option('demo_options');

In any other theme template file (header.php, single.php, page.php):

global $data; 
var_dump( $data );

To have a variable defined in header.php and not having to use global $var in other template files, the following has to be done:

  • define the variable in header.php
  • instead of get_header(); in other theme template files, use include 'header.php';
  • this way, you can reference the variable directly without having to declare the global

Upvotes: 1

Related Questions