I have a very strange issue with the wordpress widget. I am editing an existed wordpress widget form Phomedia theme, and I am trying to add one more instance so I can use it to filter posts. In the code it is $instance['custom']. But everytime I hit save the value is dissapearing. I got really stuck with it for several, trying to figure out what is going wrong, but couldn't find it. Here is the code
class MZ_Widget_Latest_Products extends WP_Widget {
/**
* Widget Constuctor
*/
function MZ_Widget_Latest_Products() {
$widget_ops = array( 'classname' => 'widget_mz_wpsc_latest_products','description' => __( 'Phomedia Latest Products Widget', 'wpsc' ) );
$this->WP_Widget( 'wpsc_latest_products', __( 'Phomedia Latest Products', 'wpsc' ), $widget_ops );
}
/**
* Widget Output
*
* #param $args (array)
* #param $instance (array) Widget values.
*/
function widget( $args, $instance ) {
global $wpdb, $table_prefix;
extract( $args );
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Latest Products', 'wpsc' ) : $instance['title'] );
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
wpsc_phomedia_latest_product( $args, $instance );
echo $after_widget;
}
/**
* Update Widget
*
* #param $new_instance (array) New widget values.
* #param $old_instance (array) Old widget values.
*
* #return (array) New values.
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['number'] = (int)$new_instance['number'];
$instance['image'] = (bool)$new_instance['image'];
$instance['height'] = (int)$new_instance['height'];
$instance['width'] = (int)$new_instance['width'];
$instance['custom'] = $instance['custom'];
return $instance;
}
/**
* Widget Options Form
*
* #param $instance (array) Widget values.
*/
function form( $instance ) {
global $wpdb;
// Defaults
$instance = wp_parse_args( (array)$instance, array(
'title' => '',
'number' => 4,
'width' => 173,
'height' => 151
) );
// Values
$title = esc_attr( $instance['title'] );
$number = (int)$instance['number'];
$image = (bool)$instance['image'];
$width = (int) $instance['width'];
$height = (int) $instance['height'];
$custom = $instance['custom']; ?>
<p><label for="<?php echo $this->get_field_id('custom'); ?>">Custom: <input class="widefat" id="<?php echo $this->get_field_id('custom'); ?>" name="<?php echo $this->get_field_name('custom'); ?>" type="text" value="<?php echo attribute_escape($custom); ?>" /></label></p>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', 'wpsc' ); ?></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 products to show:', 'wpsc' ); ?></label>
<input type="text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $number; ?>" size="3" />
</p>
<p>
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'image' ); ?>" name="<?php echo $this->get_field_name( 'image' ); ?>"<?php checked($image); ?> onclick="jQuery('.wpsc_latest_image').toggle()">
<label for="<?php echo $this->get_field_id( 'image' ); ?>"><?php _e( 'Show Thumbnails', 'wpsc' ); ?></label>
</p>
<div class="wpsc_latest_image"<?php if( !checked( $image ) ) { echo ' style="display:none;"'; } ?>>
<p>
<label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Width:', 'wpsc'); ?></label>
<input type="text" id="<?php echo $this->get_field_id('width'); ?>" name="<?php echo $this->get_field_name('width'); ?>" value="<?php echo $width ; ?>" size="3" />
<label for="<?php echo $this->get_field_id('height'); ?>"><?php _e('Height:', 'wpsc'); ?></label>
<input type="text" id="<?php echo $this->get_field_id('height'); ?>" name="<?php echo $this->get_field_name('height'); ?>" value="<?php echo $height ; ?>" size="3" />
</p>
</div>
<?php
}
}
add_action( 'widgets_init', create_function( '', 'return register_widget("MZ_Widget_Latest_Products");' ) );
function wpsc_phomedia_latest_product( $args = null, $instance ) {
global $wpdb;
$args = wp_parse_args( (array)$args, array( 'number' => 3 ) );
$siteurl = get_option( 'siteurl' );
$options = get_option( 'wpsc-widget_latest_products' );
$number = isset($instance['number']) ? (int)$instance['number'] : 3;
$image = isset($instance['image']) ? (bool)$instance['image'] : FALSE;
if ( isset($instance['width'] ) )
$width = $instance['width'];
if ( isset( $instance['height'] ) )
$height = $instance['height'];
$latest_products = get_posts( array(
'post_type' => 'wpsc-product',
'numberposts' => $number,
'orderby' => 'post_date',
'post_parent' => 0,
'post_status' => 'publish',
'order' => 'ASC'
) );
$output = '';
$total = count( $latest_products );
$i = 0;
if ( count( $latest_products ) > 0 ) {
$output .= '<div class="grid no-border" ><ul>';
foreach ( $latest_products as $latest_product ) {
//print_r($latest_product);
$i++;
if ( !($i%3)) $class = 'last'; else $class = '';
$output .= '<li class="item '.$class.' ">';
// Thumbnails, if required
if ($image) {
$output .= '<a class="product-image" href="' . wpsc_product_url( $latest_product->ID, null ) . '">';
$attached_images = (array)get_posts( array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $latest_product->ID,
'orderby' => 'menu_order',
'order' => 'ASC'
) );
$attached_image = $attached_images[0];
if ( $attached_image->ID > 0 )
$output .= '<img class="product_image" src="' . wpsc_product_image( $attached_image->ID, $width, $height ) . '" title="' . $latest_product->post_title . '" alt="' . $latest_product->post_title . '" />';
else
$output .='<img class="no-image product_image" id="product_image_'.wpsc_the_product_id().'" alt="No Image" title="'.wpsc_the_product_title().'" src="'.WPSC_URL.'/wpsc-theme/images/noimage.png" width="' . $width . '" height="' . $height . '" />';
$output .= '</a>';
}
// Link
$output .= '<h3 class="product-name">'.stripslashes( $latest_product->post_title ).'</h3>';
$output .= '<p class="product-desc">'. $latest_product->post_content.'</p>';
$output .= '<div class="action">';
$output .= '<div class="price-box left">';
//$output .= $latest_product->ID;
//$output .= wpsc_calculate_price( wpsc_the_product_id(),null,false );
$output .= wpsc_currency_display(get_post_meta( $latest_product->ID, '_wpsc_price', true ));
$output.= '</div>';
$output .= '<a class="button right" href="' . wpsc_product_url( $latest_product->ID, null ) . '"><span>'.__( 'Details', 'wpsc' ).'</span></a>';
$output .= '<div class="clear"></div></div>';
$output .= '</li>';
}
$output .= "</ul></div><div class=\"clear\"></div>";
}
echo $output;
}
UPDATE: Fixed this problem. It didn't save because of wrong class name as a parameter.
$widget_ops = array( 'classname' => 'widget_mz_wpsc_latest_products','description' => __( 'Phomedia Latest Products Widget', 'wpsc' ) );
$this->WP_Widget( 'wpsc_latest_products', __( 'Phomedia Latest Products', 'wpsc' ), $widget_ops );
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');
After I drag my custom widget into a widget area and reload the page it's gone. Any ideas?
<?php
add_action('widgets_init','register_postTypeWidget');
function register_postTypeWidget(){
register_widget('postType_widget');
}
class postType_widget extends WP_Widget {
function postType_widget() {
$widget_ops = array( 'classname' => 'postType_widget', 'description' => __('Display Posts by Post Type') );
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'postType_widget' );
$this->WP_Widget( 'postType_widget', __('Posts by Post Type', 'postType_widget'), $widget_ops, $control_ops );
}
function widget($args, $instance) {
extract($args);
echo "hello world";
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['postTypeSelect'] = strip_tags( $new_instance['postTypeSelect'] );
$instance['postTypeNumber'] = strip_tags( $new_instance['postTypeNumber'] );
$instance['selectedPostTag'] = strip_tags( $new_instance['selectedPostTag'] );
$instance['showDate'] = strip_tags( $new_instance['showDate'] );
$instance['showAuthor'] = strip_tags( $new_instance['showAuthor'] );
return $instance;
}
function form( $instance ) {
$defaults = array( 'title' => __(''), 'postTypeSelect' => __(''), 'postType_number' => __(''), 'selected_postTag' => __(''), 'show_date' => __(''), 'show_author' => __('') );
$instance = wp_parse_args( (array) $instance, $defaults);
$args = '';
$output = 'object';
$post_types = get_post_types( $args, $output );
$posttags = get_tags();
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title'); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'postTypeSelect' ); ?>"><?php _e('Select Post Type'); ?></label>
<?php
echo '<select name="postTypeSelect" style="width:100%;">';
echo '<option value="">Select Post Type</option>';
foreach($post_types as $post_type) {
if($instance['postTypeSelect'] == $post_type->name)
echo '<option value="'.$post_type->name.'" selected >'.$post_type->label.'</option>';
else
echo '<option value="'.$post_type->name.'" >'.$post_type->label.'</option>';
}
echo '</select>';
?>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'postTypeNumber' ); ?>"><?php _e('Number of Posts'); ?></label>
<?php
echo '<select name="postTypeNumber" style="width:100%;">';
echo '<option value="">Number of Posts</option>';
for($x=1; $x < 11; $x++) {
if($instance['postTypeNumber'] == $x)
echo '<option value="'.$x.'" selected >'.$x.'</option>';
else
echo '<option value="'.$x.'" >'.$x.'</option>';
}
echo '</select>';
?>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'selectedPostTag' ); ?>" style="display:block;width:100%;"><?php _e('Show Only Posts with These Tags (optional)'); ?></label>
<?php
foreach($posttags as $postTag){
if($instance['selected_postTag'] == $postTag->term_id)
echo '<input type="checkbox" name="selectedPostTag[]" value="'.$postTag->term_id.'" checked />'.$postTag->name.'</br>';
else
echo '<input type="checkbox" name="selectedPostTag[]" value="'.$postTag->term_id.'" />'.$postTag->name.'</br>';
}
?>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'additional_options' ); ?>" style="display:block;width:100%;"><?php _e('Additional Options'); ?></label>
<?php if($instance['show_date'] == 'show_date'): ?>
<input type="checkbox" id="<?php echo $this->get_field_id( 'showDate' ); ?>" name="<?php echo $this->get_field_name( 'showDate' ); ?>" value="showDate" checked />Show Date</br>
<?php else: ?>
<input type="checkbox" id="<?php echo $this->get_field_id( 'showDate' ); ?>" name="<?php echo $this->get_field_name( 'showDate' ); ?>" value="showDate" />Show Date</br>
<?php endif; ?>
<?php if($instance['show_author'] == 'show_author'): ?>
<input type="checkbox" id="<?php echo $this->get_field_id( 'showAuthor' ); ?>" name="<?php echo $this->get_field_name( 'showAuthor' ); ?>" value="showAuthor" checked />Show Author</br>
<?php else: ?>
<input type="checkbox" id="<?php echo $this->get_field_id( 'showAuthor' ); ?>" name="<?php echo $this->get_field_name( 'showAuthor' ); ?>" value="showAuthor" />Show Author</br>
<?php endif; ?>
</p>
<?php
}
}
?>
I think your widget's id_base value has to be lowercase. Looking at the source for WP_Widget:
/**
* PHP5 constructor
*
* #param string $id_base Optional Base ID for the widget, lower case,
* if left empty a portion of the widget's class name will be used. Has to be unique.
<snip>
*/
function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
So something like this should work in your constructor (note I also changed the case of the $id_base in $control_ops):
function postType_widget() {
$widget_ops = array( 'classname' => 'postType_widget', 'description' => __('Display Posts by Post Type') );
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'posttype_widget' );
$this->WP_Widget( 'posttype_widget', __('Posts by Post Type', 'postType_widget'), $widget_ops, $control_ops );
}
I have a wordpress widget where im saving the options, Title + Copy + URL
The url is generated by listing the wordpress pages
The below code saves and works fine on the front end. But in the widget settings panel when you save it reverts back to select page, i need it to display the previously selected item that it saved.
// Sidebar CTA Widget
class SidebarCTAWidget extends WP_Widget {
function SidebarCTAWidget() {
$widget_ops = array('classname' => 'SidebarCTAWidget', 'description' => 'Editable Sidebar CTAs' );
$this->WP_Widget('SidebarCTAWidget', 'Sidebar Call to Action', $widget_ops);
}
function form($instance) {
$defaults = array( 'title' => '', 'copy' => '', 'url' => '');
$instance = wp_parse_args( (array) $instance, $defaults );
$title = $instance['title'];
$copy = $instance['copy'];
$url = $instance['url'];
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Title:
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('copy'); ?>">Copy:
<textarea rows="5" class="widefat" name="<?php echo $this->get_field_name( 'copy' ); ?>" / ><?php echo esc_attr( $copy ); ?></textarea>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('url'); ?>">URL:
<select class="widefat" id="<?php echo $this->get_field_id('url'); ?>" name="<?php echo $this->get_field_name('url'); ?>" name="page-dropdown">
<option value=""> <?php echo esc_attr( __( 'Select Page' ) ); ?></option>
<?php
$pages = get_pages();
foreach ( $pages as $page ) {
$option = '<option value="' . get_page_link( $page->ID ) . '">';
$option .= $page->post_title;
$option .= '</option>';
echo $option;
}
?>
</select>
</label>
</p>
<?php }
//save the widget settings
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['copy'] = strip_tags( $new_instance['copy'] );
$instance['url'] = strip_tags( $new_instance['url'] );
return $instance;
}
function widget($args, $instance){
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
$copy = empty($instance['copy']) ? ' ' : apply_filters('widget_title', $instance['copy']);
$url = empty($instance['url']) ? ' ' : apply_filters('widget_title', $instance['url']);
if (!empty($title))
echo '<div class="clearfix sidebar-content"><h3>' . $title . '</h3>';
;
// This is the HTML
echo '<p>' . $copy . '</p></div>';
echo '<p class="sidebar-link">Find Out More</p>';
// END
echo $after_widget;
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("SidebarCTAWidget");') );
Thanks to the help of #b__ i managed to work out how to compare the saved url in the database and mark it as selected
$option = '<option '. (get_page_link( $page->ID ) == attribute_escape($url) ? "selected='selected'":"").'value="' . get_page_link( $page->ID ) . '">';
I'm trying to show the Taxonomy description for Wordpress Woothemes widget "Brands Thumbnail".
The code here returns a random term (i.e. 1 random brand) but I would also like it to include that random brands description along side it. Preferably wrapped in a div.
/** Variables to setup the widget. */
var $woo_widget_cssclass;
var $woo_widget_description;
var $woo_widget_idbase;
var $woo_widget_name;
/** constructor */
function __construct() {
/* Widget variable settings. */
$this->woo_widget_name = __('WooCommerce Brand Thumbnails MJ', 'wc_brands' );
$this->woo_widget_description = __( 'Show a RANDOM Brand (Supplier) Thumbnail', 'wc_brands' );
$this->woo_widget_idbase = 'wc_brands_brand_thumbnails';
$this->woo_widget_cssclass = 'widget_brand_thumbnails';
/* Widget settings. */
$widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description );
/* Create the widget. */
$this->WP_Widget( $this->woo_widget_idbase, $this->woo_widget_name, $widget_ops );
}
/** #see WP_Widget */
function widget( $args, $instance ) {
extract( $args );
$exclude = array_map( 'intval', explode( ',', $instance['exclude'] ) );
$order = $instance['orderby'] == 'name' ? 'asc' : 'desc';
/**MJ added args and else function MJ*/
$args = array( 'hide_empty' => $instance['hide_empty'], 'orderby' => $instance['orderby'], 'exclude' => $exclude, 'number' => $instance['number'], 'order' => $order );
if ($instance['orderby'] == 'random') {
$args['orderby'] = 'none';
$brands = get_terms( 'product_brand', $args );
shuffle($brands);
$brands = array_slice($brands, 0, 1);
}
else {
$brands = get_terms( 'product_brand', $args );
}
/**MJ added args and else function MJ*/
if ( ! $brands )
return;
echo $before_widget;
if ( ! empty( $instance['title'] ) )
echo $before_title . $instance['title'] . $after_title;
woocommerce_get_template( 'widgets/brand-thumbnailsMJ.php', array(
'brands' => $brands,
'columns' => $instance['columns']
), 'woocommerce-brands', untrailingslashit( plugin_dir_path( dirname( dirname( __FILE__ ) ) ) ) . '/templates/' );
echo $after_widget;
}
/** #see WP_Widget->update */
function update( $new_instance, $old_instance ) {
$instance['title'] = strip_tags( stripslashes( $new_instance['title'] ) );
$instance['columns'] = strip_tags( stripslashes( $new_instance['columns'] ) );
$instance['orderby'] = strip_tags( stripslashes( $new_instance['orderby'] ) );
$instance['exclude'] = strip_tags( stripslashes( $new_instance['exclude'] ) );
$instance['hide_empty'] = strip_tags( stripslashes( $new_instance['hide_empty'] ) );
$instance['number'] = strip_tags( stripslashes( $new_instance['number'] ) );
if ( ! $instance['columns'] )
$instance['columns'] = 1;
if ( ! $instance['orderby'] )
$instance['orderby'] = 'name';
if ( ! $instance['exclude'] )
$instance['exclude'] = '';
if ( ! $instance['hide_empty'] )
$instance['hide_empty'] = 0;
if ( ! $instance['number'] )
$instance['number'] = '';
return $instance;
}
/** #see WP_Widget->form */
function form( $instance ) {
if ( ! isset( $instance['hide_empty'] ) )
$instance['hide_empty'] = 0;
if ( ! isset( $instance['orderby'] ) )
$instance['orderby'] = 'name';
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'wc_brands') ?></label>
<input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php if ( isset ( $instance['title'] ) ) echo esc_attr( $instance['title'] ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'columns' ); ?>"><?php _e('Columns:', 'wc_brands') ?></label>
<input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'columns' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'columns' ) ); ?>" value="<?php if ( isset ( $instance['columns'] ) ) echo esc_attr( $instance['columns'] ); else echo '1'; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e('Number:', 'wc_brands') ?></label>
<input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" value="<?php if ( isset ( $instance['number'] ) ) echo esc_attr( $instance['number'] ); ?>" placeholder="<?php _e('All', 'wc_brands'); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'exclude' ); ?>"><?php _e('Exclude:', 'wc_brands') ?></label>
<input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'exclude' ) ); ?>" value="<?php if ( isset ( $instance['exclude'] ) ) echo esc_attr( $instance['exclude'] ); ?>" placeholder="<?php _e('None', 'wc_brands'); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'hide_empty' ); ?>"><?php _e('Hide empty brands:', 'wc_brands') ?></label>
<select id="<?php echo esc_attr( $this->get_field_id( 'hide_empty' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hide_empty' ) ); ?>">
<option value="1" <?php selected( $instance['hide_empty'], 1 ) ?>><?php _e('Yes', 'wc_brands') ?></option>
<option value="0" <?php selected( $instance['hide_empty'], 0 ) ?>><?php _e('No', 'wc_brands') ?></option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><?php _e('Order by:', 'wc_brands') ?></label>
<select id="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>">
<option value="name" <?php selected( $instance['orderby'], 'name' ) ?>><?php _e('Name', 'wc_brands') ?></option>
<option value="count" <?php selected( $instance['orderby'], 'count' ) ?>><?php _e('Count', 'wc_brands') ?></option>
<option value="random" <?php selected( $instance['orderby'], 'random' ) ?>><?php _e('Random', 'wc_brands') ?></option>
</select>
</p>
<?php
}
}
I fixed it. So for anyone else who stumbles upon a similar problem I used
<?php echo $brand->description; ?>
I also needed to truncate it, to which I used this function in function.php
function limit_words($string, $word_limit) {
$newstring = substr($string, 0, $word_limit);
if(strlen($newstring) < strlen($string))
$newstring = $newstring."...";
echo "<p>".$newstring."</p>";
}
and this modified description call (instead of the description call above)
<?php echo limit_words($brand->description,100); ?>
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.