Wordpress custom widget will not save values - php

Hello I am pretty new to Wordpress and I am trying to make a custom widget. I can get the form to display and enter values however whenever I press save, the widget reverts back to it's default values. I have tried adding in the values in accessibility mode also, but to no avail.
Here is a copy of my widget class:
function loadsidewidget()
{
register_widget('WP_Widget_sidebar_textwidget');
}
add_action('widgets_init', 'loadsidewidget');
class WP_Widget_sidebar_textwidget extends WP_Widget
{
function __construct() {
parent::__construct(
'WP_Widget_sidebar_textwidget',
__('Sidebar Textwdiget','sidewidget_domain'),
array( 'description' => __( 'Sample widget' ,'sidewidget_domain'), )
);
}
public function widget($args, $instance)
{
extract( $args );
extract($instance);
$title = apply_filters('widget_title',$instance['title']);
$show_info = isset( $instance['textarea'] ) ? $instance['textarea'] :'';
echo $args['before_widget'];
if(!empty($title))
{
echo $args['before_title'];
echo '<i class="fa fa-align-left"></i>'.$title;
echo $args['after_title'];
}
echo '<p>'.$instance['textarea'].'</p>';
}
public function update($new_instance, $old_instance)
{
$instance =$old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['textarea'] = esc_textarea($new_instance['textarea']);
return $instance;
}
//backend
public function form($instance)
{
$instance = wp_parse_args(array($intsance),array('title'=>'Title','textarea'=>'default'));
if(isset($instance['title']))
{
$title = strip_tags($instance['title']);
}
else
{
$title = 'Title';
}
$titleId = $this->get_field_id( 'title' );
$titleName = $this->get_field_name( 'title' );
$text = $instance['textarea'];
$contentId = $this->get_field_id( 'textarea' );
$contentName = $this->get_field_name( 'textarea' );
?>
<p>
<label for="<?php echo $titleId; ?>"><?php echo __('Title','sidewidget_domain');?></label>
<input class="widefat" id="<?php echo $titleId;?>" name="<?php echo $titleName;?>" value="<?php echo $title;?>"/>
</p>
<p>
<label for="<?php echo $contentId?>"><?php echo __('Content','sidewidget_domain');?></label>
<textarea class="widefat" id="<?php echo $contentId;?>" name="<?php echo $contentName;?>"><?php echo $text;?></textarea>
</p>
<h1><?php echo $instance['textarea'];?></h1>
<?php
}
}
I have a feeling it's something to do with the base widget Id but I'm not sure.

I figured it out, the problem was this line $instance = wp_parse_args(array($intsance),array('title'=>'Title','textarea'=>'default'));
if(isset($instance['title']));, I removed it and it worked.

Related

is this correct to fix Use of deprecated PHP4 style class constructor is not supported since PHP 7?

no idea what i'm doing , but in reading some other similar questions , i wanted to know if this is correct before i make the change
I am getting the error
Use of deprecated PHP4 style class constructor is not supported since PHP 7
when i run a compatibility test on my WP Blog
It references these 2 lines
function boc_latest() {
$widget_ops = array('description' => 'Aqua Latest Posts');
$this->WP_Widget('boc_latest', 'Aqua Latest Posts', $widget_ops);
}
function contact_info_widget()
{
$widget_ops = array('classname' => 'contact_info', 'description' => '');
$this->WP_Widget('contact_info-widget', 'Aqua: Contact Info', $widget_ops);
}
Would i simply change the functions as such ?
function __construct() {
$widget_ops = array('description' => 'Aqua Latest Posts');
$this->WP_Widget('boc_latest', 'Aqua Latest Posts', $widget_ops);
}
function __construct()
{
$widget_ops = array('classname' => 'contact_info', 'description' => '');
$this->WP_Widget('contact_info-widget', 'Aqua: Contact Info', $widget_ops);
}
Here is full php file
function boc_load_widgets() {
register_widget('boc_latest');
register_widget('contact_info_widget');
}
class boc_latest extends WP_Widget {
function boc_latest() {
$widget_ops = array('description' => 'Aqua Latest Posts');
$this->WP_Widget('boc_latest', 'Aqua Latest Posts', $widget_ops);
}
function widget($args, $instance) {
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance['title']) ? ' ' : '<span>'.apply_filters('widget_title', $instance['title']).'</span>';
$count = $instance['count'];
echo removeSpanFromTitle($before_title) . $title . removeSpanFromTitle($after_title);
wp_reset_query();
rewind_posts();
$recent_posts = new WP_Query(
array(
'posts_per_page' => $count,
'post_status' => 'publish',
'nopaging' => 0,
'post__not_in' => get_option('sticky_posts')
)
);
// Cycle through Posts
if ($recent_posts->have_posts()) :while ($recent_posts->have_posts()) : $recent_posts->the_post();
?>
<div class="boc_latest_post clearfix">
<?php the_post_thumbnail('small-thumb'); ?>
<p class="boc_latest_post_title"><?php the_title(); ?></p>
<p class="date"><?php echo get_the_date();?></p>
</div>
<?php
endwhile;
endif;
wp_reset_query();
rewind_posts();
echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['count'] = $new_instance['count'];
return $instance;
}
function form($instance) {
$instance = wp_parse_args((array) $instance, array('title' => ''));
$title = strip_tags($instance['title']);
$count = $instance['count'];
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Widget 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 esc_attr($title); ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('count'); ?>">How many posts? (Number):
<input class="widefat" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo esc_attr($count); ?>" />
</label>
</p>
<?php
}
}
/**
* Contact Info Widget
*/
class contact_info_widget extends WP_Widget {
function contact_info_widget()
{
$widget_ops = array('classname' => 'contact_info', 'description' => '');
$this->WP_Widget('contact_info-widget', 'Aqua: Contact Info', $widget_ops);
}
function widget($args, $instance)
{
extract($args);
$title = apply_filters('widget_title', $instance['title']);
echo $before_widget;
if($title) {
echo $before_title.$title.$after_title;
}
?>
<?php if($instance['phone']): ?>
<div class="icon_phone"><?php echo $instance['phone']; ?></div>
<?php endif; ?>
<?php if($instance['email']): ?>
<div class="icon_mail"><?php echo $instance['email']; ?></div>
<?php endif; ?>
<?php if($instance['address']): ?>
<div class="icon_loc"><?php echo $instance['address']; ?></div>
<?php endif; ?>
<div class="clear h10"></div>
<?php
echo $after_widget;
}
function update($new_instance, $old_instance)
{
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
$instance['address'] = $new_instance['address'];
$instance['phone'] = $new_instance['phone'];
$instance['fax'] = $new_instance['fax'];
$instance['email'] = $new_instance['email'];
$instance['web'] = $new_instance['web'];
return $instance;
}
function form($instance)
{
$defaults = array('title' => 'Contact Info');
$instance = wp_parse_args((array) $instance, $defaults); ?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('phone'); ?>">Phone:</label>
<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('phone'); ?>" name="<?php echo $this->get_field_name('phone'); ?>" value="<?php echo $instance['phone']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('email'); ?>">Email:</label>
<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('email'); ?>" name="<?php echo $this->get_field_name('email'); ?>" value="<?php echo $instance['email']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('address'); ?>">Address:</label>
<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('address'); ?>" name="<?php echo $this->get_field_name('address'); ?>" value="<?php echo $instance['address']; ?>" />
</p>
<?php
}
}
class boc_latest extends WP_Widget {
function boc_latest(){
...
}
}
function boc_latest() Should be renamed function __construct() because the class name is the same as the method (function) name.
class boc_latest extends WP_Widget {
function __construct(){
...
}
}
The same applies universally; If a class contains a function with an identical name (and no __construct exists) then tha function/method should be renamed as a __construct() method for that class.
From the Manual
For backwards compatibility with PHP 3 and 4, if PHP cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class.
Old style constructors are DEPRECATED in PHP 7.0, and will be removed in a future version. You should always use __construct() in new code.

Class WP_Widget not found

