2

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 :\

Plugin

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');
}

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>';

}

admin.php

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 />';
}
0

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' );

admin.php

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.

<?php

// 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 />';
}

hooks.php

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.

0

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'] ) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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