• Resolved rundall

    (@rundall)


    My website is only available for a few cities.

    I would like to make it so when users go to the search field or when listing their item in location they only have a choice of a couple of predetermined cities to choose form in the location field.

    Is this possible?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    yes, you can do that either with the Custom Fields extension https://wpadverts.com/extensions/custom-fields/ or via the API.

    For example, adding the code below in your theme functions.php file should do that, just update in the code the list of locations with your own locations.

    
    add_filter( "adverts_form_load", "adverts_add_location_to_dropdown" );
    function adverts_add_location_to_dropdown( $form ) {
      if( $form['name'] == "advert" ) {
        $field_name = "adverts_location";
      } else if( $form['name'] == "search" ) {
        $field_name = "location";
      } else {
        return $form;
      }
      foreach( $form["field"] as $key => $field ) {
        if( $field["name"] == $field_name ) {
            $form["field"][$key]["type"] = "adverts_field_select";
            $form["field"][$key]["empty_option"] = true;
            $form["field"][$key]["options"] = array(
                array( "value" => "London", "text" => "London" ),
                array( "value" => "New York", "text" => "New York" ),
                /// more options here ...
            );
        }
      }
      return $form;
    }
    
    
    Thread Starter rundall

    (@rundall)

    That is great thank you very much.
    Im thinking about the future so i might buy the plugin also…

    Where does the placeholder go please in the above code?

    Plugin Author Greg Winiarski

    (@gwin)

    If you would like to set some placeholder when no option is selected in the dropdown then below

    
    $form["field"][$key]["empty_option"] = true;
    

    you can add the following line

    
    $form["field"][$key]["empty_option_text"] = "Select location ...";
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Predetermined Cities in Location’ is closed to new replies.