我按照本教程创建了自己的小部件:http://www.wpbeginner.com/wp-tutorials/how-to-create-a-custom-wordpress-widget/
一切都很好但是......我的小工具没有出现在网页上,并且在管理面板中显示为灰色
有什么想法吗?
修改
Test.php(插件/测试)
<?php
/**
* Plugin Name: Test
* Plugin URI: http://www.test.com
* Description: This is a test plugins
* Version: 0.0.1
* Author: Doe
* Author URI: http://www.test.com
* License: GPL2
*/
include 'settings.php';
include 'widget.php';
if( is_admin() )
$settings = new TestSettings();
?>
widget.php
<?php
class test_widget extends WP_Widget {
// constructor
function test_widget() {
$widget_ops = array(
'class_name' => 'test_widget',
'description' => 'MyWidget',
);
parent::__construct( 'test_widget', 'MyWidget', $widget_ops );
}
// widget form creation
function form($instance) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'New title', 'text_domain' );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<?php
}
// widget update
function update($new_instance, $old_instance) {
$instance = $old_instance;
// Fields
$instance['title'] = strip_tags($new_instance['title']);
return $instance;
}
// widget display
function widget($args, $instance) {
echo "Is there anybody out there?";
}
}
?>
的settings.php
<?php
class TestSettings
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
add_action( 'widgets_init', array( $this, 'register_widget' ) );
}
public function register_widget() {
register_widget( 'test_widget' );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'Test',
'manage_options',
'test',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'my_option_name' );
?>
<div class="wrap">
<form method="post" action="options.php">
<?php settings_fields( 'test-settings-group' ); ?>
<?php do_settings_sections( 'test-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Fournisseur</th>
<td><input type="text" name="provider" value="<?php echo esc_attr( get_option('provider') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Script</th>
<td><textarea name="script" rows="10" style="width:100%;"><?php echo esc_attr( get_option('script') ); ?></textarea></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting( 'test-settings-group', 'provider' );
register_setting( 'test-settings-group', 'script' );
}
}
?>