Widget dynamically add new input field References I\'m trying to edit widget and add new input field for facebook user ID and Location. The widget has 4 parts of code, display the widget on the screen, fetch facebook userdata and post, update widget settings and Displays the widget settings controls on the widget panel. Code for display the widget on the screen /** * display the widget on the screen. */ function widget( $args, $instance ) { extract( $args ); /* Our variables from the widget settings. */ $user_loc1 = $instance[\'user_loc1\']; $user_loc2 = $instance[\'user_loc2\']; $user_loc3 = $instance[\'user_loc3\']; $user_loc4 = $instance[\'user_loc4\']; $user_loc5 = $instance[\'user_loc5\']; $user1 = $instance[\'user1\']; $user2 = $instance[\'user2\']; $user3 = $instance[\'user3\']; $user4 = $instance[\'user4\']; $user5 = $instance[\'user5\']; $user_gen = $instance[\'user_gen\']; $app_id = $instance[\'app_id\']; $secret_code = $instance[\'secret_code\']; $post_count = $instance[\'post_count\']; if( empty($post_count) ){ $post_count = \'5\'; } $location = wpgc_shortcode_city($atts, $content=\'\'); if ($user_loc1 == $location){ $user_id = $user1; } else if ($user_loc2 == $location){ $user_id = $user2; } else if ($user_loc3 == $location){ $user_id = $user3; } else if ($user_loc4 == $location){ $user_id = $user4; } else if ($user_loc5 == $location){ $user_id = $user5; } else { $user_id = $user_gen; } } Code for Fetch facebook userdata and post (will skip this as this will not be edited) //fetch facebook userdata and post Code for Update widget settings /** * update widget settings */ function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance[\'user_loc1\'] = $new_instance[\'user_loc1\']; $instance[\'user_loc2\'] = $new_instance[\'user_loc2\']; $instance[\'user_loc3\'] = $new_instance[\'user_loc3\']; $instance[\'user_loc4\'] = $new_instance[\'user_loc4\']; $instance[\'user_loc5\'] = $new_instance[\'user_loc5\']; $instance[\'user1\'] = $new_instance[\'user1\']; $instance[\'user2\'] = $new_instance[\'user2\']; $instance[\'user3\'] = $new_instance[\'user3\']; $instance[\'user4\'] = $new_instance[\'user4\']; $instance[\'user5\'] = $new_instance[\'user5\']; $instance[\'user_gen\'] = $new_instance[\'user_gen\']; $instance[\'app_id\'] = $new_instance[\'app_id\']; $instance[\'secret_code\'] = $new_instance[\'secret_code\']; $instance[\'post_count\'] = $new_instance[\'post_count\']; return $instance; } And finally Code for Displays the widget settings controls on the widget panel /** * Displays the widget settings controls on the widget panel. * Make use of the get_field_id() and get_field_name() function * when creating your form elements. This handles the confusing stuff. */ function form( $instance ) { /* Set up some default widget settings. */ $defaults = array( \'user_loc1\' => \'\', \'user_loc2\' => \'\', \'user_loc3\' => \'\', \'user_loc4\' => \'\', \'user_loc5\' => \'\', \'user_gen\' => \'\', \'user1\' => \'\', \'user2\' => \'\', \'user3\' => \'\', \'user4\' => \'\', \'user5\' => \'\', \'app_id\' => \'\', \'secret_code\' => \'\', \'post_count\' => 5 ); $instance = wp_parse_args( (array) $instance, $defaults ); ?>
get_field_id( \'user_gen\' ); ?>\" name=\"get_field_name( \'user_gen\' ); ?>\" value=\"\" />
get_field_id( \'user_loc1\' ); ?>\" name=\"get_field_name( \'user_loc1\' ); ?>\" value=\"\" />
get_field_id( \'user1\' ); ?>\" name=\"get_field_name( \'user1\' ); ?>\" value=\"\" />
get_field_id( \'user_loc2\' ); ?>\" name=\"get_field_name( \'user_loc2\' ); ?>\" value=\"\" />
get_field_id( \'user2\' ); ?>\" name=\"get_field_name( \'user2\' ); ?>\" value=\"\" />
get_field_id( \'user_loc3\' ); ?>\" name=\"get_field_name( \'user_loc3\' ); ?>\" value=\"\" />
get_field_id( \'user3\' ); ?>\" name=\"get_field_name( \'user3\' ); ?>\" value=\"\" />
get_field_id( \'user_loc4\' ); ?>\" name=\"get_field_name( \'user_loc4\' ); ?>\" value=\"\" />
get_field_id( \'user4\' ); ?>\" name=\"get_field_name( \'user4\' ); ?>\" value=\"\" />
get_field_id( \'user_loc5\' ); ?>\" name=\"get_field_name( \'user_loc5\' ); ?>\" value=\"\" />
get_field_id( \'user5\' ); ?>\" name=\"get_field_name( \'user5\' ); ?>\" value=\"\" />
get_field_id( \'app_id\' ); ?>\" name=\"get_field_name( \'app_id\' ); ?>\" value=\"\" />
get_field_id( \'secret_code\' ); ?>\" name=\"get_field_name( \'secret_code\' ); ?>\" value=\"\" />
get_field_id( \'post_count\' ); ?>\" name=\"get_field_name( \'post_count\' ); ?>\" value=\"\" />
No comments:
Post a Comment