17713433920 info@mac163.com
如何在WordPress仪表板中添加自定义RSS Feed
如何在WordPress仪表板中添加自定义RSS Feed

互联网上充斥着巨大的资源,很难一窥所有事物。当然,有Twitter来关注趋势或RSS阅读器软件,但是像我一样,您很忙,有时同时打开20个软件很烦人。这就是为什么我决定将WordPress仪表板用作全球平台来快速访问这些提要的原因。我今天要分享的一个示例是如何在WordPress仪表板中创建自定义RSS元框。

这是我们要创建的最终结果:

添加一个自定义RSS仪表板元框
添加一个自定义RSS仪表板元框

步骤1:外挂程式

要添加此metabox,我们需要创建一个插件。因此,只需在wp-content / plugins /中创建一个名为“ my-dashboard-metaboxes”的新文件夹,然后在新文件夹中创建一个名为my-dashboard-metaboxes.php的文件。该文件将成为主插件文件。将其打开到主编辑器中。下面的代码是将生成插件的代码。这里没有什么真正复杂的:

<?php
/*
Plugin Name: My Dashboard Metaboxes
Plugin URL: http://remicorson.com/
Description: Adds custom meta boxes on the main admin dashboard
Version: 0.1
Author: Remi Corson
Author URI: http://remicorson.com
Contributors: corsonr
Text Domain: rc_mdm
*/

步骤2:注册Metabox

现在我们有了一个空插件(我的意思是一个不做任何事情的插件),我们需要至少注册一个metabox才能在WordPress仪表板上显示。为此,我们必须创建一个新函数,该函数将钩住“ wp_dashboard_setup ”钩子。我们将此函数称为“ rc_mdm_register_widgets()”。在此函数内部,我们需要告诉WordPress我们要添加一个新的metabox,这是“ wp_add_dashboard_widget() ”函数的目标。此函数接受4个参数:

1 – $ widget_id(整数)(必需),它是小部件的识别块。这将用作其CSS类以及其在小部件数组中的键。
默认值:无

2 – $ widget_name(字符串)(必填),这是您的小部件将在其标题中显示的名称。
默认值:无

3 – $ callback(字符串)(必需)您创建的函数的名称,该函数将显示小部件的实际内容。
默认值:无

4 – $ control_callback(字符串)(可选)您创建的函数的名称,该函数将处理小部件选项(配置)表单的提交,并且还将显示表单元素。

这里重要的是第三个参数,它是定义将要加载到metabox中的函数的参数。在此示例中,它称为“ rc_mdm_create_my_rss_box()”。

/**
 * Register all dashboard metaboxes
 *
 * @access      public
 * @since       1.0 
 * @return      void
*/

function rc_mdm_register_widgets() {
	global $wp_meta_boxes;
	
	wp_add_dashboard_widget('widget_freelanceswitch', __('My RSS Feeds', 'rc_mdm'), 'rc_mdm_create_my_rss_box');
}
add_action('wp_dashboard_setup', 'rc_mdm_register_widgets');

步骤3:Metabox内容

如果您激活插件并转到WordPress仪表板,则应该看到一个新的空metabox。现在,我们需要填写其内容。此函数的重要事项是包括WordPress内置的“ feed.php”文件,允许其使用“ fetch_feed()”函数。请注意,由于不建议使用“ fetch_rss()”,“ get_rss()”和“ wp_rss()”,因此我们使用的是“ fetch_feed()”。一次,我直接在代码中包含了所有注释,但是我想在metabox函数中使用的一些不错的功能上引起您的注意。

首先是“ fetch_feed() ”函数。此代码用于获取和解析提要内容。该函数使用SimplePie类,因此您几乎可以利用其中包含的所有函数。

然后,我们有了“ human_time_diff()”函数,该函数用于将时间显示为“ human_time_diff() ”,例如,显示诸如“ 2小时前”,“ 4天前”等内容……这是一个WordPress函数。

最后,我们有“ wp_html_excerpt() ”来缩短每个提要内容。

