2011-11-18 79 views
1

我試圖保存一個簡單的小部件,但是每次點擊保存時,它都不會保存這些值。而是將表單刷新爲默認值。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 


    } 

} 

回答

5

看起來你之前問過這個問題,但我會回答以防其他人跟隨。

確保您使用:

$field_name = $this->get_field_name('your_field_name'); 

創建你的widget形式的項目字段名稱時。字段名稱需要符合WordPress的命名約定。否則,您將無法從表單文章中取回任何內容。

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。

0

在闡述A B's answer,只是如果你正在使用你的字段的標籤和ID的你將不得不使用get_field_id()方法太字段名稱可能還不夠:

$field_name = $this->get_field_name('your_field_name'); 
$field_id = $this->get_field_id('your_field_id'); 

echo '<label for="'.$field_id.'">Field</label>'; 
echo '<input type="text" id="'.$field_id.'" name="'.$field_name.'">'; 

另外,爲了防止頭痛,使確定您使用update()方法中收集的變量返回$instance陣列。