This is from an unsupported WordPress plugin that I'm trying to revive. I'm getting an error in the following code which states
Fatal Error class WP_Widget not found on Line 7
It works with PHP 5.3.3 but not on 5.6 or any version of PHP 7. Any suggestions would be appreciated.
<?php
/**
*
* Widget Class
*/
class Twitter_Like_Box_Widget extends WP_Widget
{
var $_options;
function __construct( ) {
global $tlb;
$this->wpb_prefix = $tlb->get_domain();
$widget_ops = array( 'classname' => 'tlb_widget', 'description' => ' Display your Twitter followers along with a follow me button' ); // Widget Settings
$control_ops = array( 'id_base' => 'tlb_widget' ); // Widget Control Settings
parent::__construct( 'tlb_widget', 'Twitter Like Box', $widget_ops, $control_ops );
$this->_options = $tlb->getOptions();
}
//Function to init the widget values and call the display widget function
function widget($args,$instance)
{
$title = apply_filters('widget_title', $instance['title']); // the widget title
$username = $instance['username']; // the widget title
$total_number = $instance['total_number']; // the number of followers to show
$show_followers = $instance['show_followers']; // show followers or users i follow
$link_followers = $instance['link_followers']; // link followers to profile
$width = $instance['width']; // link followers to profile
$widget = array ( 'username' => $username ,'total' => $total_number , 'show_followers' => $show_followers ,'link_followers'=> $link_followers, 'width' => $width, 'options' => $this->_options);
echo $args['before_widget'];
if ( $title )
echo $args['before_title'] . $title . $args['after_title'];
$this->display_widget($widget);
echo $args['after_widget'];
}
//function that display the widget form
function form($instance)
{
global $tlb;
$defaults = array( 'total_number' => 10, 'show_followers' => 'followers','link_followers'=> 'on','title' => 'My Followers', 'username' => 'chifliiiii');
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<?php if( $tlb->error != '' && defined('DOING_AJAX')):?>
<div class="error">
<p><?php echo sprintf(__('Check you OAuth settings, there is a problem with the connection.',$this->wpb_prefix),admin_url('options-general.php?page=twitter-like-box-reloaded'));?></p>
</div>
<?php endif;?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:',$this->wpb_prefix);?></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 $instance['title']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('username'); ?>"><?php _e('Username (without #):',$this->wpb_prefix);?></label>
<input class="widefat" id="<?php echo $this->get_field_id('username'); ?>" name="<?php echo $this->get_field_name('username'); ?>'" type="text" value="<?php echo $instance['username']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('show_thumbs'); ?>"><?php _e('Show Followers or people you follow?',$this->wpb_prefix);?></label>
</p>
<ul>
<li>
<input type="radio" class="radio" <?php checked( $instance['show_followers'], 'followers' ); ?> id="<?php echo $this->get_field_id('show_followers'); ?>" name="<?php echo $this->get_field_name('show_followers'); ?>" value="followers"/> <?php _e('Followers',$this->wpb_prefix);?>
</li>
<li>
<input type="radio" class="radio" <?php checked( $instance['show_followers'], 'nofollowers' ); ?> id="<?php echo $this->get_field_id('show_followers'); ?>" name="<?php echo $this->get_field_name('show_followers'); ?>" value="nofollowers" /> <?php _e('People I follow',$this->wpb_prefix);?>
</li>
</ul>
<p>
<label for="<?php echo $this->get_field_id('total_number'); ?>"><?php _e('How many you want to show?',$this->wpb_prefix); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('total_number'); ?>" name="<?php echo $this->get_field_name('total_number'); ?>" type="text" value="<?php echo $instance['total_number']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('link_followers'); ?>"><?php _e('Link followers to their profiles?',$this->wpb_prefix); ?></label>
<input type="checkbox" class="checkbox" <?php checked( $instance['link_followers'], 'on' ); ?> id="<?php echo $this->get_field_id('link_followers'); ?>" name="<?php echo $this->get_field_name('link_followers'); ?>" value="on" />
</p>
<p>
<label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Widget width:',$this->wpb_prefix); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('width'); ?>" name="<?php echo $this->get_field_name('width') ; ?>'" type="text" value="<?php echo isset($instance['width']) ? $instance['width'] : '100%'; ?>" />
</p>
<p>
<?php _e('More settings',$this->wpb_prefix);?>
</p>
<?php
}
//function that save the widget
function update($new_instance, $old_instance)
{
$instance['title'] = strip_tags($new_instance['title']);
$instance['username'] = strip_tags( $new_instance['username'] )== '' ? 'chifliiiii' : strip_tags( $new_instance['username'] );
$instance['total_number'] = strip_tags($new_instance['total_number']);
$instance['show_followers'] = $new_instance['show_followers'];
$instance['link_followers'] = $new_instance['link_followers'];
$instance['width'] = $new_instance['width'];
//Delete transient in case exist
$key = 'tlb_widgets_' . $instance['username'];
delete_transient($key);
return $instance;
}
//Finally thevfunction that create the widget
static function get_tlb_widget($widget)
{
global $tlb,$you;
$wpb_prefix = $tlb->get_domain();
$twitter = self::fetch_twitter_followers($widget);
ob_start();
if( !empty($you['error']) && '32' == $you['code']) {
echo $you['error'];
}
else
{
?>
<style type="text/css">
<?php echo $widget['options']['custom_css'];?>
</style>
<div id="tlb_container" style="width: <?php echo isset($widget['width']) ? $widget['width'] : 'auto';?>">
<?php if(isset($twitter['error']) ) :?>
<?php echo $twitter['error'];?>
<?php else : ?>
<div>
<div id="tlb_profile_img">
<a target="_blank" href="http://twitter.com/<?php echo $widget['username'];?>">
<img src="<?php echo $twitter['profile_image_url'];?>" width="44" height="44" align="left" alt="<?php echo $widget['username'];?>">
</a>
</div>
<div id="tlb_name">
<a target="_blank" href="http://twitter.com/<?php echo $widget['username'];?>">
<?php echo $widget['username'];?><span> <?php _e('on Twitter',$wpb_prefix);?></span>
</a>
</div>
<div id="tlb_follow">
<?php _e('Follow #',$wpb_prefix);?><?php echo $widget['username'];?>
</div>
</div><br>
<div style="padding:0; color:#637746;">
<div id="tlb_follow_total">
<?php
if ( $widget['show_followers'] == 'followers')
{
echo $twitter['followers_count'].' '.__('people follow',$wpb_prefix).' <strong>'. $widget['username'].'</strong>';
}
else
{
echo __('You follow ',$wpb_prefix). $twitter['friends_count'].__(' users',$wpb_prefix);
}
?>
</div>
<?php for($i=0; $i < $widget['total']; $i++) :?>
<span class="tlb_user_item">
<?php if($widget['link_followers'] == 'on' ): ?>
<a target="_blank" href="http://twitter.com/<?php echo $twitter['followers'][$i]['screen_name'];?>" title="<?php echo $twitter['followers'][$i]['screen_name'];?>" rel="nofollow">
<?php endif;?>
<img src="<?php echo $twitter['followers'][$i]['profile_image_url'];?>" width="48" height="48" alt="<?php echo $twitter['followers'][$i]['screen_name'];?>">
<span><?php echo substr($twitter['followers'][$i]['screen_name'], 0, 8);?></span>
<?php if($widget['link_followers'] == 'on' ): ?>
</a>
<?php endif;?>
</span>
<?php endfor;?>
<br style="clear:both">
</div>
<?php if ( $widget['options']['credits'] == 'true' ) echo '<div style="font-size:9px;text-align:right;">Widget By Timersys</div>';?>
<?php endif;//twitter error ?>
</div>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
</script>
<?php
}
$widget_code = ob_get_contents();
ob_end_clean();
return $widget_code;
}
/**
* Display widget
*/
static function display_widget($options){
echo Twitter_Like_Box_Widget::get_tlb_widget($options);
}
static function fetch_twitter_followers($options)
{
global $tlb,$you;
$cache_time = $tlb->_options['cache-time'];
$id = isset($options['id']) ? $options['id'] : 'widgets';
$key = 'tlb_'.$id.'_' . $options['username'];
// Let's see if we have a cached version
$followers = get_transient($key);
if ($followers !== false)
return $followers;
else
{
$tlb->connect();
$response = $tlb->connection->get("users/lookup", array('screen_name' => $options['username']));
if (Twitter_Like_Box_Widget::is_twitter_error($response))
{
// In case Twitter is down we return the last successful count
return get_option($key);
}
else
{
$json = $response;
#$you['name'] = $json[0]->name;
#$you['screen_name'] = $json[0]->screen_name;
#$you['followers_count'] = $json[0]->followers_count;
#$you['profile_image_url'] = $json[0]->profile_image_url;
#$you['friends_count'] = $json[0]->friends_count;
if ( $options['show_followers'] == 'followers' )
{
$fans = $tlb->connection->get('followers/ids',array('screen_name' => $options['username']));
}
else
{
$fans = $tlb->connection->get('friends/ids',array('screen_name' => $options['username']));
}
if (!Twitter_Like_Box_Widget::is_twitter_error($fans))
{
if ($options['total'] > 90 )
{
$fans_ids = array_chunk($fans->ids, 90);
$fans = array();
foreach ( $fans_ids as $ids_a )
{
$fans_ids = (string)implode( ',', $ids_a );
#$result = $tlb->connection->get('users/lookup',array('user_id' => $fans_ids ));
#$fans = array_merge($fans , $result );
}
}
else
{
$fans_ids = (string)implode( ',', array_slice($fans->ids, 0, $options['total']) );
#$fans = $tlb->connection->get('users/lookup',array('user_id' =>$fans_ids));
}
}
if( !Twitter_Like_Box_Widget::is_twitter_error($fans) && isset($fans[0]->screen_name) )
{
$followers = array();
for($i=0; $i < $options['total']; $i++)
{
$followers[$i]['screen_name'] = (string)$fans[$i]->screen_name;
$followers[$i]['profile_image_url'] = (string)$fans[$i]->profile_image_url;
}
$you['followers'] = $followers;
// Store the result in a transient, expires after 1 hour
// Also store it as the last successful using update_option
set_transient($key, $you, 60*60* $cache_time);
update_option($key, $you);
}
return $you;
}
}
}
static function is_twitter_error($response){
global $you,$tlb;
if(is_object($response) && isset($response->errors) )
{
$you['error'] = 'Error code: '. $response->errors[0]->code .'<br>Error message: '.$response->errors[0]->message;
$you['code'] = $response->errors[0]->code;
$tlb->log_error($you['error']);
return true;
}
if(is_object($response) && isset($response->ids) && empty($response->ids))
{
$you['error'] = '<br>Error message: You got no users to show. check if you have followers';
$tlb->log_error($you['error']);
return true;
}
return false;
}
} //end of class
Wordpress have ABSPATH variable defined in wp-config.php file, which contains the project's root path. Now we can include any file of wordpress codebase. eg.
require_once ABSPATH.'wp-includes/class-wp-widget.php';
I don't know the reason for the error, propably something with autoloading, but you can solve it if you include the file for the WP_Widget class in the first line.
require_once get_home_path()."wp-includes/class-wp-widget.php";

Receiving syntax error "Unexpected }" for a professional WordPress plugin, Sidebar Menu Widget

I'm receiving an "Unexpected }" syntax error for a commercially available WordPress plugin, Sidebar Menu Widget, and I can't seem to find out why such an error is being thrown. The widget consists of the following, with the "Unexpected }" syntax error happening at the closing brace of the function form($instance) within the class sidebar_menu_widget extends WP_Widget:
<? ob_start();
/*
Plugin Name: Sidebar Menu Widget
Plugin URI: http://webloungeinc.com
Description: With this plugin you can display menu in sidebar according to nev menu.
Author: Weblounge inc.
Version: 1.0
Author URI: http://webloungeinc.com
*/
define( 'PLUGINS_PATH', plugin_dir_url(__FILE__) );
wp_register_style( 'style', PLUGINS_PATH . 'style.css' );
wp_enqueue_style ( 'style' );
function _getSidebarMenu($menu_id='',$post)
{
$menuItems = wp_get_nav_menu_items($menu_id);
foreach($menuItems as $menuItem)
{
if($menuItem->object_id == $post->ID && $menuItem->object == $post->post_type)
{
$parentMenuId = $menuItem->menu_item_parent;
$currentMenuId = $menuItem->ID;
}
}
$menuItems = (array) $menuItems;
$_cnt = 0;
foreach($menuItems as $_counter)
{
if($currentMenuId==$_counter->menu_item_parent)
{
$_cnt++;
}
}
foreach($menuItems as $kmi=>$vmi)
{
$vmi = (array) $vmi;
if($currentMenuId <> "")
{
if(in_array($currentMenuId,$vmi))
{
if($currentMenuId == $vmi[ID])
{
if($_cnt <> 0)
{
echo $content = '<li class="m_title">' . $vmi[title] . '</li>';
}
else
{
foreach($menuItems as $sis_menu)
{
// Parent
$sis_menu = (array) $sis_menu;
if($sis_menu[ID] == $parentMenuId)
{
echo $content = '<li class="m_title">' . $sis_menu[title] . '</li>';
}
if($sis_menu[menu_item_parent] == $parentMenuId)
{
$_other_cls = implode(" ",$sis_menu[classes]);
if($sis_menu[ID] == $currentMenuId)
{
$_cur_class = 'current_m_item';
}
else
{
$_cur_class = "";
}
echo $content = '<li>» ' . $sis_menu[title] . '</li>';
}
}
}
}
else
{
$_other_cls = implode(" ",$vmi[classes]);
echo $content = '<li>» ' . $vmi[title] . '</li>';
}
}
}
else
{
if($vmi[menu_item_parent]==0)
{
$_other_cls = implode(" ",$vmi[classes]);
echo $content = '<li>» ' . $vmi[title] . '</li>';
}
}
}
}
/* Custom Product Specials Widget */
class sidebar_menu_widget extends WP_Widget {
function sidebar_menu_widget() {
// widget actual processes
parent::WP_Widget(false, $name = 'Sidebar Menu', array(
'description' => 'Displays a Sidebar Menu'
));
}
function widget($args, $instance) {
global $post;
extract($args);
echo $before_widget;
$instance['title'] = apply_filters('widget_title', $instance['title'], $instance, $this->id_base);
if ( !empty($instance['title']) )
echo $before_title . $instance['title'] . $after_title;
echo '<ul class="menu ' . $instance['class'] . '">';
_getSidebarMenu($instance['nav_menu'],$post);
echo '</ul>';
echo $after_widget;
}
function update($new_instance, $old_instance) {
return $new_instance;
}
function form($instance) {
$title = isset( $instance['title'] ) ? $instance['title'] : '';
$class = isset( $instance['class'] ) ? $instance['class'] : '';
$nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
// Get menus
$menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
// If no menus exists, direct the user to go and create some.
if ( !$menus ) {
echo '<p>'. sprintf( __('No menus have been created yet. Create some.'), admin_url('nav-menus.php') ) .'</p>';
return;
}
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('class'); ?>"><?php _e('Class:') ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('class'); ?>" name="<?php echo $this->get_field_name('class'); ?>" value="<?php echo $class; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('nav_menu'); ?>"><?php _e('Select Menu:'); ?></label>
<select id="<?php echo $this->get_field_id('nav_menu'); ?>" name="<?php echo $this->get_field_name('nav_menu'); ?>">
<?php
foreach ( $menus as $menu ) {
$selected = $nav_menu == $menu->term_id ? ' selected="selected"' : '';
echo '<option'. $selected .' value="'. $menu->term_id .'">'. $menu->name .'</option>';
}
?>
</select>
</p>
<?php
----> }
}
add_action('widgets_init', 'register_sidebar_menu_widget');
function register_sidebar_menu_widget() {
register_widget('sidebar_menu_widget');
}
I've indicated the spot of the error with an arrow. After extensive debugging, the only thing that I can think of that could cause such an error is the insertion of HTML in the form() function. However, I thought that ob_start() could allow such an interruption to take place while maintaining the flow of php via buffering the output? Do I need to make some sort of ob_get_contents() call after the HTML to allow such a continuation?
I'm about to rewrite the plugin in order to avoid the interruption, but it bothers me that this is a commercially available plugin that won't work for me straight out of the box. Any help would be greatly appreciated! Thanks.
I'm running PHP 5.5.12 by the way, and this error message is being given to me via XDebug. Please let me know if you need any other information if you're looking to help.

Where do I put my custom widget code for my wordpress theme?

I am creating a custom widget for my wordpress theme. The code I have breaks at a certain point.
I do not want to package the widget as a plugin so I am putting my code in the functions.php file.
This is my code:
class gmp_widget extends WP_Widget {
function gmp_widget() {
// widget actual processes
$widget_ops = array('classname' => 'gmp_widget', 'description' => __('Example widget that displays a user\'s bio.','gmp-plugin'));
$this->WP_Widget('gmp_widget_bio', __('Bio Widget','gmp-plugin'), $widget_ops);
}
public function form( $instance ) {
// outputs the options form on admin
$defaults = array( 'title' => __('My Bio', 'gmp-plugin'), 'name' => '', 'bio' => '');
$instance = wp_parse_args( (array) $instance, $defaults);
$title = strip_tags($instance['title']);
$name = strip_tags($instance['name']);
$bio = strip_tags($instance['bio']);
<p><?php _e('Title', 'gmp-plugin') ?>: <input class="widefat" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($name); ?>"</p>
<p><?php _e('Name', 'gmp-plugin') ?>: <input class="widefat" name="<?php echo $this->get_field_name('name'); ?>" type="text" value="<?php echo esc_attr($name); ?>"</p>
<p><?php _e('Bio', 'gmp-plugin') ?>: <textarea class="widefat" name="<?php echo $this->get_field_name('bio'); ?>" <?php echo esc_attr($title); ?></textarea></p>
}
public function update( $new_instance, $old_instance ) {
// processes widget options to be saved
$instance = array();
$instance['title'] = strip_tags($new_instance['title']);
$instance['name'] = strip_tags($new_instance['name']);
$instance['bio'] = strip_tags($new_instance['bio']);
return $instance;
}
public function widget( $args, $instance ) {
// outputs the content of the widget
extract($args);
echo $before_widget;
$title = apply_filters('widget_title', $instance['title'] );
$name = empty($instance['name']) ? ' ' :
apply_filters('widget_name', $instance['name']);
$bio = empty($instance['bio']) ? ' ' :
apply_filters('widget_bio', $instance['bio']);
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
echo '<p>' .__('Name', 'gmp-plugin') .': ' . $name . '</p>';
echo '<p>' .__('Bio', 'gmp-plugin') .': ' . $bio . '</p>';
echo $after_widget;
}
}
When it gets to the function form ($instance) I can see that the p tags are giving a syntax error as all the code below this goes from looking correct and all the right colour to black in my text editor which tells me there is a problem somewhere but I cannot figure out what it is. Any help will be greatly appreciated! Thanks in advance!
You have a problem with the php tags you have to close the php tags then start your html like this
public function form( $instance ) {
// outputs the options form on admin
$defaults = array( 'title' => __('My Bio', 'gmp-plugin'), 'name' => '', 'bio' => '');
$instance = wp_parse_args( (array) $instance, $defaults);
$title = strip_tags($instance['title']);
$name = strip_tags($instance['name']);
$bio = strip_tags($instance['bio']); ?> //close php tag here
// Now put your html
<p><?php _e('Title', 'gmp-plugin') ?>: <input class="widefat" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($name); ?>"</p>
<p><?php _e('Name', 'gmp-plugin') ?>: <input class="widefat" name="<?php echo $this->get_field_name('name'); ?>" type="text" value="<?php echo esc_attr($name); ?>"</p>
<p><?php _e('Bio', 'gmp-plugin') ?>: <textarea class="widefat" name="<?php echo $this->get_field_name('bio'); ?>" <?php echo esc_attr($title); ?></textarea></p> //end of html
// Now open php tag
<?php }

Wordpress widget with option in JS

I was wondering if in the JS from the code below I can add an option that can be set from the widget, when in wordpress backend, I hope you get my point.
More specifically, I need to add an option for slide duration. In the jCarousel that I am using, that duration is specified in $('.slides_widget').jcarousel , by the "auto: 5", so I must replace the "5" with the "slide_duration" option that is included in the widget below as you can see. How should I do it?
<?php
/* Slides Widget */
class Slides_Widget extends WP_Widget {
function Slides_Widget() {
$widget_ops = array('classname' => 'widget_slides', 'description' => __('Create slides with this widget.'));
$control_ops = array('width' => 400, 'height' => 350);
parent::__construct('slides_widget', __('Slides_Widget'), $widget_ops, $control_ops);
}
function widget( $args, $instance ) {
extract($args);
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
$slide_duration = $instance['slide_duration'];
$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">
<ul id="slides_widget" class="slides_widget">
<?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?>
</ul>
</div>
<?php
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['slide_duration'] = absint($new_instance['slide_duration']);
if (!$instance['slide_duration']) {
$instance['slide_duration'] = "";
}
if ( current_user_can('unfiltered_html') )
$instance['text'] = $new_instance['text'];
else
$instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) );
$instance['filter'] = isset($new_instance['filter']);
return $instance;
}
function form( $instance ) {
$defaults = array( 'filter' => true, 'title' => 'Create Your Slides', 'text' => '<li>Each slide must be in a "li" element, like this one.</li>
<li>Or like this one. Save it and check it.</li>' );
$instance = wp_parse_args( (array) $instance, $defaults );
$title = strip_tags($instance['title']);
$text = esc_textarea($instance['text']);
$slide_duration = strip_tags($instance['slide_duration']);
?>
<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><label for="<?php echo $this->get_field_id('slide_duration'); ?>"><?php _e('Slide Duration:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('slide_duration'); ?>" name="<?php echo $this->get_field_name('slide_duration'); ?>" type="text" value="<?php echo esc_attr($slide_duration); ?>" />
</p>
<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> <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
}
}
register_widget('Slides_Widget');
function pixy_asd() {
?>
<script type='text/javascript'>
jQuery(document).ready(function($) {
/* Slides Widget */
function mycarousel_initCallback(carousel)
{
carousel.buttonNext.bind('click', function() {
carousel.startAuto(0);
});
carousel.buttonPrev.bind('click', function() {
carousel.startAuto(0);
});
carousel.clip.hover(function() {
carousel.stopAuto();
}, function() {
carousel.startAuto();
});
};
$('.slides_widget').jcarousel({
auto: 5,
wrap: 'last',
visible: 1,
scroll: 1,
initCallback: mycarousel_initCallback
});
});
</script>
<?php
}
add_action('wp_head', 'pixy_asd');
function pixy_slides_widget_script() {
wp_enqueue_script('pixySlidesJS', get_stylesheet_directory_uri() . '/admin/include/theme-slides-widget/js/slides_widget.js', array('jquery'), true);
wp_enqueue_style( 'pixySlidesCSS', get_stylesheet_directory_uri() . '/admin/include/theme-slides-widget/css/slides_widget.css', false, 1, 'screen' );
}
add_action('wp_enqueue_scripts', 'pixy_slides_widget_script');
?>
Please let me know if you need more clarification.
Thank you!
in form() add:
<input type="hidden" id="slide_duration" value="<?php echo $slide_duration;?>">
then use instead of auto: 5 this:
auto: $("#widgets-right #slide_duration").val()
What about a php solution? In pixy_asd():
$options = get_option('Slides_Widget');
$slide_duration = $options['slide_duration'];
?>
...
$('.slides_widget').jcarousel({
auto: <?php echo $slide_duration;?>,
wrap: 'last',

Categories