Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 ); ?>

    <p>
        <label for="<?php echo $this->get_field_id( 'user_gen' ); ?>"><?php _e('Main Facebook:', 'dfsthemes') ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'user_gen' ); ?>" name="<?php echo $this->get_field_name( 'user_gen' ); ?>" value="<?php echo $instance['user_gen']; ?>" />
    </p>

    <p>
        <label for="<?php echo $this->get_field_id( 'user_loc1' ); ?>"><?php _e('Location 1:', 'dfsthemes') ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'user_loc1' ); ?>" name="<?php echo $this->get_field_name( 'user_loc1' ); ?>" value="<?php echo $instance['user_loc1']; ?>" />
    </p>

    <p>
        <label for="<?php echo $this->get_field_id( 'user1' ); ?>"><?php _e('User ID 1:', 'dfsthemes') ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'user1' ); ?>" name="<?php echo $this->get_field_name( 'user1' ); ?>" value="<?php echo $instance['user1']; ?>" />
    </p>

    <p>
        <label for="<?php echo $this->get_field_id( 'user_loc2' ); ?>"><?php _e('Location 2:', 'dfsthemes') ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'user_loc2' ); ?>" name="<?php echo $this->get_field_name( 'user_loc2' ); ?>" value="<?php echo $instance['user_loc2']; ?>" />
    </p>

    <p>
        <label for="<?php echo $this->get_field_id( 'user2' ); ?>"><?php _e('User ID 2:', 'dfsthemes') ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'user2' ); ?>" name="<?php echo $this->get_field_name( 'user2' ); ?>" value="<?php echo $instance['user2']; ?>" />
    </p>

    <p>
        <label for="<?php echo $this->get_field_id( 'user_loc3' ); ?>"><?php _e('Location: 3', 'dfsthemes') ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'user_loc3' ); ?>" name="<?php echo $this->get_field_name( 'user_loc3' ); ?>" value="<?php echo $instance['user_loc3']; ?>" />
    </p>

    <p>
        <label for="<?php echo $this->get_field_id( 'user3' ); ?>"><?php _e('User ID 3:', 'dfsthemes') ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'user3' ); ?>" name="<?php echo $this->get_field_name( 'user3' ); ?>" value="<?php echo $instance['user3']; ?>" />
    </p>

    <p>
        <label for="<?php echo $this->get_field_id( 'user_loc4' ); ?>"><?php _e('Location 4:', 'dfsthemes') ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'user_loc4' ); ?>" name="<?php echo $this->get_field_name( 'user_loc4' ); ?>" value="<?php echo $instance['user_loc4']; ?>" />
    </p>

    <p>
        <label for="<?php echo $this->get_field_id( 'user4' ); ?>"><?php _e('User ID 4:', 'dfsthemes') ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'user4' ); ?>" name="<?php echo $this->get_field_name( 'user4' ); ?>" value="<?php echo $instance['user4']; ?>" />
    </p>

    <p>
        <label for="<?php echo $this->get_field_id( 'user_loc5' ); ?>"><?php _e('Location 5:', 'dfsthemes') ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'user_loc5' ); ?>" name="<?php echo $this->get_field_name( 'user_loc5' ); ?>" value="<?php echo $instance['user_loc5']; ?>" />
    </p>

    <p>
        <label for="<?php echo $this->get_field_id( 'user5' ); ?>"><?php _e('User ID 5:', 'dfsthemes') ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'user5' ); ?>" name="<?php echo $this->get_field_name( 'user5' ); ?>" value="<?php echo $instance['user5']; ?>" />
    </p>

    <p>
        <label for="<?php echo $this->get_field_id( 'app_id' ); ?>"><?php _e('App ID:', 'dfsthemes') ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'app_id' ); ?>" name="<?php echo $this->get_field_name( 'app_id' ); ?>" value="<?php echo $instance['app_id']; ?>" />
    </p>

    <p>
        <label for="<?php echo $this->get_field_id( 'secret_code' ); ?>"><?php _e('App Secret Code:', 'dfsthemes') ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'secret_code' ); ?>" name="<?php echo $this->get_field_name( 'secret_code' ); ?>" value="<?php echo $instance['secret_code']; ?>" />
    </p>

    <p>
        <label for="<?php echo $this->get_field_id( 'post_count' ); ?>"><?php _e('Number of posts to display:', 'dfsthemes') ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'post_count' ); ?>" name="<?php echo $this->get_field_name( 'post_count' ); ?>" value="<?php echo $instance['post_count']; ?>" />
    </p>
<?php
}

I am manually adding the user1, user2, user3 etc. and user_loc1, user_loc2, user_loc3, etc. only parts of the code.

Now my question is.. What would be the approach to dynamically add new user and user_loc on the settings panel so I don't need to manually edit the file and upload it. I'm thinking of having a Add New User and Add New Location button but I'm not sure how to code it that will automatically update all user and user_loc in all part of the widget script.

I've seen some code using jQuery and append the new inputs but I not sure how to make it work to update all the data with user and user_loc.

Any help on this? If you can provide me with the exact code to use then that would really much be appreciated.

TIA :D

share|improve this question
    
Something like this? –  toscho Aug 25 '13 at 7:01
    
I think that will do. will check and see if I can auto generate two new input field instead of one. :D thanks! –  Mac Mendoza Aug 27 '13 at 5:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.