WP_REGISTER_SIDABAR_WIDGET()取消了我的Widges子菜单--我做错了什么?

时间:2013-10-12 作者:Mayeenul Islam

将他人制作的主题作为官方职责。起初,它会导致wp admin Widgets页面出错—根本不起作用。然后我第一个想到的是调试。。。

Debugging steps
“raquo;已激活WP_DEBUG.
“raquo;停用所有插件
“raquo;切换到默认主题“T13”。

Errors
发现问题来自主题。再次激活主题,它在前端和后端都显示了许多调试通知。首先是:

register_sidebar_widget() is deprecated.

所以我毫不犹豫地去了wp_register_sidebar_widget(). 但却发现自己置身于一个幽闭恐怖的竞技场;我完全不知道如何在wp_register_sidebar_widget. 但是有Codex 和其他WP。东南部。com线程,我尝试了以下操作:

<?php
// Register widgetized areas
function theme_widgets_init() {

    // Area 1   
    wp_register_sidebar_widget(
        \'primary_widget_area\',          //widget_id/ widget_slug
        \'Primary Widget Area\',          //widget_name
        \'primary_widget_display\',       //callback function
        array(
            \'description\' => \'The primary widget on the right side\'
        )
    );

    function primary_widget_display( $args ) {

        extract($args);
        echo $before_widget;
        echo $before_title . \'My Unique Widget\' . $after_title;
        echo $after_widget;
        // print some HTML for the widget to display here
        echo "Your Widget Test";

    }

    // Area 2

    wp_register_sidebar_widget(
        \'secondary_widget_area\', //widget_id/ widget_slug
        \'Secondary Widget Area\', //widget_name
        \'secondary_widget_display\', //callback function
        array(
            \'description\' => \'The secondary widget on the right side\'
        )
    );

    function secondary_widget_display( $args ) {        
        echo "show the secondary widget";       
    }

} // end theme_widgets_init

add_action( \'init\', \'theme_widgets_init\' );
?>
问题1:wp admin中外观菜单下的小部件子菜单已完全消失

问题#2:

我正在努力解决新问题callback_function 属于wp_register_sidebar_widget(). 如何使用新的callback_function. 所以,对于主要小部件区域,您可以看到,我只是复制了&;粘贴Codex中的代码以了解代码的工作方式。但作为Problem #1 发生,我无法测试em。

这是主题中主要使用的代码块,现已弃用<我如何使用新的callback_function 做同样的事

<?php
        // Deprecated Codes Below
        register_sidebar_widget(
            array (
                \'name\' => \'Secondary Widget Area\',
                \'id\' => \'secondary_widget_area\',
                \'before_widget\' => \'<li id="%1$s" class="widget-container %2$s">\',
                \'after_widget\' => "</li>",
                \'before_title\' => \'<h3 class="widget-title">\',
                \'after_title\' => \'</h3>\',
            )
        );
?>
NOTE: 但我粘贴在顶部的新代码解决了一件事&mdash;所有调试通知都消失了。这让人松了口气,阿罕杜利拉!:)

1 个回复
最合适的回答,由SO网友:Wyck 整理而成

您的代码中有一些问题,但您很接近,有一些解决问题的指针。

首先,尽可能多地删除仍然包含问题的代码。在您的情况下,删除第二个小部件,您需要查看的代码越少,就越容易找到问题。

换句话说,使用SSCCE:http://sscce.org/

另一条建议是使用“Primary”和以下名称创建唯一的小部件id并为您的函数添加前缀theme_widgets_init 这不是个好主意。

如果在菜单中的“外观”下未看到任何可用的小部件,如果菜单项does not exist 归根结底,这是因为您至少需要一个使用register_sidebar.

您至少需要以下其中之一:

 function islam_widgets_sidebar() {
    // Area 1, located at the top of the sidebar.
    register_sidebar( array(
        \'name\' => __( \'Primary Widget Area\'),
        \'id\' => \'primary-widget-area\',
        \'description\' => __( \'The primary widget area\'),
        \'before_widget\' => \'<li id="%1$s" class="widget-container %2$s">\',
        \'after_widget\' => \'</li>\',
        \'before_title\' => \'<h3 class="widget-title">\',
        \'after_title\' => \'</h3>\',
        ) );
      }
  add_action( \'widgets_init\', \'islam_widgets_sidebar\' );
对于您实际的小部件问题,您将一个函数嵌套在另一个函数中,显示回调需要在该函数之外。

// Register widgetized areas
function islam_theme_widgets() {

    // Area 1
    wp_register_sidebar_widget(
        \'mayeenul_widget_area\',          //widget_id/ widget_slug
        \'Mayeenuls Widget Area\',          //widget_name
        \'islam_widget_display\',       //callback function
        array(
            \'description\' => \'The primary widget on the right side\'
        )
    );

} // end theme_widgets_init

 function islam_widget_display( $args ) {

        extract($args);
        echo $before_widget;
        echo $before_title . \'My Unique Widget\' . $after_title;
        echo $after_widget;
        // print some HTML for the widget to display here
        echo "Your Widget Test";

    }

add_action( \'init\', \'islam_theme_widgets\' );
对于第二个小部件,您可以执行相同的操作,但当然要使用不同的函数回调和id/名称。

It\'s really important to note that the above functions are pretty useless because the widget can only be used once in exactly 1 of the sidebars.

您真的想扩展WP_Widget 类别:
http://codex.wordpress.org/Function_Reference/register_widget

结束

相关推荐

Remove Widgets in Dashboard

另一个noob问题。。。当作者登录并访问其仪表板时,他们会看到各种小部件,例如Internet Explorer警告、WordPress动态新闻。是否要为所有用户(现在和将来)删除所有这些内容,以便他们只看到“快速按”&;\'现在的小部件?谢谢