Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Search by city Widget (tutorial)

  1. #1
    Marketplace Seller ahikmahin's Avatar
    Join Date
    Aug 2010
    Location
    Australia
    Posts
    504
    Thanks
    32
    Thanked 52 Times in 42 Posts

    Thumbs up Search by city Widget (tutorial)

    Hallo All,

    here is the tutorial step by step (basically full codes) for "Search by top city" widget. We decided to make it free for every one! by following this codes you can get others widgets as well.

    Lets go!!

    1. use any text editor make a php file name widget-city.php

    2. copy and paste these codes on your widget-city.php file

    PHP Code:
    <?php
    /**
     * Add function to widgets_init that'll load our widget.
     */
    add_action'widgets_init''CP_Cities_load_widgets' );

    /**
     * Register our widget.
     */
    function CP_Cities_load_widgets() {
        
    register_widget'CP_Cities_Widget' );
    }

    /**
     * CP_Cities_Widget class.
     */
    class CP_Cities_Widget extends WP_Widget {

        
    /**
         * Widget setup.
         */
        
    function CP_Cities_Widget() {
            
    /* Widget settings. */
            
    $widget_ops = array( 'classname' => 'city''description' => __('An widget that displays Cities''example') );

            
    /* Create the widget. */
            
    $this->WP_Widget'CP-cities'__('CP City Widget''example'), $widget_ops$control_ops );
        }

        
    /**
         * How to display the widget on the screen.
         */
        
    function widget$args$instance ) {
            
    extract$args );

            
    /* Our variables from the widget settings. */
            
    $title apply_filters('widget_title'$instance['title'] );
            
    $count = isset( $instance['count'] ) ? $instance['count'] : 5;

            
    /* Before widget (defined by themes). */
            
    echo $before_widget;

            
    /* Display the widget title if one was input (before and after defined by themes). */
            
    if ( $title )
                echo 
    $before_title $title $after_title;

            global 
    $wpdb;
            
    $cities = array();
            
    $sql $wpdb->prepare("SELECT * FROM `{$wpdb->prefix}cp_ad_pop_total` t
                                LEFT JOIN 
    $wpdb->postmeta m
                                ON t.postnum=m.post_id
                                WHERE m.meta_key='cp_city' ORDER BY t.postcount desc"
    );
                                
            
    $results $wpdb->get_results($sql);
            foreach(
    $results as $r)
            {
                
    array_push($cities$r->meta_value);
            }
            
    $cities array_unique($cities);
            
    $cities array_values($cities);

            
    $cities_n count($cities) - 1;

            for (
    $i=$i <= $cities_n $i++)
            {
                if ( (
    $i%2) == 0)
                    
    $cities_left $cities_left '<li><a href="?s=' urlencode($cities[$i]) . '">' $cities[$i] . '</a></li>';

                else
                    
    $cities_right $cities_right '<li><a href="?s=' urlencode($cities[$i]) . '">' $cities[$i] . '</a></li>';
            }

            echo 
    '<ul class="sidebar-left-col">';
            echo 
    $cities_left;
            echo 
    '</ul>';

            echo 
    '<ul class="sidebar-right-col">';
            echo 
    $cities_right;
            echo 
    '</ul>';

            
    /* After widget (defined by themes). */
            
    echo $after_widget;
        }


        
    /**
         * Update the widget settings.
         */
        
    function update$new_instance$old_instance ) {
            
    $instance $old_instance;

            
    /* Strip tags for title and name to remove HTML (important for text inputs). */
            
    $instance['title'] = strip_tags$new_instance['title'] );

            return 
    $instance;
        }

        
    /**
         * Displays the widget settings controls on the widget panel.
         */
        
    function form$instance ) {

            
    /* Set up some default widget settings. */
            
    $defaults = array( 'title' => '');
            
    $instance wp_parse_args( (array) $instance$defaults ); ?>

            <!-- Widget Title -->
            <p>
                <label for="<?php echo $this->get_field_id'title' ); ?>"><?php _e('Title:''hybrid'); ?></label>
                <input id="<?php echo $this->get_field_id'title' ); ?>" class="widefat" name="<?php echo $this->get_field_name'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
            </p>
        <?php
        
    }
    }

    ?>
    3. Save it and upload to your server theme folder

    4. Open your functions.php file

    5.You can see these lines at end (classipress v3.04)
    /**
    * add any of your custom functions below this section
    */
    add this line at the end -

    PHP Code:
    include_once 'widget-city.php'
    6. go to your admin backend-> widgets

    You can see the city search widget there , now active it as normal widget anywhere you want!

    for live demo - www.amartutor.com

    image:-





    * Its picked up the city name from your users ad post
    * First comes first -> User who post first that city name comes first

    << It doesn't show all cities. Only shows, existing ad's city names >>

    if you have any questions or got idea to make it more usefull please share here.

    Thank you & Enjoy!

  2. The Following User Says Thank You to ahikmahin For This Useful Post:

    pratish7 (November 4th, 2010)

  3. #2
    jomarkosabel's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    40,982
    Thanks
    170
    Thanked 3,408 Times in 3,279 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!
    Please help our moderating team work more efficiently by not sending us support questions via PM. You can read more about how AppThemes support works here. However, you can send a PM to follow up and remind me if I missed your support request/thread.

    Thank you and have a nice day.

  4. The Following User Says Thank You to jomarkosabel For This Useful Post:

    ahikmahin (November 2nd, 2010)

  5. #3
    Junior Member scatter's Avatar
    Join Date
    Oct 2010
    Location
    Slovakia
    Posts
    24
    Thanks
    0
    Thanked 3 Times in 1 Post
    You must be an AppThemes customer and logged in to view this response. Join today!

  6. #4
    Marketplace Seller ahikmahin's Avatar
    Join Date
    Aug 2010
    Location
    Australia
    Posts
    504
    Thanks
    32
    Thanked 52 Times in 42 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!

  7. #5
    Veteran spymare's Avatar
    Join Date
    Aug 2010
    Location
    Denmark
    Posts
    871
    Thanks
    16
    Thanked 82 Times in 61 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!

  8. #6
    Marketplace Seller ahikmahin's Avatar
    Join Date
    Aug 2010
    Location
    Australia
    Posts
    504
    Thanks
    32
    Thanked 52 Times in 42 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!

  9. #7
    the1stco's Avatar
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    89
    Thanks
    2
    Thanked 0 Times in 0 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!

  10. #8
    Junior Member scatter's Avatar
    Join Date
    Oct 2010
    Location
    Slovakia
    Posts
    24
    Thanks
    0
    Thanked 3 Times in 1 Post
    You must be an AppThemes customer and logged in to view this response. Join today!

  11. #9
    Marketplace Seller ahikmahin's Avatar
    Join Date
    Aug 2010
    Location
    Australia
    Posts
    504
    Thanks
    32
    Thanked 52 Times in 42 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!

  12. #10
    Marketplace Seller ahikmahin's Avatar
    Join Date
    Aug 2010
    Location
    Australia
    Posts
    504
    Thanks
    32
    Thanked 52 Times in 42 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Adding search via Country, State and City
    By vistago in forum ClassiPress General Discussion
    Replies: 11
    Last Post: October 11th, 2013, 09:15 PM
  2. Search by City Hack - Solution
    By buhle78 in forum Help Using ClassiPress
    Replies: 9
    Last Post: June 17th, 2010, 12:08 AM
  3. Adding search via Country, State and City
    By vistago in forum Help Using ClassiPress
    Replies: 0
    Last Post: December 31st, 1969, 07:00 PM