I am trying to create a custom recent posts widget by forking the default WordPress widget. I understand you shouldn;t hack core code, but rather fork the original widget and then register it in the functions file.
The only thing is, I don't know where to fork. I've found the default widget and I know that I'd register the widget in my theme's functions.php file, I just don't know where to save the code I'm forking. For example, do I save it as a plugin in the plugins folder, etc.?
Here's the default WordPress code:
/**
* Recent_Posts widget class
*
* #since 2.8.0
*/
class WP_Widget_Recent_Posts extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "Your site’s most recent Posts.") );
parent::__construct('recent-posts', __('Recent Posts'), $widget_ops);
$this->alt_option_name = 'widget_recent_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) {
$cache = array();
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( 'widget_recent_posts', 'widget' );
}
if ( ! is_array( $cache ) ) {
$cache = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
ob_start();
extract($args);
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' );
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
if ( ! $number )
$number = 5;
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
/**
* Filter the arguments for the Recent Posts widget.
*
* #since 3.4.0
*
* #see WP_Query::get_posts()
*
* #param array $args An array of arguments used to retrieve the recent posts.
*/
$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
) ) );
if ($r->have_posts()) :
?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<ul>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li>
<?php get_the_title() ? the_title() : the_ID(); ?>
<?php if ( $show_date ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $after_widget; ?>
<?php
// Reset the global $the_post as this query will have stomped on it
wp_reset_postdata();
endif;
if ( ! $this->is_preview() ) {
$cache[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'widget_recent_posts', $cache, 'widget' );
} else {
ob_end_flush();
}
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = (int) $new_instance['number'];
$instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['widget_recent_entries']) )
delete_option('widget_recent_entries');
return $instance;
}
function flush_widget_cache() {
wp_cache_delete('widget_recent_posts', 'widget');
}
function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
$show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p>
<?php
}
}
Create a folder in Plugins and save it in there.
You don't have to create a new folder, but I would advise it just to keep the project clean.
More info here : http://codex.wordpress.org/Writing_a_Plugin
Related
hi i need to registry two widget of two post type. the first type is movies and the second is news
i have add the widget to function but is showing just one widget of them.
i have create two classes and when i test only one of theme its working good ? but when i add it together i see only one of them
this is my code
class Movies_Recent_Posts extends WP_Widget {
public function __construct() {
$widget_ops = array('classname' => 'Movies_Recent_Posts', 'description' => esc_html__( "Latest movies.",'artor') );
parent::__construct('wpsites-recent-posts', esc_html__('Recent Movies','artor'), $widget_ops);
$this->alt_option_name = 'Movies_Recent_Posts';
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') );
}
public function widget($args, $instance) {
$cache = array();
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( 'movies_widget_recent_posts', 'widget' );
}
if ( ! is_array( $cache ) ) {
$cache = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
ob_start();
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : esc_html__( 'Recent Movies','artor' );
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
if ( ! $number )
$number = 5;
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
$r = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number,
'no_found_rows' => true,
'post_status' => 'publish',
'post_type' => array('movies',
'ignore_sticky_posts' => true
) ) ) );
if ($r->have_posts()) :
?>
<?php echo $args['before_widget']; ?>
<?php if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
} ?>
<ul>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li>
<?php get_the_title() ? the_title() : the_ID(); ?>
<?php if ( $show_date ) : ?>
<small class="wmovie-date"><?php echo get_the_date(); ?></small>
<hr>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $args['after_widget']; ?>
<?php
wp_reset_postdata();
endif;
if ( ! $this->is_preview() ) {
$cache[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'movies_widget_recent_posts', $cache, 'widget' );
} else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = (int) $new_instance['number'];
$instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['Movies_Recent_Posts']) )
delete_option('Movies_Recent_Posts');
return $instance;
}
public function flush_widget_cache() {
wp_cache_delete('movies_widget_recent_posts', 'widget');
}
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
$show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo esc_html__( 'Title:' ,'artor'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php echo esc_html__( 'Number of movies to show:','artor' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php echo esc_html__( 'Display movies date?','artor' ); ?></label></p>
<?php
}
}
class News_Recent_Posts extends WP_Widget {
public function __construct() {
$widget_news = array('classname' => 'News_Recent_Posts', 'description' => esc_html__( 'Recent News.','artor') );
parent::__construct('wpsites-recent-posts', esc_html__('Recent News','artor'), $widget_news);
$this->alt_option_name = 'News_Recent_Posts';
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') );
}
public function widget($args, $instance_news) {
$cache_news = array();
if ( ! $this->is_preview() ) {
$cache_news = wp_cache_get( 'news_widget_recent_posts', 'widget' );
}
if ( ! is_array( $cache_news ) ) {
$cache_news = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache_news[ $args['widget_id'] ] ) ) {
echo $cache_news[ $args['widget_id'] ];
return;
}
ob_start();
$title_news = ( ! empty( $instance_news['title'] ) ) ? $instance_news['title'] : esc_html__( 'Recent news','artor' );
/** This filter is documented in wp-includes/default-widgets.php */
$title_news = apply_filters( 'widget_title', $title_news, $instance_news, $this->id_base );
$number_news = ( ! empty( $instance_news['number'] ) ) ? absint( $instance_news['number'] ) : 5;
if ( ! $number_news )
$number_news = 5;
$show_date_news = isset( $instance_news['show_date'] ) ? $instance_news['show_date'] : false;
$r_news = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number_news,
'no_found_rows' => true,
'post_status' => 'publish',
'post_type' => array('news',
'ignore_sticky_posts' => true
) ) ) );
if ($r_news->have_posts()) :
?>
<?php echo $args['before_widget']; ?>
<?php if ( $title_news ) {
echo $args['before_title'] . $title_news . $args['after_title'];
} ?>
<ul>
<?php while ( $r_news->have_posts() ) : $r_news->the_post(); ?>
<li>
<?php get_the_title() ? the_title() : the_ID(); ?>
<?php if ( $show_date_news ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $args['after_widget']; ?>
<?php
wp_reset_postdata();
endif;
if ( ! $this->is_preview() ) {
$cache_news[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'news_widget_recent_posts', $cache_news, 'widget' );
} else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance_news ) {
$instance_news = $old_instance_news;
$instance_news['title'] = strip_tags($new_instance['title']);
$instance_news['number'] = (int) $new_instance['number'];
$instance_news['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions_news = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions_news['News_Recent_Posts']) )
delete_option('News_Recent_Posts');
return $instance_news;
}
public function flush_widget_cache() {
wp_cache_delete('news_widget_recent_posts', 'widget');
}
public function form( $instance_news ) {
$title_news = isset( $instance_news['title'] ) ? esc_attr( $instance_news['title'] ) : '';
$number_news = isset( $instance_news['number'] ) ? absint( $instance_news['number'] ) : 5;
$show_date_news = isset( $instance_news['show_date'] ) ? (bool) $instance_news['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo esc_html__( 'Title:','artor' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title_news; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php echo esc_html__( 'Number of news to show:','artor' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number_news; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date_news ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php echo esc_html__( 'Display news date?' ,'artor'); ?></label></p>
<?php
}
}
function load_widgets() {
register_widget('Movies_Recent_Posts');
register_widget('News_Recent_Posts');
}
add_action( 'widgets_init', 'load_widgets');
Sorry i have fixed . thank you all
class Movies_Recent_Posts extends WP_Widget {
public function __construct() {
$widget_ops = array('classname' => 'Movies_Recent_Posts', 'description' => esc_html__( "Latest movies.",'artor') );
parent::__construct('widjet-recent-movies', esc_html__('Recent Movies','artor'), $widget_ops);
$this->alt_option_name = 'Movies_Recent_Posts';
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') );
}
public function widget($args, $instance) {
$cache = array();
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( 'movies_widget_recent_posts', 'widget' );
}
if ( ! is_array( $cache ) ) {
$cache = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
ob_start();
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : esc_html__( 'Recent Movies','artor' );
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
if ( ! $number )
$number = 5;
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
$r = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number,
'no_found_rows' => true,
'post_status' => 'publish',
'post_type' => array('movies',
'ignore_sticky_posts' => true
) ) ) );
if ($r->have_posts()) :
?>
<?php echo $args['before_widget']; ?>
<?php if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
} ?>
<ul>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li>
<?php get_the_title() ? the_title() : the_ID(); ?>
<?php if ( $show_date ) : ?>
<small class="wmovie-date"><?php echo get_the_date(); ?></small>
<hr>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $args['after_widget']; ?>
<?php
wp_reset_postdata();
endif;
if ( ! $this->is_preview() ) {
$cache[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'movies_widget_recent_posts', $cache, 'widget' );
} else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = (int) $new_instance['number'];
$instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['Movies_Recent_Posts']) )
delete_option('Movies_Recent_Posts');
return $instance;
}
public function flush_widget_cache() {
wp_cache_delete('movies_widget_recent_posts', 'widget');
}
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
$show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo esc_html__( 'Title:' ,'artor'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php echo esc_html__( 'Number of movies to show:','artor' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php echo esc_html__( 'Display movies date?','artor' ); ?></label></p>
<?php
}
}
class News_Recent_Posts extends WP_Widget {
public function __construct() {
$widget_news = array('classname' => 'News_Recent_Posts', 'description' => esc_html__( 'Recent News.','artor') );
parent::__construct('widjet-recent-news', esc_html__('Recent News','artor'), $widget_news);
$this->alt_option_name = 'News_Recent_Posts';
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') );
}
public function widget($args, $instance_news) {
$cache_news = array();
if ( ! $this->is_preview() ) {
$cache_news = wp_cache_get( 'news_widget_recent_posts', 'widget' );
}
if ( ! is_array( $cache_news ) ) {
$cache_news = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache_news[ $args['widget_id'] ] ) ) {
echo $cache_news[ $args['widget_id'] ];
return;
}
ob_start();
$title_news = ( ! empty( $instance_news['title'] ) ) ? $instance_news['title'] : esc_html__( 'Recent news','artor' );
/** This filter is documented in wp-includes/default-widgets.php */
$title_news = apply_filters( 'widget_title', $title_news, $instance_news, $this->id_base );
$number_news = ( ! empty( $instance_news['number'] ) ) ? absint( $instance_news['number'] ) : 5;
if ( ! $number_news )
$number_news = 5;
$show_date_news = isset( $instance_news['show_date'] ) ? $instance_news['show_date'] : false;
$r_news = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number_news,
'no_found_rows' => true,
'post_status' => 'publish',
'post_type' => array('news',
'ignore_sticky_posts' => true
) ) ) );
if ($r_news->have_posts()) :
?>
<?php echo $args['before_widget']; ?>
<?php if ( $title_news ) {
echo $args['before_title'] . $title_news . $args['after_title'];
} ?>
<ul>
<?php while ( $r_news->have_posts() ) : $r_news->the_post(); ?>
<li>
<?php get_the_title() ? the_title() : the_ID(); ?>
<?php if ( $show_date_news ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $args['after_widget']; ?>
<?php
wp_reset_postdata();
endif;
if ( ! $this->is_preview() ) {
$cache_news[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'news_widget_recent_posts', $cache_news, 'widget' );
} else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance_news ) {
$instance_news = $old_instance_news;
$instance_news['title'] = strip_tags($new_instance['title']);
$instance_news['number'] = (int) $new_instance['number'];
$instance_news['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions_news = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions_news['News_Recent_Posts']) )
delete_option('News_Recent_Posts');
return $instance_news;
}
public function flush_widget_cache() {
wp_cache_delete('news_widget_recent_posts', 'widget');
}
public function form( $instance_news ) {
$title_news = isset( $instance_news['title'] ) ? esc_attr( $instance_news['title'] ) : '';
$number_news = isset( $instance_news['number'] ) ? absint( $instance_news['number'] ) : 5;
$show_date_news = isset( $instance_news['show_date'] ) ? (bool) $instance_news['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo esc_html__( 'Title:','artor' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title_news; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php echo esc_html__( 'Number of news to show:','artor' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number_news; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date_news ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php echo esc_html__( 'Display news date?' ,'artor'); ?></label></p>
<?php
}
}
function load_widgets() {
register_widget('Movies_Recent_Posts');
register_widget('News_Recent_Posts');
}
add_action( 'widgets_init', 'load_widgets');
I'm using a specific widget that coded myself and use this widget on main page almost 18 times for displaying different category posts. does it cause bad loading in main page or not?! if I use normal coding instead of the widget is that better or not different?! You can check my website loading http://akhbartop.com/ In my system and some friends loading of the main page is not good. I want to know more widget in main page related to loading or not?! What do you suggest instead using a widget?!
this is my codes in widget
<?php
// Creating the widget
class wpb_box extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_box',
// Widget name will appear in UI
__('ابزارک اختصاصی باکس مطالب خبرخوان', 'bigtheme'),
// Widget description
array( 'description' => __( 'ابزارک نمایش باکس مطالب از سایت های مختلف توسط آدرس خوراک یا فید سایت', 'bigtheme' ), )
);
add_action('save_post', array( $this, 'delete_query_caches') );
}
// Creating widget front-end
// This is where the action happens
/**
* Delete transients
*/
function delete_query_caches( $post_id ){
if( !isset( $_POST['post_type'] ) || $_POST['post_type'] !== 'post' ) return;
$categories = wp_get_post_terms( $post_id, 'category' );
if( $categories && ! is_WP_Error( $categories ) ) {
foreach( $categories as $cat ) {
delete_transient('post'.$cat->term_id.'_query');
}
}
}
public function widget( $args, $instance ) {
$name = apply_filters( 'widget_title', $instance['name'] );
$category = apply_filters( 'widget_title', $instance['category'] );
$id = apply_filters( 'widget_title', $instance['id'] );
$link = apply_filters( 'widget_title', $instance['link'] );
$display = apply_filters( 'widget_title', $instance['display'] );
$color = apply_filters( 'widget_title', $instance['color'] );
// This is where you run the code and display the output
?>
<div class="col-sm-6 col-xs-12 padding5">
<div class="article">
<div class="title">
<h3><?php echo $name ?></h3>
<div class="clear"></div>
<div class="line"></div>
</div>
<?php
if ( false === ( $slider = get_transient( 'post'.$id.'_query' ) ) ) {
$slider = new WP_Query(array(
'post_status' =>'publish',
'post_type' =>'post',
'cat' =>''.$id.'',
'posts_per_page' =>'9'
));
// Make a new query cache for 1 week
set_transient( 'post'.$id.'_query', $slider, 168 * HOUR_IN_SECONDS );
}
if( !$slider->have_posts() ) return; ?>
<ul>
<?php while( $slider->have_posts() ) : $slider->the_post(); ?>
<li>
<?php the_title(); ?>
<div style="display:<?php echo $display;?>" class="tooltiptext hidden-xs"><?php the_excerpt(); ?></div>
</li>
<?php endwhile; $slider->rewind_posts(); ?>
</ul>
<?php wp_reset_postdata(); ?>
<div class="list">مشاهده آرشیو کامل<img src="<?php bloginfo('template_url'); ?>/images/rss.png" width="18" height="18" alt="خوراک سایت" ></div>
</div>
</div>
<?php
echo $args['after_widget'];
}
public function form( $instance ) {
$name = ( isset( $instance[ 'name' ] ) ) ? $instance[ 'name' ] : '';
$category = ( isset( $instance[ 'category' ] ) ) ? $instance[ 'category' ] : '';
$link = ( isset( $instance[ 'link' ] ) ) ? $instance[ 'link' ] : '';
$color = ( isset( $instance[ 'color' ] ) ) ? $instance[ 'color' ] : '';
$id = ( isset( $instance[ 'id' ] ) ) ? $instance[ 'id' ] : '';
$display = ( isset( $instance[ 'display' ] ) ) ? $instance[ 'display' ] : '';
?>
<p>
<label for="<?php echo $this->get_field_id( 'color' ); ?>"><?php _e( 'رنگ باکس مطالب:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'color' ); ?>" name="<?php echo $this->get_field_name( 'color' ); ?>" type="text" value="<?php echo esc_attr( $color ); ?>" placeholder="مثال : #CCC , #dd3333 , black , blue" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e( 'عنوان باکس:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" type="text" value="<?php echo esc_attr( $name ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'id' ); ?>"><?php _e( 'آی دی دسته بندی' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'id' ); ?>" name="<?php echo $this->get_field_name( 'id' ); ?>" type="text" value="<?php echo esc_attr( $id ); ?>" />
</p>
<select id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" class="widefat" style="width:100%;">
<?php foreach(get_terms('category','parent=0&hide_empty=0') as $term) { ?>
<option <?php selected( $instance['category'], $term->term_id ); ?> value="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></option>
<?php } ?>
</select>
<p>
<label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e( 'لینک آرشیو' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" type="text" value="<?php echo esc_attr( $link ); ?>" />
</p>
<p>
<label><?php _e( 'نمایش توضیحات مطالب' ); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id( 'display' ); ?>" name="<?php echo $this->get_field_name( 'display' ); ?>">
<option <?php selected( $instance['display'], 'block'); ?> value="block">بله</option>
<option <?php selected( $instance['display'], 'none'); ?> value="none">خیر</option>
</select>
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['name'] = ( ! empty( $new_instance['name'] ) ) ? strip_tags( $new_instance['name'] ) : '';
$instance['category'] = ( ! empty( $new_instance['category'] ) ) ? strip_tags( $new_instance['category'] ) : '';
$instance['link'] = ( ! empty( $new_instance['link'] ) ) ? strip_tags( $new_instance['link'] ) : '';
$instance['link2'] = ( ! empty( $new_instance['link2'] ) ) ? strip_tags( $new_instance['link2'] ) : '';
$instance['id'] = ( ! empty( $new_instance['id'] ) ) ? strip_tags( $new_instance['id'] ) : '';
$instance['link3'] = ( ! empty( $new_instance['link3'] ) ) ? strip_tags( $new_instance['link3'] ) : '';
$instance['link4'] = ( ! empty( $new_instance['link4'] ) ) ? strip_tags( $new_instance['link4'] ) : '';
$instance['link5'] = ( ! empty( $new_instance['link5'] ) ) ? strip_tags( $new_instance['link5'] ) : '';
$instance['color'] = ( ! empty( $new_instance['color'] ) ) ? strip_tags( $new_instance['color'] ) : '';
$instance['display'] = ( ! empty( $new_instance['display'] ) ) ? strip_tags( $new_instance['display'] ) : '';
$instance['source'] = ( ! empty( $new_instance['source'] ) ) ? strip_tags( $new_instance['source'] ) : '';
$instance['time'] = ( ! empty( $new_instance['time'] ) ) ? strip_tags( $new_instance['time'] ) : '';
return $instance;
}
} // Class wpb_box ends here
// Register and load the widget
function wpb_box() {
register_widget( 'wpb_box' );
}
add_action( 'widgets_init', 'wpb_box' );
?>
i did some changes and put transient api codes in widget but still has problem with widget updates!!!when posts publish widgets won't update!!
If you're using too many queries, caching the queries might be helpful.
Let's say your custom widget looks like this:
<?php
/**
* My widget class
*/
class dw_myWidget extends WP_Widget {
function __construct() {
// Instantiate the parent object
parent::__construct( false, __('My widget') );
}
function widget( $args, $instance ) {
$title = apply_filters( 'title', $instance['title'] );
$link1 = apply_filters( 'category', $instance['category'] ); // category id
$portfolio = new WP_Query(array(
'post_status' =>'publish',
'post_type' =>'post',
'cat' =>''.$link1.'',
'posts_per_page' =>'9'
));
// And the rest of the code
}
function update( $new_instance, $old_instance ) {
// update stuff
}
function form( $instance ) {
// widget form stuff
}
}
function dw_MyWidget_register() {
register_widget( 'dw_myWidget' );
}
add_action( 'widgets_init', 'dw_MyWidget_register' );
We cache the query using Wordpress's transient key, but since we don't know how many widgets will be out there we must include a unique thing in our transient API, like the $link1 variable which represents the category id:
function widget( $args, $instance ) {
$title = apply_filters( 'title', $instance['title'] );
$link1 = apply_filters( 'category', $instance['category'] ); // category id
if ( false === ( $portfolio = get_transient( 'post'.$link1.'_query' ) ) ) {
$portfolio = new WP_Query(array(
'post_status' =>'publish',
'post_type' =>'post',
'cat' =>''.$link1.'',
'posts_per_page' =>'9'
));
set_transient( 'post'.$link1.'_query', $portfolio, 168 * HOUR_IN_SECONDS );
}
// And the rest of the code
}
That's not all, we need to delete the transient when a new post is created, so we register a hook to save_post action, in the constructor
function __construct() {
// Instantiate the parent object
parent::__construct( false, __('My widget') );
add_action('save_post', array( $this, 'delete_query_caches') );
}
/**
* Delete transients
*/
function delete_query_caches( $post_id ){
if( !isset( $_POST['post_type'] ) || $_POST['post_type'] !== 'post' ) return;
$categories = wp_get_post_terms( $post_id, 'category' );
if( $categories && ! is_WP_Error( $categories ) ) {
foreach( $categories as $cat ) {
delete_transient('post'.$cat->term_id.'_query');
}
}
}
So the final code looks something like this:
<?php
/**
* My widget class
*/
class dw_myWidget extends WP_Widget {
function __construct() {
// Instantiate the parent object
parent::__construct( false, __('My widget') );
add_action('save_post', array( $this, 'delete_query_caches') );
}
/**
* Delete transients
*/
function delete_query_caches( $post_id ){
if( !isset( $_POST['post_type'] ) || $_POST['post_type'] !== 'post' ) return;
$categories = wp_get_post_terms( $post_id, 'category' );
if( $categories && ! is_WP_Error( $categories ) ) {
foreach( $categories as $cat ) {
delete_transient('post'.$cat->term_id.'_query');
}
}
}
function widget( $args, $instance ) {
$title = apply_filters( 'title', $instance['title'] );
$link1 = apply_filters( 'category', $instance['category'] ); // category id
if ( false === ( $portfolio = get_transient( 'post'.$link1.'_query' ) ) ) {
$portfolio = new WP_Query(array(
'post_status' =>'publish',
'post_type' =>'post',
'cat' =>''.$link1.'',
'posts_per_page' =>'9'
));
set_transient( 'post'.$link1.'_query', $portfolio, 168 * HOUR_IN_SECONDS );
}
// And the rest of the code
}
function update( $new_instance, $old_instance ) {
// update stuff
}
function form( $instance ) {
// widget form stuff
}
}
function dw_MyWidget_register() {
register_widget( 'dw_myWidget' );
}
add_action( 'widgets_init', 'dw_MyWidget_register' );
Hope it'll help.
From the code point of view, it does not matter where the code resides. Your code is executed in the same way whether it's a widget or a template part, or a plugin function hooked to some action.
What does matter is what the code is actually doing and how (and again, it does not matter where the code is).
In your case, I suppose, the widget is getting some RSS feeds, thus making http requests to external resources. These requests are slow, the results must be cached to avoid repeating the requests on every page load. You can have different expiration time (even 30 seconds difference) for each instance of the widget to try to prevent them all updating at the same time. Or even better, you should update them in the background, never making a front-end visitor to fetch those feeds.
Transients API is what you should use to make it fast.
Im trying to override the text widget in WordPress. My code looks like this:
class WP_Widget_Text_Custom extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML'));
$control_ops = array('width' => 400, 'height' => 350);
parent::__construct('text', __('Text'), $widget_ops, $control_ops);
}
function WP_Widget_Calendar( $args, $instance ) {
extract($args);
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
$text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
echo $before_widget;
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
<div class="textwidget test"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
<?php
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
if ( current_user_can('unfiltered_html') )
$instance['text'] = $new_instance['text'];
else
$instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed
$instance['filter'] = isset($new_instance['filter']);
return $instance;
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
$title = strip_tags($instance['title']);
$text = esc_textarea($instance['text']);
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
<textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
<p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> /> <label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<?php
}
}
function custom_register_widgets() {
register_widget( 'WP_Widget_Text_Custom' );
}
add_action( 'widgets_init', 'custom_register_widgets' );
Im getting the following error message:
*function WP_Widget::widget() must be over-ridden in a sub-class.*
I have copied the WP_Widget_Text from default-widgets.php to functions.php and added _Custom in class name.
Why am I getting this error and how do I fix it?
You might try changing WP_Widget_Calendar to be widget.
I've developed a local site with wordpress and the theme Lugada.
All is OK on local. I've transfered all data on ovh.
When I want to see what is done I get the error:
Parse error: syntax error, unexpected T_STRING in
/homez.705/cadeauxd/www/wp-content/themes/lugada/include/widget.php on
line 1
When I add empty rows at the beginning it's the same thing: on line 1
Here's the file :
<?php
class RecentPost_Widget extends WP_Widget {
/* Register widget with WordPress. */
public function __construct() {
parent::__construct(
'recentpost_widget', // Base ID
'(Lugada) Recent Post with Thumbnail', // Name
array( 'description' => __( 'lugada recent post with post-thumbnail support widget.', 'lugada' ), ) // Args
);
}
/* Front-end display of widget. */
public function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
$rcnumber = $instance['rcnumber'] ;
echo $before_widget;
if ( ! empty( $title ) )
echo $before_title . $title . $after_title;
?>
<?php
echo '<ul>';
echo lugada_display_recent_posts($rcnumber);
echo '</ul>';?>
<?php
echo $after_widget;
}
/* Sanitize widget form values as they are saved. */
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['rcnumber'] = strip_tags( $new_instance['rcnumber'] );
return $instance;
}
/* Back-end widget form. */
public function form( $instance ) {
if ( $instance ) {
$title = esc_attr( $instance[ 'title' ] );
$rcnumber = esc_attr( $instance[ 'rcnumber' ] );
}
else {
$title = __( 'Recent post', 'lugada' );
$rcnumber = __( '5', 'lugada' );
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:','lugada' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'rcnumber' ); ?>"><?php _e( 'Number of recent post to show:','lugada' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'rcnumber' ); ?>" name="<?php echo $this->get_field_name( 'rcnumber' ); ?>" type="text" value="<?php echo $rcnumber; ?>" />
</p>
<?php
}
} // class RecentPost_Widget
class RandomPost_Widget extends WP_Widget {
/* Register widget with WordPress. */
public function __construct() {
parent::__construct(
'randompost_widget', // Base ID
'(Lugada) Random Post', // Name
array( 'description' => __( 'lugada random post widget.', 'lugada' ), ) // Args
);
}
/* Front-end display of widget. */
public function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
$rndnumber = $instance['rndnumber'] ;
echo $before_widget;
if ( ! empty( $title ) )
echo $before_title . $title . $after_title;
?>
<?php
echo '<ul>';
echo lugada_display_random_posts($rndnumber);
echo '</ul>';?>
<?php
echo $after_widget;
}
/* Sanitize widget form values as they are saved. */
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['rndnumber'] = strip_tags( $new_instance['rndnumber'] );
return $instance;
}
/* Back-end widget form. */
public function form( $instance ) {
if ( $instance ) {
$title = esc_attr( $instance[ 'title' ] );
$rndnumber = esc_attr( $instance[ 'rndnumber' ] );
}
else {
$title = __( 'Random post', 'lugada' );
$rndnumber = __( '5', 'lugada' );
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:','lugada' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'rndnumber' ); ?>"><?php _e( 'Number of random post to show:','lugada' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'rndnumber' ); ?>" name="<?php echo $this->get_field_name( 'rndnumber' ); ?>" type="text" value="<?php echo $rndnumber; ?>" />
</p>
<?php
}
} // class RandomPost_Widget
?>
That error usually means that you've got an unclosed string somewhere; the stated line number is usually not meaningful. Also check for things like unpaired parentheses or brackets, and places where you think you're writing in either HTML or PHP but you're actually writing in the other one.
I wrote my first custom wordpress plugin. It is basically a copy of the default Recent Posts plugin (that comes out of the box), and then I am adding a filter to only get a certain post category. Originally, I just hardcoded this, but I figured I would just add a widget option that the user can change. This required adding an additional field in 'function form()'. I basically just copied and pasted the input field for the "title" text box (and then added the appropriate code for the new field - again copying and pastying from title). After I did this, the field showed up just fine, but I could the option would not save when clicking Save (the field just wipes out each time). Basically, the field is not posting properly (or something along those lines). My first question is, where are these fields stored? Also, am I supposed to be registering the field somewhere?
The code is below... The Category field is what I am trying to add. Please advise. Thanks.
<?php
/**
* Recent_Posts widget class
*
* #since 2.8.0
*/
class custom_RecentPostsByCategory extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your site (by Category)") );
parent::__construct('recent-posts', __('Custom: Recent Posts'), $widget_ops);
$this->alt_option_name = 'widget_recent_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) {
$cache = wp_cache_get('widget_recent_posts', 'widget');
if ( !is_array($cache) )
$cache = array();
if ( ! isset( $args['widget_id'] ) )
$args['widget_id'] = $this->id;
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
ob_start();
extract($args);
$title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title'], $instance, $this->id_base);
if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
$number = 10;
$r = new WP_Query(array('posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'category_name' => $instance['cat']));
if ($r->have_posts()) :
?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<ul class="twitter-list">
<?php while ($r->have_posts()) : $r->the_post(); ?>
<li class="twitter-item">
<?php if ( get_the_title() ) the_title(); else the_ID(); ?><br/>
<?php the_time("F j, Y"); ?>
</li>
<!--<li><?php if ( get_the_title() ) the_title(); else the_ID(); ?></li>-->
<?php endwhile; ?>
</ul>
<?php echo $after_widget; ?>
<?php
// Reset the global $the_post as this query will have stomped on it
wp_reset_postdata();
endif;
$cache[$args['widget_id']] = ob_get_flush();
wp_cache_set('widget_recent_posts', $cache, 'widget');
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['cat'] = strip_tags($new_instance['cat']);
$instance['number'] = (int) $new_instance['number'];
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['widget_recent_entries']) )
delete_option('widget_recent_entries');
return $instance;
}
function flush_widget_cache() {
wp_cache_delete('widget_recent_posts', 'widget');
}
function form( $instance ) {
$title = isset($instance['title']) ? esc_attr($instance['title']) : '';
$cat = isset($instance['cat']) ? esc_attr($instance['cat']) : '';
$number = isset($instance['number']) ? absint($instance['number']) : 5;
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p><label for="<?php echo $this->get_field_id('cat'); ?>"><?php _e('Category:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('cat'); ?>" name="<?php echo $this->get_field_name('cat'); ?>" type="text" value="<?php echo $cat; ?>" /></p>
<p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label>
<input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<?php
}
}
function wpzoom_register_rpa_widget() {
register_widget('custom_RecentPostsByCategory');
}
add_action('widgets_init', 'wpzoom_register_rpa_widget');
?>
You have to use unique id base for Widgets.
Change
parent::__construct('recent-posts', __('Custom: Recent Posts'), $widget_ops);
To
parent::__construct('recent-posts-custom', __('Custom: Recent Posts'), $widget_ops);