i created my own widget code by php programe for wordpress...and i want to add that widget into widget panel..so if i want to show on that widget in my widget panel where i add(means which file in wordpress) my widget code...?
<?php
add_action( 'widgets_init', 'my_widget' );
function my_widget() {
register_widget( 'MY_Widget' );
}
class MY_Widget extends WP_Widget {
function MY_Widget() {
$widget_ops = array( 'classname' => 'example', 'description' => __('A widget that displays the authors name ', 'example') );
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'example-widget' );
$this->WP_Widget( 'example-widget', __('Example Widget', 'example'), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args );
//Our variables from the widget settings.
$title = apply_filters('widget_title', $instance['title'] );
$name = $instance['name'];
$show_info = isset( $instance['show_info'] ) ? $instance['show_info'] : false;
echo $before_widget;
// Display the widget title
if ( $title )
echo $before_title . $title . $after_title;
//Display the name
if ( $name )
printf( '<p>' . __('Hey their Sailor! My name is %1$s.', 'example') . '</p>', $name );
if ( $show_info )
printf( $name );
echo $after_widget;
}
//Update the widget
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
//Strip tags from title and name to remove HTML
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['name'] = strip_tags( $new_instance['name'] );
$instance['show_info'] = $new_instance['show_info'];
return $instance;
}
function form( $instance ) {
//Set up some default widget settings.
$defaults = array( 'title' => __('Example', 'example'), 'name' => __('Bilal Shaheen', 'example'), 'show_info' => true );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
//Widget Title: Text Input.
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'example'); ?></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>
//Text Input.
<p>
<label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e('Your Name:', 'example'); ?></label>
<input id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" value="<?php echo $instance['name']; ?>" style="width:100%;" />
</p>
//Checkbox.
<p>
<input class="checkbox" type="checkbox" <?php checked( $instance['show_info'], true ); ?> id="<?php echo $this->get_field_id( 'show_info' ); ?>" name="<?php echo $this->get_field_name( 'show_info' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_info' ); ?>"><?php _e('Display info publicly?', 'example'); ?></label>
</p>
<?php
}
}
?>
You can keep it in your theme's functions.php file but it would be better if you keep it in a separate file and then include the file in functions.php like
include('mywidget.php');
Create the file (mywidget.php) and save it in your themes root folder (where functions.php is located), and paste the code in this file, that's it. Keeping the widget code in a different file will keep your functions.php clean and your widget file will be easily readable/editable in it's own file without looking the code in functions.php.
Related
I created widget to add [woocommerce_cart] to my archive product sidebar
Number of product per row is set to 3.
After submit add to cart the number of product per row change to 2 !
Any guidance on that?
Here is my archive product link:
Here is the widget code:
// Creating the widget basket
class wpb_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_widget',
// Widget name will appear in UI
__('ابزارک سبد خرید', 'wpb_widget_domain'),
// Widget description
array( 'description' => __( 'سبد خرید اختصاصی ', 'wpb_widget_domain' ), )
);
}
// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before widget arguments are defined by themes
echo $args['before_widget'];
// This is where you run the code and display the output
?>
<div class="header__card-icon" >
<a href="<?php echo wc_get_cart_url(); ?>"title="<?php is_rtl() ? _e( 'نمایش سبد خرید' ) : _e( 'View your shopping cart' ); ?>">
<span class="demo-icon icon-basket-1"> </span>
<?php // if ( ! empty( $title ) ) echo $args['before_title'] . $title . $args['after_title']; ?>
</a>
<a class="cart-contents" id="mini-cart-count" href="<?php echo wc_get_cart_url(); ?>" title="<?php is_rtl() ? _e( 'نمایش سبد خرید' ) : _e( 'View your shopping cart'); ?>" ><?php if(WC()->cart->get_cart_contents_count()>0){ echo sprintf( WC()->cart->get_cart_contents_count() );} ?> </a>
</div>
<!--header__card-icon-->
<h3 class="widget-title"> ســبــد خــریــد </h3>
<?php
echo do_shortcode('[woocommerce_cart]');
// after widget arguments are defined by themes
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( '', 'wpb_widget_domain' );
}
// Widget admin form
?>
<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>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // Class wpb_widget ends here
// Register and load the widget
function wpb_load_widget() {
register_widget( 'wpb_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );
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'm very new to coding Wordpress plugins/widgets (this is my first time!). I've built a very basic Wordpress plugin which consists of 2 text inputs and a select field. The text inputs work fine however the select box doesn't appear to be saving when I hit the "Save" button.
Here is my plugin code:
<?php
/* Plugin Name: Sidebar Box
Plugin URI: http://www.website.com
Description: Displays contact box in sidebar
Version: 1.0
Author: JM
Author URI: N/A
*/
// use widgets_init action hook to execute custom function
add_action( 'widgets_init', 'jm_box_widget' );
//register our widget
function jm_box_widget() {
register_widget( 'jm_box_widget_my_info' );
}
//boj_widget_my_info class
class jm_box_widget_my_info extends WP_Widget {
//process the new widget
function jm_box_widget_my_info() {
$widget_ops = array(
'classname' => 'jm_box_widget_class',
'description' => 'Sidebar Box Widget.'
);
$this->WP_Widget( 'jm_box_widget_my_info', 'Box Widget', $widget_ops );
}
//build the widget settings form
function form($instance) {
$defaults = array( 'title' => 'Box Page Widget', 'description' => '', 'boxtype' => '' );
$instance = wp_parse_args( (array) $instance, $defaults );
$title = $instance['title'];
$description = $instance['description'];
$boxtype = $instance['boxtype'];
?>
<p>Title: <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
<p>Description: <textarea class="widefat" name="<?php echo $this->get_field_name( 'description' ); ?>" / ><?php echo esc_attr( $description ); ?></textarea></p>
<p>Sex:
<select id="<?php echo $this->get_field_id( 'boxtype' ); ?>" name="<?php echo $this->get_field_name( 'boxtype' ); ?>" class="widefat" style="width:100%;">
<option <?php if ( 'box1' == $instance['format'] ) echo 'selected="selected"'; ?> value="box1">box1</option>
<option <?php if ( 'box2' == $instance['format'] ) echo 'selected="selected"'; ?> value="box2">box2</option>
</select>
</p>
<?php
}
//save the widget settings
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['description'] = strip_tags( $new_instance['description'] );
$instance['boxtype'] = ( $new_instance['boxtype'] );
return $instance;
}
//display the widget
function widget($args, $instance) {
extract($args);
echo $before_widget;
$title = apply_filters( 'widget_title', $instance['title'] );
$description = empty( $instance['description'] ) ? ' ' : $instance['description'];
$boxtype = empty( $instance['boxtype'] ) ? ' ' : $instance['boxtype'];
echo '<div class="sidebar-box" id="' . $boxtype . '" onmouseover="this.style.cursor=\'pointer\'" onmouseup="window.location=\'' . $boxtype . '\'">
<h3>' . $title . '</h3>
<p>' . $description . '</p>
</div>';
echo $after_widget;
}
}
?>
I can't for the life of me workout why it's not saving.
Any help would be greatly appreciated.
Thanks,
James
Somewhere along the line you must have changed $instance['format'] to $instance['boxtype'] but not in the form. The options need changing.
<option <?php if ('box1' == $boxtype )
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.