wordpress api

The WordPress Dashboard Widgets API lets themes and plugins add, remove or re-position WordPress dashboard widgets. The WordPress Dashboard is the first thing we see when we log in to the WordPress administration screen. The WordPress Dashboard Widget API has been available from WordPress 2.7 onwards.

WordPress仪表板小部件API允许主题和插件添加,删除或重新定位WordPress仪表板小部件。 当我们登录WordPress管理屏幕时,首先会看到WordPress仪表板。 从WordPress 2.7开始可以使用WordPress Dashboard Widget API。

In this tutorial we’ll look at the different actions and functions that are provided by the API. And, how to add, remove and position dashboard widgets using this API.

在本教程中,我们将研究API提供的不同动作和功能。 以及如何使用此API添加,删除和放置仪表板小部件。

(Behind the Scene of WordPress Dashboard Widgets )

Internally, WordPress dashboard widgets are WordPress meta boxes. The Dashboard Widget API uses the WordPress meta box API to add, remove and position dashboard widgets.

在内部,WordPress仪表板小部件是WordPress meta box 。 仪表板小部件API使用WordPress元框API添加,删除和放置仪表板小部件。

(Adding a Dashboard Widget)

We can add a widget to the WordPress dashboard using the wp_add_dashboard_widget function. We need to wrap this function inside the wp_dashboard_setup hook.

我们可以使用wp_add_dashboard_widget函数将小部件添加到WordPress仪表板。 我们需要将此函数包装在wp_dashboard_setup挂钩中。

Here’s some example code on how to use wp_add_dashboard_widget:

这是一些有关如何使用wp_add_dashboard_widget示例代码:

function add_dashboard_widget()
{
    wp_add_dashboard_widget("sitepoint", "SitePoint Videos", "display_sitepoint_dashboard_widget");
}

function display_sitepoint_dashboard_widget()
{
	echo "Watch Video Courses at <a href='http://learnable.com'>Learnable</a>";
}

add_action("wp_dashboard_setup", "add_dashboard_widget");

Here, we are calling wp_add_dashboard_widget inside add_dashboard_widget, which is triggered by the wp_dashboard_setup hook.

在此,我们呼吁wp_add_dashboard_widgetadd_dashboard_widget ,这是由触发wp_dashboard_setup钩。

wp_add_dashboard_widget takes three arguments. Here are the arguments:

wp_add_dashboard_widget接受三个参数。 这里是参数:

  1. $id: Every dashboard widget must have a unique id. Make sure you prefix it with your plugin or theme name to avoid overriding.
    $ id :每个仪表板小部件都必须具有唯一的ID。 确保使用插件或主题名称作为前缀,以避免覆盖。
  2. $title: Title of the widget to be displayed.
    $ title :要显示的小部件的标题。
  3. $callback: A callback which displays the content of the widget.
    $ callback :显示小部件内容的回调。

Here’s how the widget looks:

小部件的外观如下:




wordpress客服插件代码_javascript


(Removing a Dashboard Widget)

We can remove a default or custom dashboard widget using the remove_meta_box function. We need wrap this function in the wp_dashboard_setup function.

我们可以使用remove_meta_box函数删除默认或自定义的仪表板小部件。 我们需要将此函数包装在wp_dashboard_setup函数中。

Here’s how we’d use remove_meta_box:

这是我们使用remove_meta_box

function remove_dashboard_widget()
{
	remove_meta_box("sitepoint", "dashboard", "normal");
}

add_action("wp_dashboard_setup", "remove_dashboard_widget");

Here, we’re calling the remove_meta_box function inside the remove_dashboard_widget function, which is indeed triggered by the wp_dashboard_setup action.

在这里,我们正在调用remove_dashboard_widget函数中的remove_meta_box函数,该函数实际上是由wp_dashboard_setup操作触发的。

remove_meta_box takes three arguments, they are:

remove_meta_box三个参数,它们是:

  1. $id: The unique id of the widget. Here’s the list of the id’s of the default widgets: dashboard_activity, dashboard_right_now, dashboard_recent_comments, dashboard_recent_drafts, dashboard_primary, dashboard_quick_press, dashboard_secondary, dashboard_primary and dashboard_incoming_links.
    $ id :小部件的唯一ID。 以下是默认小部件ID的列表: dashboard_activitydashboard_right_nowdashboard_recent_commentsdashboard_recent_draftsdashboard_primarydashboard_quick_pressdashboard_secondarydashboard_primarydashboard_incoming_links
  2. $location: This will always have the value “dashboard” while removing a dashboard.
    $ location :删除仪表板时,该位置始终具有“ dashboard”值。
  3. $position: If you’re removing a widget which was created by WordPress core by default, or created using wp_add_dashboard_widget then its value is “normal”.
    $ position :如果要删除默认情况下由WordPress核心创建的小部件,或使用wp_add_dashboard_widget创建的小部件,则其值为“正常”。

