Create Custom widget in function.php in WordPress

PHP Tutorial

To create custom widget ***\wordpress\wp-content\themes\theme_name\function.php

Then copy and paste this below code.

class MyNewWidget extends WP_Widget {

function __construct() {
$widget_ops = array(‘classname’ => ‘widget_mywidgets’, ‘description’ => __( “The my recent posts on your site”) );
parent::__construct(‘my-recent-posts’, __(‘New Widget Posts’), $widget_ops);
$this->alt_option_name = ‘widget_posts_entries’;
add_action( ‘save_post’, array($this, ‘flush_widget_cache’) );
add_action( ‘deleted_post’, array($this, ‘flush_widget_cache’) );
add_action( ‘switch_theme’, array($this, ‘flush_widget_cache’) );

function widget( $args, $instance ) {
// Widget output
$cache = wp_cache_get(‘widget_post_lists’, ‘widget’);
if ( empty( $instance[‘number’] ) || ! $number = absint( $instance[‘number’] ) )
$number = 6;
$sortby = !empty($instance[‘sortby’] ) ? $instance[‘sortby’] : ‘ASC’;
$category = !empty($instance[‘category’] ) ? $instance[‘category’] : 4;
$format = !empty($instance[‘format’] ) ? $instance[‘format’] : ‘ul’;
$custom_class = !empty($instance[‘custom_class’] ) ? $instance[‘custom_class’] : null;
$r = new WP_Query( apply_filters( ‘widget_posts_args’, array( ‘posts_per_page’ => $number, ‘no_found_rows’ => true, ‘post_status’ => ‘publish’, ‘ignore_sticky_posts’ => true,’cat’=> $category  ,’order’ => $sortby) ) );

if ($r->have_posts()) :

View original post 474 more words


13 thoughts on “Create Custom widget in function.php in WordPress

  1. Admiring the hard work you put into your website
    and detailed information you provide. It’s nice to come across a blog every once in a while that isn’t the same out of date rehashed material.
    Great read! I’ve bookmarked your site and I’m adding your RSS feeds to my Google account.

  2. I blog quite often and I really appreciate your content.
    This article has truly peaked my interest. I’m going to book mark your site and keep checking for new information about once per week. I subscribed to your RSS feed too.

  3. As such, it mustn’t be too difficult to invest some time building a database of tables like what is suggested at where they’ve provided a sample
    database spec for use in a college accommodation reservation application.
    A very convenient approach to search for things on the web is to
    simply speak your quest term. There are a few significant improvements made over Vista, and also for the most part, it’s simply a version of Vista that is both stable plus much more user-friendly.

  4. Wonderful goods from you, man. I’ve understand your stuff previous to and you are just extremely wonderful. I actually like what you’ve
    acquired here, really like what you are saying and the way in which you
    say it. You make it entertaining and you still take care of to keep it wise.

    I cant wait to read far more from you. This is actually a tremendous web site.

  5. I was suggested this web site via my cousin.

    I am no longer positive whether this post is written via
    him as nobody else recognize such detailed about my difficulty.
    You’re incredible! Thank you!

  6. When I seek patent protection on my inventions where I work, we use
    patent attorneys. 2) Public use in the United States: If the invention you wish
    to patent was used in the United States by you or another more than one year before your filing of a patent application,
    then you are “barred” from ever obtaining a patent on your invention.
    Hero’s vending machine was like this, a person would deposit the coin into a slot, and the coin would fall into a pan.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s