I'm working on my first plugin and I have made a nice settings page which saves and works.

But I have split it to few files and now I can't determine if field is checked.. I have also tried declaring a global variable but I can't get it to work :\


This is how my plugin starts:

global $if_autoload;

add_action('init', 'additional_menus_admin');

function additional_menus_admin() {
    require_once(dirname(__FILE__) . '/admin.php');
    require_once(dirname(__FILE__) . '/hooks.php');


Here I try to see if the "autoload" checkbox is checked:

add_action('wp_head','hook_additional_menu', 20);

function hook_additional_menu() {
    global $if_autoload;

    echo '<test>'.$if_autoload.'</test>';



I have also tried using the get_option function but nothing changes. All the fields are being save on my admin.php file and I tried pushing the global variable from there too:

function menu_autoload_callback() {
    $if_autoload = get_option( 'autoload-or-shortcode' );
    echo '<input type="checkbox" name="autoload-or-shortcode" value="1"'. checked( 1, get_option( 'autoload-or-shortcode' ), false ) .' /><br />';

2 Answers 2


Save yourself a headache and make a class. The static variable can be accessed like a global once it's loaded. Just be sure the class exists before you try to use it!

if ( ! class_exists( 'WPSE_20150123_Plugin' ) ) {

    class WPSE_20150123_Plugin {

        // our variable that will be set and read back later
        public static $if_autoload = 'I\'m Not Sure???';

        // 'init' hook
        public static function init() {

            // show value
            echo static::$if_autoload; // I'm quite sure 

// set the static value
WPSE_20150123_Plugin::$if_autoload = 'I\'m quite sure!';

// hook init to static function
add_action( 'init', 'WPSE_20150123_Plugin::init' );


Looking at your checked() function, I'm not quite sure you're using it correctly. This is the example they give and your args look like they're in the wrong place.


// Get an array of options from the database.
$options = get_option( 'slug_option' );

// Get the value of this option.
$checked = $options['self-destruct'];

// The value to compare with (the value of the checkbox below).
$current = 1; 

// True by default, just here to make things clear.
$echo = true;

<input name="slug-option[self-destruct]" value="1" <?php checked( $checked, $current, $echo ); ?>/>

Your function might work better as:

function menu_autoload_callback() {
    $if_autoload = get_option( 'autoload-or-shortcode' );
    echo '<input type="checkbox" name="autoload-or-shortcode" value="1" ' . checked( $if_autoload , 1, false ) . ' /><br />';


Another thing, it looks like you echo invalid html:

echo '<test>'.$if_autoload.'</test>';

should be

echo '<h1 class="test" >If Autoload: ' . $if_autoload . '</h1>';

It wouldn't hurt to take a look at http://wppb.me.


In the end i used this-

// Define global variables
    global  $am_globals;
            $am_globals = array(
                'if_autoload' => get_option( 'autoload-or-shortcode' ),

and this-

if( ! $if_autoload = $GLOBALS['am_globals']['if_autoload'] ) {
  • 1
    I think you would have done better taking the advice you asked for :)
    – Q Studio
    Commented Nov 30, 2020 at 17:04

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.