admin管理员组文章数量:1313598
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 />';
}
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 />';
}
Share
Improve this question
edited Jan 24, 2016 at 3:56
jgraup
9,9043 gold badges32 silver badges70 bronze badges
asked Jan 23, 2016 at 21:01
NateNate
1867 bronze badges
2 Answers
Reset to default 1Save 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.
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'] ) {
本文标签: phpCustom plugin Trying to show saved data on frontend
版权声明:本文标题:php - Custom plugin: Trying to show saved data on frontend 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741951034a2406706.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论