Custom plugin: Trying to show saved data on frontend

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.

Leave a Comment