自定义WP_Widget未出现在管理面板中

时间:2012-12-12 20:01:18

标签: php widget wordpress

为了对默认Recent Posts Widget应用某些更改,我已将其复制并修改为我的需要。它包含在我的模板functions.php中并注册,当在管理面板中加载小部件时,它的构造函数就会运行。

问题是它没有出现在可用小部件列表中,因此无法使用。当我尝试http://www.darrenhoyt.com/2009/12/22/creating-custom-wordpress-widgets/的最小例子时也会出现问题,所以我想我错过了一些重要的东西,但不知道是什么。

  • 到目前为止,我的主题是https://wordpress.org/extend/themes/toolbox的修改后的克隆版。 这解释了代码中toolbox_的多次出现。
  • 以下是functions.phpinc/widgets.php的代码摘录 <?php // BEGIN functions.php if ( ! function_exists( 'toolbox_setup' ) ): /** * Sets up theme defaults and registers support for various WordPress features. * * Note that this function is hooked into the after_setup_theme hook, which runs * before the init hook. The init hook is too late for some features, such as indicating * support post thumbnails. * * To override toolbox_setup() in a child theme, add your own toolbox_setup to your child theme's * functions.php file. */ function toolbox_setup() { // [...] require( get_template_directory() . '/inc/widgets.php' ); // [...] } endif; // toolbox_setup /** * Tell WordPress to run toolbox_setup() when the 'after_setup_theme' hook is run. */ add_action( 'after_setup_theme', 'toolbox_setup' ); /** * Register widgetized area and update sidebar with default widgets */ function toolbox_widgets_init() { register_widget("My_Widget_Recent_Posts"); register_sidebar( array( 'name' => __( 'Sidebar 1', 'toolbox' ), 'id' => 'sidebar-1', 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => "</aside>", 'before_title' => '<h1 class="widget-title small">', 'after_title' => '</h1>', ) ); } add_action( 'init', 'toolbox_widgets_init' ); // END functions.php ?> <?php // BEGIN inc/widgets.php /** * Recent_Posts widget class * * @since 2.8.0 */ class My_Widget_Recent_Posts extends WP_Widget { function __construct() { $widget_ops = array('classname' => 'my_widget_recent_entries', 'description' => __( "The most recent posts on your site (modified)") ); parent::__construct('my-recent-posts', __('Recent Posts (modified)'), $widget_ops); $this->alt_option_name = 'my_widget_recent_entries'; add_action( 'save_post', array(&$this, 'flush_widget_cache') ); add_action( 'deleted_post', array(&$this, 'flush_widget_cache') ); add_action( 'switch_theme', array(&$this, 'flush_widget_cache') ); } function widget($args, $instance) { $cache = wp_cache_get('my_widget_recent_posts', 'widget'); if ( !is_array($cache) ) $cache = array(); if ( ! isset( $args['widget_id'] ) ) $args['widget_id'] = $this->id; if ( isset( $cache[ $args['widget_id'] ] ) ) { echo $cache[ $args['widget_id'] ]; return; } ob_start(); extract($args); $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title'], $instance, $this->id_base); if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) ) $number = 10; $r = new WP_Query( apply_filters( 'widget_posts_args', array( 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true ) ) ); if ($r->have_posts()) : ?> <?php echo $before_widget; ?> <?php if ( $title ) echo $before_title . $title . $after_title; ?> <ul> <?php while ($r->have_posts()) : $r->the_post(); ?> <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a> <?php the_content(); ?></li> <?php endwhile; ?> </ul> <?php echo $after_widget; ?> <?php // Reset the global $the_post as this query will have stomped on it wp_reset_postdata(); endif; $cache[$args['widget_id']] = ob_get_flush(); wp_cache_set('my_widget_recent_posts', $cache, 'widget'); } function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); $instance['number'] = (int) $new_instance['number']; $this->flush_widget_cache(); $alloptions = wp_cache_get( 'alloptions', 'options' ); if ( isset($alloptions['my_widget_recent_entries']) ) delete_option('my_widget_recent_entries'); return $instance; } function flush_widget_cache() { wp_cache_delete('my_widget_recent_posts', 'widget'); } function form( $instance ) { $title = isset($instance['title']) ? esc_attr($instance['title']) : ''; $number = isset($instance['number']) ? absint($instance['number']) : 5; ?> <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 $title; ?>" /></p> <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label> <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p> <?php } } // END inc/widgets.php ?> 。如果需要,我可以提供更多代码。请让我 在评论中知道。

代码:

{{1}}

3 个答案:

答案 0 :(得分:3)

您必须将register_widget挂钩到widgets_init操作,而不是init操作。在你的情况下,它是这样的:

<?php
add_action( 'widgets_init', 'toolbox_widgets_init' );

function toolbox_widgets_init() {
  register_widget("My_Widget_Recent_Posts");
}
?>

答案 1 :(得分:1)

好的,让我们做一些调试测试:

echo "include widget file";
require( get_template_directory() . '/inc/widgets.php' );

此外:

echo "register widget";
register_widget("My_Widget_Recent_Posts");

然后:

function __construct() {
echo "widget class";

检查你的HTML,看看哪些部分回显了,哪些部分没有你可能会得到Headers已经发送错误,你可以忽略。希望我们能够看到哪个部分没有被触发以缩小问题的范围。

答案 2 :(得分:1)

如下注册小部件解决了我的问题,但在我看来并不是很优雅。在类定义后面我插入了行

<?php
add_action( 'widgets_init', create_function('',
    'return register_widget("My_Widget_Recent_Posts");') );
?>

并从我认为属于的toolbox_widgets_init()删除了注册。

如果有人可以解释为什么它必须是这样的,或者我可以改变什么,以便它能像我打算在我高度赞赏评论之前那样工作。但是现在问题已经解决了。

相关问题