Generic API - Dashboard Widgets

We often hear about the WordPress Dashboard Widgets API, so what exactly is this. Learn what this API is used for, why it's so important for your WordPress site, and how to easily add widgets to your dashboard. Enhance your site's functionality.

图片[1]-通用 API – 仪表板小部件-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

What is Dashboard Widget API

The Dashboard Widgets API is a set of tools provided by WordPress that allows you to easily add, remove or modify widgets on your dashboard. This is perfect for those who want to communicate directly with their users - whether it's displaying information or getting them to perform certain actions.

Take a look at these examples:

  • Jetpack Plugin: It adds a widget to the dashboard that lets you see the site's visit statistics, as well as the most popular posts and pages.
  • Seriously Simple Podcasting: This plugin comes with Seriously Simple Stats, which allows you to view your blog statistics. Also, it has an RSS feed widget that displays Castos news - Castos is the company that developed this blog plugin.

These APIs and widgets make your WordPress dashboard more powerful and personalized, making information presentation and user interaction more direct and efficient.

图片[2]-通用 API – 仪表板小部件-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

How to use the Dashboard Widget API

Starting with WordPress version 2.7, the Dashboard Widget API makes it easy to add new widgets to the WordPress admin dashboard. The main features you need to know arewp_add_dashboard_widget() function. The function takes multiple arguments:

  1. $widget_id- The unique ID of your widget, which is also used as the id attribute in the widget's HTML output
  2. $widget_name- The name of your widget
  3. $callback- The function that will be called to output the contents of the widget. The function should echo back the contents of the widget

In addition, you can specify some optional parameters.

  1. $control_callback- An optional function that will be called to output the data that the control has used to configure the widget, as well as to process any data that the control has submitted
  2. $callback_args- Array of optional parameters to be passed to the callback function
  3. $context- An optional string that defines which columns the widget will display. The default value is "normal", but other options include "side", "column3" and "column4 "
  4. $priority- An optional string that defines the priority of widgets in the context. The default value is "core", but other options include "default", "high" and "low".

To understand how it works, let's build a sample dashboard widget

图片[3]-通用 API – 仪表板小部件-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

Add Dashboard Widget

Initial setup and output

First, create a new plugin directory and plugin PHP files in the wp-content/plugins directory. For this example, you can call it thewp-learn-dashboard-widgets.

In order for the plugin to be recognized as a WordPress plugin, you need to add a plugin header that contains at least one plugin name value.

As with most WordPress APIs, you will first hook an action. In this case, use thewp_dashboard_setup manipulate, this action is triggered when the dashboard is initialized. You will add our widget here.

add_action( 'wp_dashboard_setup', 'wp_learn_dashboard_widget' );
function wp_learn_dashboard_widget(){
    wp_add_dashboard_widget(
        'wp_learn_dashboard_widget',
        'Learn WordPress Dashboard Widget',
        'wp_learn_dashboard_widget_callback'
    );
}

function wp_learn_dashboard_widget_callback(){
    echo '<p>Hello, World!</p>';
}

If you add this code to an empty plugin file, activate the plugin and load the dashboard, you will see your widget at the bottom of the first column of the dashboard.

图片[4]-通用 API – 仪表板小部件-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

Your content callback function has a lot of flexibility, as long as it calls back valid HTML.

wp_get_recent_posts() Trying to use functionsAdd a list of the latest posts to your widget.

function wp_learn_dashboard_widget_callback(){
    $args = array(
        'numberposts' =&gt; 5, 'post_status' =&gt; 'publish', 'post_status' =&gt; 'publish')
        'post_status' =&gt; 'publish'
    );
    $recent_posts = wp_get_recent_posts( $args );
    echo '<ul>';
    foreach( $recent_posts as $recent ){
        echo '<li><a href="/en/' . get_permalink( $recent['ID'] ) . '/">' . $recent['post_title'] . '</a></li>';
    }
    echo '</ul>';
}

Adding Widget Controls

You can also add some controls to configure your widget. Let's say you want to control the number of posts displayed. To do so, configure the control callback function.

Add a function to callwp_learn_dashboard_widget_control()to process the data.

function wp_learn_dashboard_widget_control(){
    echo '';
    echo ''; ''; }
}

When you refresh the dashboard, nothing seems to happen, but if you hover over the widget, you will see a new link that shows the "Configuration". Clicking on that link will display the form you just created.

图片[5]-通用 API – 仪表板小部件-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

Now you need to update the control callback to handle the data submitted by the form.

To save data, use theupdate_option() function (math.), the function stores the value in the options table.

function wp_learn_dashboard_widget_control_callback(){
    if (isset($_POST['wp_learn_dashboard_widget_numberposts'])){
        update_option( 'wp_learn_dashboard_widget_numberposts', sanitize_text_field( $_POST['wp_learn_dashboard_widget_numberposts'] ) );;
    }
    $number_posts = get_option( 'wp_learn_dashboard_widget_numberposts', 5 );
    echo '';
    echo ''; }
}

Then you need to return to the content callback function and update the array$argsto use the value you just saved. To get the value, you can use theget_option()function (math.)The

function wp_learn_dashboard_widget_callback(){
    $numberposts = get_option( 'wp_learn_dashboard_widget_numberposts', 5 );
    $args = array(
        'numberposts' =&gt; $numberposts,
        'post_status' =&gt; 'publish'
    );
    $recent_posts = wp_get_recent_posts( $args );
    echo '<ul>';
    foreach( $recent_posts as $recent ){
        echo '<li><a href="/en/' . get_permalink( $recent['ID'] ) . '/">' . $recent['post_title'] . '</a></li>';
    }
    echo '</ul>';
}

reach a verdict

For more information on using the dashboard widget, including examples of how to use the optional callback parameters, context, and priority parameters, check out theDashboard Widgets API SectionThe


Contact Us
Can't read the article? Contact us for free answers! Free help for personal, small business sites!
Tel: 020-2206-9892
QQ咨询:1025174874
(iii) E-mail: info@361sale.com
Working hours: Monday to Friday, 9:30-18:30, holidays off
© Reprint statement
This article was written by Harry
THE END
If you like it, support it.
kudos0 share (joys, benefits, privileges etc) with others
commentaries sofa-buying

Please log in to post a comment

    No comments