我正在尝试保存一个简单的小部件,但是每次点击“保存”时,它都不会保存这些值。而是将表单刷新为默认值。
代码如下。
我发现$instance
函数中update()
的值是一个空数组,即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
}
}
答案 0 :(得分:6)
看起来你刚才问过这个问题,但我会回答以防其他人跟进。
确保您正在使用:
$field_name = $this->get_field_name('your_field_name');
为窗口小部件表单项创建字段名称时。字段名称需要符合WordPress的命名约定。否则,您将从表单帖子中获得任何回复。
答案 1 :(得分:0)
更改您的班级名称
class Page_Widget extends WP_Widget
到这个
class WP_Widget_Page_Widget extends WP_Widget
并更改此行
add_action( 'widgets_init', create_function( '', 'register_widget("Page_Widget");' ) );
到
add_action( 'widgets_init', create_function( '', 'register_widget("WP_Widget_Page_Widget ");' ) );
这将适用于insaAllah。