How To Create Dashboard Widget WordPress Plugin

Dashboard widget is widget which is shown in the administration dashboard.

Code which ussualy used :

  • wp_add_dashboard_widget
  • add_action , wp_dashboard_setup

The minimalist code which can be used :

/**
 * Add a widget to the dashboard.
 *
 * This function is hooked into the 'wp_dashboard_setup' action below.
 */
function zz_add_dashboard_widgets() {

	wp_add_dashboard_widget(
                 'zz_dashboard_widget',         // Widget slug.
                 'ZZ Dashboard Widget',         // Title.
                 'zz_dashboard_widget_function' // Display function.
        );	
}
add_action( 'wp_dashboard_setup', 'zz_add_dashboard_widgets' );

/**
 * Create the function to output the contents of our Dashboard Widget.
 */
function zz_dashboard_widget_function() {

	// Display whatever it is you want to show.
	echo "<h2>RSS Feed</h2>";
	output_rss_feed('http://wpamanuke.com/?feed=rss', 5, true, true, 200);
}

Complete Code

<?php
/*
Plugin Name: ZZ Dashboard Widgets
Plugin URI: http://www.wpamanuke.com
Description: Sample Dashboard Widgets Code
Version: 1.0.1
Author: WPAmaNuke
Author URI: https://wpamanuke.com
*/

/**
 * Add a widget to the dashboard.
 *
 * This function is hooked into the 'wp_dashboard_setup' action below.
 */
function zz_add_dashboard_widgets() {

	wp_add_dashboard_widget(
                 'zz_dashboard_widget',         // Widget slug.
                 'ZZ Dashboard Widget',         // Title.
                 'zz_dashboard_widget_function' // Display function.
        );	
}
add_action( 'wp_dashboard_setup', 'zz_add_dashboard_widgets' );

/**
 * Create the function to output the contents of our Dashboard Widget.
 */
function zz_dashboard_widget_function() {

	// Display whatever it is you want to show.
	echo "<h2>RSS Feed</h2>";
	output_rss_feed('http://wpamanuke.com/?feed=rss', 5, true, true, 200);
}

function get_rss_feed_as_html($feed_url, $max_item_cnt = 10, $show_date = true, $show_description = true, $max_words = 0)
{
    $result = "";
    // get feeds and parse items
    $rss = new DOMDocument();
	$rss->load($feed_url);

    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
        $item = array (
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'content' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
        );
        $content = $node->getElementsByTagName('encoded'); // <content:encoded>
        if ($content->length > 0) {
            $item['content'] = $content->item(0)->nodeValue;
        }
        array_push($feed, $item);
    }
    // real good count
    if ($max_item_cnt > count($feed)) {
        $max_item_cnt = count($feed);
    }
    $result .= '<ul class="feed-lists">';
    for ($x=0;$x<$max_item_cnt;$x++) {
        $title = str_replace(' & ', ' & ', $feed[$x]['title']);
        $link = $feed[$x]['link'];
        $result .= '<li class="feed-item">';
        $result .= '<div class="feed-title"><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong></div>';
        if ($show_date) {
            $date = date('l F d, Y', strtotime($feed[$x]['date']));
            $result .= '<small class="feed-date"><em>Posted on '.$date.'</em></small>';
        }
        if ($show_description) {
            $description = $feed[$x]['desc'];
            $content = $feed[$x]['content'];
            // find the img
            $has_image = preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $content, $image);
            // no html tags
            $description = strip_tags(preg_replace('/(<(script|style)\b[^>]*>).*?(<\/\2>)/s', "$1$3", $description), '');
            // whether cut by number of words
            if ($max_words > 0) {
                $arr = explode(' ', $description);
                if ($max_words < count($arr)) {
                    $description = '';
                    $w_cnt = 0;
                    foreach($arr as $w) {
                        $description .= $w . ' ';
                        $w_cnt = $w_cnt + 1;
                        if ($w_cnt == $max_words) {
                            break;
                        }
                    }
                    $description .= " ...";
                }
            }
            // add img if it exists
            if ($has_image == 1) {
                $description = '<img class="feed-item-image" src="' . $image['src'] . '" />' . $description;
            }
            $result .= '<div class="feed-description">' . $description;
            $result .= ' <a href="'.$link.'" title="'.$title.'">Continue Reading »</a>'.'</div>';
        }
        $result .= '</li>';
    }
    $result .= '</ul>';
    return $result;
}
function output_rss_feed($feed_url, $max_item_cnt = 10, $show_date = true, $show_description = true, $max_words = 0)
{
    echo get_rss_feed_as_html($feed_url, $max_item_cnt, $show_date, $show_description, $max_words);
}

Leave a Reply

Your email address will not be published. Required fields are marked *