我試圖保存一個簡單的小部件,但是每次點擊保存時,它都不會保存這些值。而是將表單刷新爲默認值。WordPress的小工具不保存
代碼如下。
我發現的是update()
函數中的$instance
的值是一個空數組,也就是array()
這是爲什麼?
<?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
}
}