如何为我的主题创建新的 WordPress 小部件

发布于 2024-09-18 16:14:10 字数 97 浏览 3 评论 0原文

如何为我的主题创建新的 WordPress 小部件?

替代文本

How to create new wordpress widgets for my themes ?

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲喜皆因你 2024-09-25 16:14:11

这是我最喜欢的教程:

http://justintadlock.com/archives/2009/05/26/the-complete-guide-to-creating-widgets-in-wordpress-28

我用它作为基础我所有的小部件。

基本上,小部件是一个插件,但您扩展了 WP_Widget 类。非常简单,按照教程操作即可。

这也很有帮助 http://codex.wordpress.org/Widgets_API

祝你好运!

This is my favourite tutorial about that:

http://justintadlock.com/archives/2009/05/26/the-complete-guide-to-creating-widgets-in-wordpress-28

I'm using it as a base to all my widgets.

Basically, the widget is a plugin, but you extend the class WP_Widget. It's quite easy, just follow that tutorial.

Also this is helpful http://codex.wordpress.org/Widgets_API

Good luck!

吻安 2024-09-25 16:14:11

这更多是谷歌的问题,而不是 stackoverflow 的问题。您将在网络上找到大量教程和示例。

This is more a question for google than stackoverflow. You'll find tons of tutorials and examples on the web.

呆头 2024-09-25 16:14:10

如果你想创建一个小部件。导航到您的插件文件夹并在“Sample”文件夹中创建一个名为“sample.php”的空白文件。

复制以下代码并粘贴到sample.php 文件中。

<?php
/*
Plugin Name: Widget
Description: Widget Description
*/

// Creating the widget
// Create a class named as "sample_widget" that is a child class of "WP_Widget".
class sample_widget extends WP_Widget
{
    //Constructor of class
    public function __construct()
    {
        parent::__construct(
        // Id of our widget.
            'sample_widget', 
        // This is the widget name that will be visible to user
            __('Sample Widget'), 
        // Description of our widget
            array(
            'description' => __('Description of our widget.')
        ));
    }

    // Creating widget front-end
    // This is where the action happens
    // Creating function named as "widget", receiving two parameters.
    public function widget($args, $instance)
    {
        /*Getting and assigning our widget title to wordpress hook "widget_title"
        and passing its value to "$title" */
        $title = apply_filters('widget_title', $instance['title']);
        // Area, that is before the widget is diplayed
        echo $args['before_widget'];
        // Checking "$title" is empty or not
        if (!empty($title)) /* If "$title" is not empty, below code will execute.
        $args['before_title'] -> Displaying content before our widget title
        $title -> Display our widget title
        $args['after_title'] -> Displaying content after our widget title */ {
            echo $args['before_title'] . $title . $args['after_title'];
        }

        // Displaying text of our widget
        echo __('Hello, this is our widget text!');
        // Displaying content after our widget
        echo $args['after_widget'];
    }

    // This function naming our widget title
    public function form($instance)
    {
        // If title is already set.
        if (isset($instance['title'])) {
            // $title is getting already assigned title
            $title = $instance['title'];
        }
        // Otherwise our default title will be "Widget title"
        else {
            $title = __('Widget title');
        }

?>
<!-- These are the settings and user interface that an admin will see -->
<p>
<!-- Already set title will be displayed at the top of our widget in admin panel -->
<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
    }

    // This function will replace old title with new title of our widget
    public function update($new_instance, $old_instance)
    {
        $instance          = array();
        $instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
        return $instance;
    }
}

// Function to register and load our newly widget
function sample_widget_load()
{
    // Registering our widget named as "sample_widget"
    register_widget('sample_widget');
}
// Calling our newly created function named as "sample_widget_load" to register our widget
add_action('widgets_init', 'sample_widget_load');

?>

If you want to create a widget. Navigate to your plugins folder and create a blank file named as “sample.php” in “Sample” folder..

Copy below code and paste in sample.php file.

<?php
/*
Plugin Name: Widget
Description: Widget Description
*/

// Creating the widget
// Create a class named as "sample_widget" that is a child class of "WP_Widget".
class sample_widget extends WP_Widget
{
    //Constructor of class
    public function __construct()
    {
        parent::__construct(
        // Id of our widget.
            'sample_widget', 
        // This is the widget name that will be visible to user
            __('Sample Widget'), 
        // Description of our widget
            array(
            'description' => __('Description of our widget.')
        ));
    }

    // Creating widget front-end
    // This is where the action happens
    // Creating function named as "widget", receiving two parameters.
    public function widget($args, $instance)
    {
        /*Getting and assigning our widget title to wordpress hook "widget_title"
        and passing its value to "$title" */
        $title = apply_filters('widget_title', $instance['title']);
        // Area, that is before the widget is diplayed
        echo $args['before_widget'];
        // Checking "$title" is empty or not
        if (!empty($title)) /* If "$title" is not empty, below code will execute.
        $args['before_title'] -> Displaying content before our widget title
        $title -> Display our widget title
        $args['after_title'] -> Displaying content after our widget title */ {
            echo $args['before_title'] . $title . $args['after_title'];
        }

        // Displaying text of our widget
        echo __('Hello, this is our widget text!');
        // Displaying content after our widget
        echo $args['after_widget'];
    }

    // This function naming our widget title
    public function form($instance)
    {
        // If title is already set.
        if (isset($instance['title'])) {
            // $title is getting already assigned title
            $title = $instance['title'];
        }
        // Otherwise our default title will be "Widget title"
        else {
            $title = __('Widget title');
        }

?>
<!-- These are the settings and user interface that an admin will see -->
<p>
<!-- Already set title will be displayed at the top of our widget in admin panel -->
<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
    }

    // This function will replace old title with new title of our widget
    public function update($new_instance, $old_instance)
    {
        $instance          = array();
        $instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
        return $instance;
    }
}

// Function to register and load our newly widget
function sample_widget_load()
{
    // Registering our widget named as "sample_widget"
    register_widget('sample_widget');
}
// Calling our newly created function named as "sample_widget_load" to register our widget
add_action('widgets_init', 'sample_widget_load');

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文