所有其他功能都是众所周知的WordPress功能,或包含在Simple Pie类中。

这是代码:

/**
 * Creates the RSS metabox
 *
 * @access      public
 * @since       1.0 
 * @return      void
*/

function rc_mdm_create_my_rss_box() {
	
	// Get RSS Feed(s)
	include_once(ABSPATH . WPINC . '/feed.php');
	
	// My feeds list (add your own RSS feeds urls)
	$my_feeds = array( 
				'http://feeds.feedburner.com/FSAllJobs', 
				'http://www.wphired.com/feed/?post_type=job_listing' 
				);
	
	// Loop through Feeds
	foreach ( $my_feeds as $feed) :
	
		// Get a SimplePie feed object from the specified feed source.
		$rss = fetch_feed( $feed );
		if (!is_wp_error( $rss ) ) : // Checks that the object is created correctly 
		    // Figure out how many total items there are, and choose a limit 
		    $maxitems = $rss->get_item_quantity( 3 ); 
		
		    // Build an array of all the items, starting with element 0 (first element).
		    $rss_items = $rss->get_items( 0, $maxitems ); 
	
		    // Get RSS title
		    $rss_title = '<a href="'.$rss->get_permalink().'" target="_blank">'.strtoupper( $rss->get_title() ).'</a>'; 
		endif;
	
		// Display the container
		echo '<div class="rss-widget">';
		echo '<strong>'.$rss_title.'</strong>';
		echo '<hr style="border: 0; background-color: #DFDFDF; height: 1px;">';
		
		// Starts items listing within <ul> tag
		echo '<ul>';
		
		// Check items
		if ( $maxitems == 0 ) {
			echo '<li>'.__( 'No item', 'rc_mdm').'.</li>';
		} else {
			// Loop through each feed item and display each item as a hyperlink.
			foreach ( $rss_items as $item ) :
				// Uncomment line below to display non human date
				//$item_date = $item->get_date( get_option('date_format').' @ '.get_option('time_format') );
				
				// Get human date (comment if you want to use non human date)
				$item_date = human_time_diff( $item->get_date('U'), current_time('timestamp')).' '.__( 'ago', 'rc_mdm' );
				
				// Start displaying item content within a <li> tag
				echo '<li>';
				// create item link
				echo '<a href="'.esc_url( $item->get_permalink() ).'" title="'.$item_date.'">';
				// Get item title
				echo esc_html( $item->get_title() );
				echo '</a>';
				// Display date
				echo ' <span class="rss-date">'.$item_date.'</span><br />';
				// Get item content
				$content = $item->get_content();
				// Shorten content
				$content = wp_html_excerpt($content, 120) . ' [...]';
				// Display content
				echo $content;
				// End <li> tag
				echo '</li>';
			endforeach;
		}
		// End <ul> tag
		echo '</ul></div>';

	endforeach; // End foreach feed
}

在第15行,有一个数组,您可以在其中放置任意数量的提要。您还可以定义要在第27行显示的每个提要项的数量。最后,在第50和54行,您可以选择显示人工日期或正常日期。由你决定。

结论

好的,所以我们创建了一个简单的metabox,但是现在您已经具备了使用自己的内容创建自己的metabox的基础。您还可以删除默认的WordPress metabox,并全面了解仪表板小部件API,我鼓励您像往常一样看一下Codex


微信二维码

微信扫描二维码联系我们!
我们在微信上24小时期待你的声音
提供外贸路由器设备产品,轻松翻墙,解答:WP主题推荐,WP网站建设,Google SEO,百度SEO,专业服务器环境搭建等!


需要提供WordPress主题/插件的汉化服务可以随时联系我们!另外成品WordPress网站以及半成品WordPress网站建设,海外Google SEO优化托管服务,百度SEO优化托管服务,Centos/Debian服务器WP专用环境搭建,WP缓存服务器搭建,我们都是你的首选,拥有多年WP开源程序服务经验,我们一直在坚持客户体验,没有最好,只有更好!
回到顶部