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 )
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' );
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 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.
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);