Automatically populate Country field when adding ads
There is one thing I’m missing on basic Classipress installation and that is automatic population of Country field when adding an ad. You could buy a plugin called Advanced Custom Fields for ClassiPress but this plugin offers much more than we need here.
This mod will automatically populate Country field with the country you are surfing from, with the help of GeoLite data created by MaxMind, available from http://www.maxmind.com.
This modification was tested on Classipress 3.3.X, 3.4.x and 3.5.x
This tutorial requires you to change this file (version prior to 3.4):
wp-content/themes/classipress/includes/forms/step-functions.php
And this file is you are using version 3.4 or newer:
wp-content/themes/classipress/includes/custom-forms.php
and add these two files into the same folder:
geoip.inc
GeoIP.dat
You can obtain these two files here:
GeoIP.zip
These two files were downloaded from MaxMind’s web page.
It is recommended to make backup of files before making any changes.
Step 1
Now open file step-functions.php (custom-forms.php for CP v3.4) and do this changes:
Find this code (it’s around line 19):
if ( !function_exists('cp_formbuilder') ) { function cp_formbuilder($results, $post = false) { global $wpdb, $cp_options; |
and add this code after it:
// Start of geoip code require_once( get_template_directory() . '/includes/forms/geoip.inc' ); $gi = geoip_open(get_template_directory() . '/includes/forms/GeoIP.dat', GEOIP_MEMORY_CACHE); $country = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']); geoip_close($gi); // End of geoip code |
Then around line 86 (case ‘drop-down’:) find this code:
if ( $option == $post_meta_val ) |
and change it to:
if ( $option == $post_meta_val || ($args['value'] == $country && !$post_meta_val) ) |
Don’t forget to copy two files I mentioned before to the same folder this file is in. You can move them to a different location, but make sure to change the code accordingly.
Step 2
Step 2 is optional and is meant for those who have changed the names of Countries in Classipress custom field Country – cp_country.
If you haven’t touched names of the countries in that list, then you should skip this step.
You have to open file geoip.inc and change array GEOIP_COUNTRY_NAMES according to the values in cp_country.
If you find any issues with this tutorial, please be so kind and write a comment. I’ll try to help as much as I can.
If you find this tutorial useful, please consider making a small donation.
Do this will display the country flag along with country name? or there any other tutorial for displaying each country flag along with that countries
I don’t know of any feature that will display a country flag along with country name. But it could be developed.
I can’t find the 2 files
I’ve downloaded every downloadable file in the link provided. There is nothing like ‘GeoIP.inc” in them.
I have added a link to these two files, since they could not be found on MaxMind’s page any more.
Hi,
it doesn’t work for classipress 3.4, the file step-functions.php is deprecated…
Can you provide the method for the last version of Classipress or a link to download plugin like ACF or DMLF?
Thanks by advance.
YaRi
so i comeback with the solution; this is the file to be modified:
../includes/custom-forms.php
line 100
line 170
I have changed the manual to work with Classipress version 3.4.
Regards.
I have checked your site and I see everything is in Russian language. But I don’t see the field for Country. If a list of countries on your site is in different language, you have change it also in geopip file. Values have to be the same.
hi man cant access the files
Hi,
The link was pointing to my old domain. I have corrected the link.
Kindly give it a try.
Blaz
Hi Man Now there is one more problem the latest classipress version doesnt have the necessary code in includes/forms/step-functions.php instaed they have only this code
<?php
/**
* Deprecated. No longer needed.
*
* @package ClassiPress
* @author AppThemes
* @since ClassiPress 3.0
*/
_deprecated_file( basename( __FILE__ ), '3.4', null, __( 'This file no longer needs to be included.', APP_TD ) );
do u need a copy of latest template to test
It is mentioned in the blog that for version 3.4 and higher you should change file custom-forms.php.
Regards.