How to Add an extra field in the user profile and inherit them in ads
Inherit the address (and other extra fields) of the user in the ad is very useful for those users who have a lot of ads.
This allows the user does not enter every time one and same values and inherit them from their profile.
There are two questions:
1. How do I add additional fields in the user profile?
2. How to inherit them?
In this tutorial I will share my solution.
I will create a text field 'phone _number', 'user _city', 'user _adress', 'user _office', and a drop-down list of 'user _state'.
To avoid mistakes of the list of states will be taken from the values 'cp _state'.
When filling out forms, user can leave these values, or replace with new ones.
You can create any other fields.
1. How do I add additional fields in the user profile
Need to modify the file classipress\includes\theme-profile.php
1.1 Find the array declaration '$appthemes_extended_profile_fields = array(' and replace the code:
PHP Code:
$appthemes_extended_profile_fields = array(
'twitter_id' => array(
'title'=> __('Twitter:','appthemes'),
'type' => 'text',
'description' => __('Enter your Twitter username without the URL.','appthemes')
),
'facebook_id' => array(
'title'=> __('Facebook:','appthemes'),
'type' => 'text',
'description' => sprintf(__("Enter your Facebook username without the URL. <br />Don't have one yet? <a target='_blank' href='%s'>Get a custom URL.</a>",'appthemes'), 'http://www.facebook.com/username/')
),
'paypal_email' => array(
'title'=> __('PayPal Email:','appthemes'),
'type' => 'text',
'description' => __('Used for purchasing ads via PayPal (if enabled).','appthemes')
),
'active_membership_pack' => array(
'title'=> __('Active Membership Pack:','appthemes'),
'protected' => 'yes',
'type' => 'active_membership_pack',
'description' => __('Custom Membership Pack active for the user. Can only be changed by admins.','appthemes')
),
'membership_expires' => array(
'title'=> __('Membership Pack Expires Date:','appthemes'),
'protected' => 'yes',
'type' => 'date',
'description' => __('Date for unlimited/dealer posting (if enabled). Can only be changed by admins.','appthemes')
),
/////////Your's extra fields////////////
'phone_number' => array(
'title'=> 'phone number:',
'type' => 'text',
'description' => 'Enter phone number'
),
'user_state' => array(
'title'=> 'State:',
'type' => 'statedropdown',
'description' => 'Select a state'
),
'user_city' => array(
'title'=> 'City:',
'type' => 'text',
'description' => 'Enter city'
),
'user_adress' => array(
'title'=> 'Adress:',
'type' => 'text',
'description' => 'Enter adress'
),
'user_office' => array(
'title'=> 'Office:',
'type' => 'text',
'description' => 'Enter office'
)
///////////////////////////////////////////////////////
);
1.2 Find tis code:
PHP Code:
case 'active_membership_pack':
?>
<input type="text" name="<?php echo $field_id; ?>" id="<?php echo $field_id; ?>" value="<?php esc_attr_e( $the_value ); ?>" class="regular-text" size="35" <?php if(!empty($protected)) echo 'style="display: none;"'; ?> /><br />
<input type="text" name="<?php echo $field_id; ?>_display" id="<?php echo $field_id; ?>" value="<?php esc_attr_e( $the_display_value ); ?>" class="regular-text" size="35" disabled="disabled" /><br />
<span class="description"><?php echo $field_values['description']; ?></span>
<?php
break;
Below insert:
PHP Code:
case 'statedropdown':
global $wpdb;
$regions = $wpdb->get_var( $wpdb->prepare( "SELECT field_values FROM ". $wpdb->prefix . "cp_ad_fields WHERE field_name = 'cp_state';" ) );
if ( $regions ) {
?>
<select name="<?php echo $field_id; ?>" id="<?php echo $field_id; ?>" >
<option value="">-- <?php _e('Select', 'appthemes') ?> --</option>
<?php
$options = explode( ',', $regions);
foreach ( $options as $option ) {
?>
<option <?php if ($the_value == trim($option)) echo "selected='selected'"; ?> value="<?php esc_attr_e($option); ?>"><?php esc_attr_e($option); ?></option>
<?php
}
?>
</select>
<br />
<span class="description"><?php echo $field_values['description']; ?></span>
<?php
}
break;
2. Inherit fields
2.1 Add function pt_fields_defaults () in functions.php file of your child theme
PHP Code:
function pt_fields_defaults($field_name,$user) {
if ($user->ID){
switch($field_name){
case 'cp_city':
if ($user->user_city) return $user->user_city;
break;
case 'cp_street':
if ($user->user_adress) return $user->user_adress;
break;
case 'cp_office':
if ($user->user_office) return $user->user_office;
break;
case 'cp_state':
if ($user->user_state) return $user->user_state;
break;
}
}
}
Need to modify the file classipress\includes\forms\step-functions.php
2.2 Find this code:
PHP Code:
function cp_formbuilder($results) {
global $wpdb;
foreach ( $results as $result ) {
?>
Replace:
PHP Code:
function cp_formbuilder($results) {
global $wpdb;
$current_user = wp_get_current_user();
foreach ( $results as $result ) {
$opt = pt_fields_defaults($result->field_name,$current_user);
?>
2.3 Changes the attribute 'value' for the text box
Find:
PHP Code:
case 'text box':
?>
<input name="<?php esc_attr_e($result->field_name); ?>" id="<?php esc_attr_e($result->field_name); ?>" type="text" minlength="<?php if (empty($result->field_min_length)) echo '2'; else echo esc_attr($result->field_min_length); ?>" value="<?php if (isset($_POST[$result->field_name])) echo esc_attr($_POST[$result->field_name]); ?>" class="text <?php if ($result->field_req) echo 'required' ?>" />
then
PHP Code:
value="<?php if (isset($_POST[$result->field_name])) echo esc_attr($_POST[$result->field_name]); ?>"
replace:
PHP Code:
value="<?php if (isset($_POST[$result->field_name])) echo esc_attr($_POST[$result->field_name]); else echo esc_attr($opt);?>"
2.4 Drop-down to be set by default by adding the tag "option" attribute selected = "selected". Selects a value from the user profile
Find
PHP Code:
case 'drop-down':
?>
<select name="<?php esc_attr_e($result->field_name); ?>" id="<?php esc_attr_e($result->field_name); ?>" class="dropdownlist <?php if ($result->field_req) echo 'required' ?>">
<option value="">-- <?php _e('Select', 'appthemes') ?> --</option>
<?php
$options = explode( ',', $result->field_values );
foreach ( $options as $option ) {
?>
<option value="<?php esc_attr_e($option); ?>"><?php esc_attr_e($option); ?></option>
<?php
}
?>
</select>
<div class="clr"></div>
<?php
break;
then
PHP Code:
<option value="<?php esc_attr_e($option); ?>"><?php esc_attr_e($option); ?></option>
replace:
PHP Code:
<option <?php if (trim($option) == $opt ) echo "selected='selected'"; ?> value="<?php esc_attr_e($option); ?>"><?php esc_attr_e($option); ?></option>
FINALLY!!!!!!!
OK send your questions and comments