Last active
January 19, 2017 22:20
-
-
Save elhardoum/6ea2f815e69d52a63122f50c6190c780 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** @link https://samelh.com/blog/2016/12/22/add-custom-fields-bbpress-profile/ **/ | |
// add field to bbP profile edit | |
add_action( "bbp_user_edit_after_contact", "se_add_city_field" ); | |
function se_add_city_field() { | |
// a random selection of cities | |
$cities = array( "Seoul", "Mexico City", "Amsterdam", "Mumbai", "Agadir", "Egypt" ); | |
// selected city, if any | |
$user_city = bbp_get_displayed_user_field('se_city'); | |
?> | |
<div> | |
<label for="city"><?php _e( 'City', 'my-domain' ); ?></label> | |
<select name="city" id="city"> | |
<option value="">Select City</option> | |
<?php foreach ( $cities as $city ) : ?> | |
<option <?php selected($user_city,$city); ?>><?php echo esc_attr( $city ); ?></option> | |
<?php endforeach; ?> | |
</select> | |
</div> | |
<?php | |
} | |
// when updating our profile | |
add_action( "personal_options_update", "se_save_city_value" ); | |
// when updating other users' profiles | |
add_action( "edit_user_profile_update", "se_save_city_value" ); | |
function se_save_city_value( $user_id ) { | |
// exclude profile.php/user-edit update | |
if ( is_admin() ) return; | |
// update preference | |
if ( isset( $_POST['city'] ) && $_POST['city'] ) { | |
return update_user_meta( $user_id, "se_city", sanitize_text_field( $_POST['city'] ) ); | |
} else { | |
return delete_user_meta( $user_id, "se_city" ); | |
} | |
} | |
// add city after user profile details | |
add_action( "bbp_template_after_user_profile", "se_embed_city_profile" ); | |
function se_embed_city_profile() { | |
// selected city, if any | |
$user_city = bbp_get_displayed_user_field('se_city'); | |
// making sure user has set their city first | |
if ( !trim( $user_city ) ) return; | |
// all good | |
return printf( | |
'<p>%s is based on <strong>%s</strong></p>', | |
esc_attr( bbp_get_displayed_user_field('nickname') ), | |
esc_attr( $user_city ) | |
); | |
} | |
// add city after topic/reply author details | |
add_action( "bbp_theme_after_reply_author_details", "se_embed_city_forums" ); | |
function se_embed_city_forums() { | |
// topic/reply author | |
$user_id = bbp_get_reply_author_id(); | |
// selected city, if any | |
$user_city = get_user_meta( $user_id, 'se_city', true ); | |
// making sure user has set their city first | |
if ( !trim( $user_city ) ) return; | |
// all good | |
return printf( | |
'<p>%s is based on <strong>%s</strong></p>', | |
esc_attr( bbp_get_displayed_user_field('nickname') ), | |
esc_attr( $user_city ) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment