Reputation: 11649
I'm trying to save a simple widget, however everytime I hit save, it does not save the values. Instead the form is refreshed to a default value.
The code is below.
What I have found is that the value of $instance
in the update()
function is an empty array i.e. array()
why is this??
<?php
defined('ABSPATH') or die("Cannot access pages directly.");
defined("DS") or define("DS", DIRECTORY_SEPARATOR);
add_action( 'widgets_init', create_function( '', 'register_widget("Page_Widget");' ) );
add_action( 'admin_head', 'page_widget_admin_head' );
$pw_class = new Page_Widget();
add_action( 'wp_ajax_nopriv_pw_get_option', array($pw_class, 'get_options'));
add_action( 'wp_ajax_pw_get_option', array($pw_class, 'get_options'));
function page_widget_admin_head()
{
if(basename($_SERVER['SCRIPT_NAME']) != 'widgets.php'){return false; }
echo '<style> .titler { width:80px; display:inline-block; }</style>';
echo '<script type="text/javascript">
jQuery(function($){
$(".post_type_select").live("change", function(){
the_opt = $(this).val();
el = $(this);
$.post(ajaxurl, "action=pw_get_option&pw_post_type="+the_opt, function(data){
el.siblings(".posts_select").html(data);
})
});
});</script>';
}
/**
*
* @author byrd
* Document Widget
*/
class Page_Widget extends WP_Widget
{
/**
* Constructor
*
* Registers the widget details with the parent class
*/
function __construct()
{
// widget actual processes
parent::__construct( $id = 'page_widget', $name = 'Page Widget', $options = array( 'description' => 'A Widget for grabbing page ids' ) );
}
function form($instance)
{
// outputs the options form on admin
$post_type_str = '<span class="titler">Post Type: </span><select name="pw_post_type" class="post_type_select">';
$post_types = get_post_types(array('public' => true),' objects');
$i = 1;
$pw_post_type = '';
$pw_post_id = '';
error_log(var_export($instance, true));
if ( $instance ) {
$pw_post_type = esc_attr( $instance[ 'pw_post_type' ] );
$pw_post_id = esc_attr( $instance[ 'pw_post_id' ] );
}
foreach ($post_types as $post_type ) {
$name = $post_type->labels->name;
$var = $post_type->name;
if($i == 1){$first = $var; }
if($pw_post_type == $var){$selected = 'selected="selected"'; }else{$selected = ''; }
$post_type_str .= '<option '.$selected.' value="'.$var.'">'. $name. '</option>';
$i++;
}
$options = $this->get_options($first, $pw_post_id);
$post_type_str .= '</select><br/><span class="titler">Post Name: </span><select name="pw_post_id" class="posts_select">'.$options.'</select>';
echo $post_type_str;
}
function get_options($post_type = false, $pw_post_id = '', $ajax = false)
{
if(!$post_type){ $post_type = $_POST['pw_post_type']; $ajax = true; }
$args = array('numberposts' => -1, 'post_type' => $post_type);
$str = '';
$posts_array = get_posts( $args );
foreach( $posts_array as $post )
{
if($pw_post_id == $post->ID){$selected = 'selected="selected"'; }else{ $selected = ''; }
$str .= '<option '.$selected.' value="'.$post->ID.'">'.$post->post_title.'</option>';
}
if($ajax){ echo $str; exit(); }
else{ return $str; }
}
function update($new_instance, $old_instance)
{
error_log(var_export($new_instance, true));
error_log(var_export($old_instance, true));
// processes widget options to be saved
$instance = wp_parse_args($old_instance, $new_instance);
return $new_instance;
}
function widget($args, $instance)
{
// outputs the content of the widget
}
}
Upvotes: 1
Views: 2174
Reputation: 665
Change your class name which is
class Page_Widget extends WP_Widget
to this
class WP_Widget_Page_Widget extends WP_Widget
and also change this line
add_action( 'widgets_init', create_function( '', 'register_widget("Page_Widget");' ) );
to
add_action( 'widgets_init', create_function( '', 'register_widget("WP_Widget_Page_Widget ");' ) );
This will work insaAllah.
Upvotes: 0
Reputation: 61
Looks like you asked this a while ago, but I'll answer in case others follow along.
Make sure you're using:
$field_name = $this->get_field_name('your_field_name');
when creating field names for your widget form items. The field names need to conform to WordPress' naming conventions. Otherwise, you'll get nothing back from the form post.
Upvotes: 6