(Positioning Widgets in the Dashboard)

By default, the new widgets are added to bottom left of the dashboard page. The WordPress Dashboard Widgets API doesn’t provide a way to change the position of the widgets. However, we can hack the WordPress core $wp_meta_box variable as a workaround.

默认情况下,新的小部件将添加到仪表板页面的左下方。 WordPress仪表板小部件API没有提供更改小部件位置的方法。 但是,我们可以破解WordPress核心$wp_meta_box变量作为解决方法。

Here is an example on how we can move our widget to the top:

这是一个有关如何将小部件移至顶部的示例:

function add_dashboard_widget()
{
    wp_add_dashboard_widget("sitepoint", "SitePoint Videos", "display_sitepoint_dashboard_widget");
	
	global $wp_meta_boxes;
 	
 	// Get reference to the list of dashboard widgets
 	$dashboard_widgets = $wp_meta_boxes["dashboard"]["normal"]["core"];
 	
 	//Create a backup of the dashboard widgets list
 	$dashboard_widgets_backup = array("sitepoint" => $dashboard_widgets["example_dashboard_widget"] );
 	
 	//remove our widget from the end
 	unset($dashboard_widgets["example_dashboard_widget"]);
 
 	//Merge the backedup and original arrays
 	$sorted_dashboard = array_merge($dashboard_widgets_backup, $dashboard_widgets);
 
 	//save the new sorted widgets array
 	$wp_meta_boxes["dashboard"]["normal"]["core"] = $sorted_dashboard;
}

function display_sitepoint_dashboard_widget()
{
	echo "Watch Video Courses at <a href='http://learnable.com'>Learnable</a>";
}

add_action("wp_dashboard_setup", "add_dashboard_widget");

You can alter this array and change the position of the widgets as needed.

您可以根据需要更改此数组并更改小部件的位置。

(Displaying an RSS Feed in a Dashboard Widget)

Let’s create a dashboard widget which retrieves SitePoint’s RSS feed, then displays it. We’ll use WordPress’ API to fetch and parse the RSS feed.

让我们创建一个仪表板小部件,该小部件检索SitePoint的RSS feed,然后显示它。 我们将使用WordPress的API来获取和解析RSS feed。

Here’s the code for our widget:

这是我们的小部件的代码:

function add_dashboard_widget()
{
    wp_add_dashboard_widget("rss-feed", "RSS", "display_rss_dashboard_widget");
}

function display_rss_dashboard_widget()
{

	include_once( ABSPATH . WPINC . "/feed.php");

	$rss = fetch_feed("https://www.sitepoint.com/feed/");
    $maxitems = $rss->get_item_quantity(10); 
    $rss_items = $rss->get_items(0, $maxitems);
	
	?>

	<ul>
		<?php
			if($maxitems == 0)
			{
				echo "<li>No items</li>";
			}
			else
			{
				foreach($rss_items as $item)
				{
					?>
						<li>
			                <a href="<?php echo esc_url($item->get_permalink()); ?>">
			                    <?php echo esc_html($item->get_title()); ?>
			                </a>
			            </li>
					<?php
				}
			}
		?>
	</ul>

	<?php
}

add_action("wp_dashboard_setup", "add_dashboard_widget");

Here, we are including the file “feed.php”, which exposes all of the APIs needed to work with RSS in WordPress. Then, we use fetch_feed to download and parse the RSS feed, finally we display 10 RSS items.

在这里,我们包括文件“ feed.php”,该文件公开了在WordPress中使用RSS所需的所有API。 然后,我们使用fetch_feed下载并解析RSS feed,最后显示10个RSS项目。

Here is what our RSS dashboard widget should look like:

这是我们的RSS仪表板小部件的外观:


wordpress客服插件代码_javascript_02


(Conclusion)

Many WordPress plugins such as analytics, WooCommerce addons and various monitoring extensions use the Dashboard Widgets API. The WordPress Dashboard Widgets API is one of the most important APIs for WordPress developers, in this example we’ve walked you through some basic examples. Please feel free to comment on your experiences with the API below.

许多WordPress插件(例如分析,WooCommerce插件和各种监视扩展)都使用仪表板小部件API。 WordPress Dashboard Widgets API是WordPress开发人员最重要的API之一,在此示例中,我们为您介绍了一些基本示例。 请随时在下面对您使用API的经历发表评论。

翻译自: https://www.sitepoint.com/wordpress-dashboard-widgets-api/

wordpress api