Skip to content

Instantly share code, notes, and snippets.

@benthebear
Last active February 17, 2016 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benthebear/04a72256c660d8551891 to your computer and use it in GitHub Desktop.
Save benthebear/04a72256c660d8551891 to your computer and use it in GitHub Desktop.
user-addons.php
<?php
/**
* Plugin Name: User Addons
* Description: Adds custom User Meta Fields in Backend
* Version: 1.0
* Author: PALASTHOTEL (in Person: Benjamin Birkenhake)
* Author URI: http://www.palasthotel.de
* Text Domain: user_addons
*/
// Add WordPress Hooks
add_action( 'show_user_profile', 'user_addons_add_fields' );
add_action( 'edit_user_profile', 'user_addons_add_fields' );
add_action( 'personal_options_update', 'user_addons_save_fields' );
add_action( 'edit_user_profile_update', 'user_addons_save_fields' );
add_shortcode( 'usersearch' , 'user_addons_search_users' );
function user_addons_get_travel_types(){
$traveltypes['allincl'] = "Pauschalreisen";
$traveltypes['flight'] = "Nur Flug";
$traveltypes['club'] = "Club-Reisen";
$traveltypes['single'] = "Single-Reisen";
$traveltypes['honeymoon'] = "Hochzeitsreisen";
$traveltypes['indivual'] = "Individualreisen";
$traveltypes['business'] = "Business Travel";
$traveltypes['sport'] = "Sport-Reisen";
$traveltypes['adventure'] = "Abenteuerreisen";
$traveltypes['family'] = "Familienreisen";
$traveltypes['cruise'] = "Kreuzfahrten";
$traveltypes['river'] = "Flussreisen";
$traveltypes['group'] = "Gruppenreisen";
$traveltypes['tour'] = "Rundreisen";
$traveltypes['residence'] = "Ferienhäuser";
$traveltypes['hiking'] = "Wanderreisen";
$traveltypes['lastminute'] = "Last Minute";
return $traveltypes;
}
// Add the Fields to the Profile View within the Backend
function user_addons_add_fields( $user ) {
$user_addons_field_city = get_user_meta( $user->ID, 'user_addons_city', true );
$user_addons_field_zip = get_user_meta( $user->ID, 'user_addons_zip', true );
$user_addons_field_phone = get_user_meta( $user->ID, 'user_addons_phone', true );
$user_addons_field_agency = get_user_meta( $user->ID, 'user_addons_agency', true );
$user_addons_field_best = get_user_meta( $user->ID, 'user_addons_best', true );
$user_addons_field_next = get_user_meta( $user->ID, 'user_addons_next', true );
$user_addons_field_why = get_user_meta( $user->ID, 'user_addons_why', true );
$user_addons_field_what = get_user_meta( $user->ID, 'user_addons_what', true );
$user_addons_field_specialone = get_user_meta( $user->ID, 'user_addons_specialone', true );
$user_addons_field_specialtwo = get_user_meta( $user->ID, 'user_addons_specialtwo', true );
$user_addons_field_specialthree = get_user_meta( $user->ID, 'user_addons_specialthree', true );
$user_addons_field_specialfour = get_user_meta( $user->ID, 'user_addons_specialfour', true );
$user_addons_field_specialfive = get_user_meta( $user->ID, 'user_addons_specialfive', true );
$user_addons_field_specialsix = get_user_meta( $user->ID, 'user_addons_specialsix', true );
$user_addons_field_specialseven = get_user_meta( $user->ID, 'user_addons_specialseven', true );
$user_addons_field_specialeight = get_user_meta( $user->ID, 'user_addons_specialeight', true );
$user_addons_field_specialnine = get_user_meta( $user->ID, 'user_addons_specialnine', true );
$user_addons_field_specialten = get_user_meta( $user->ID, 'user_addons_specialten', true );
$traveltypes = user_addons_get_travel_types();
?>
<h3><?php _e( 'Weitere Einstellungen', 'user_addons' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="address"><?php _e( 'Ort', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<input id="user_addons_city" type="text" name="user_addons_city" value ="'.$user_addons_field_city.'" >';
echo '</p>';
?>
</td>
</tr>
<tr>
<th><label for="address"><?php _e( 'PLZ', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<input id="user_addons_zip" type="text" name="user_addons_zip" value ="'.$user_addons_field_zip.'" >';
echo '</p>';
?>
</td>
</tr>
<tr>
<th><label for="address"><?php _e( 'Telefonnummer', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<input id="user_addons_phone" type="text" name="user_addons_phone" value ="'.$user_addons_field_phone.'" >';
echo '</p>';
?>
</td>
</tr>
<tr>
<th><label for="address"><?php _e( 'Agenturname', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<input id="user_addons_agency" type="text" name="user_addons_agency" value ="'.$user_addons_field_agency.'" >';
echo '</p>';
?>
</td>
</tr>
<tr>
<th><label for="address"><?php _e( 'Meine beste Reise bisher', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<input id="user_addons_best" type="text" name="user_addons_best" value ="'.$user_addons_field_best.'" >';
echo '</p>';
?>
</td>
</tr>
<tr>
<th><label for="address"><?php _e( 'Meine nächste Reise', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<input id="user_addons_next" type="text" name="user_addons_next" value ="'.$user_addons_field_next.'" >';
echo '</p>';
?>
</td>
</tr>
<tr>
<th><label for="address"><?php _e( 'Warum ich Reiseberater/in bin', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<input id="user_addons_why" type="text" name="user_addons_why" value ="'.$user_addons_field_why.'" >';
echo '</p>';
?>
</td>
</tr>
<tr>
<th><label for="address"><?php _e( 'Was Sie bei mir erwartet', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<textarea id="user_addons_what" rows="4" cols="50" type="text" name="user_addons_what" value ="'.$user_addons_field_what.'" ></textarea>';
echo '</p>';
?>
</td>
</tr>
<tr>
<th><label for="address"><?php _e( '1. Spezialgebiet', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<input id="user_addons_specialone" type="text" name="user_addons_specialone" value ="'.$user_addons_field_specialone.'" >';
echo '</p>';
?>
</td>
</tr>
<tr>
<th><label for="address"><?php _e( '2. Spezialgebiet', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<input id="user_addons_specialtwo" type="text" name="user_addons_specialtwo" value ="'.$user_addons_field_specialtwo.'" >';
echo '</p>';
?>
</td>
</tr>
<tr>
<th><label for="address"><?php _e( '3. Spezialgebiet', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<input id="user_addons_specialthree" type="text" name="user_addons_specialthree" value ="'.$user_addons_field_specialthree.'" >';
echo '</p>';
?>
</td>
</tr>
<tr>
<th><label for="address"><?php _e( '4. Spezialgebiet', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<input id="user_addons_specialfour" type="text" name="user_addons_specialfour" value ="'.$user_addons_field_specialfour.'" >';
echo '</p>';
?>
</td>
</tr>
<tr>
<th><label for="address"><?php _e( '5. Spezialgebiet', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<input id="user_addons_specialfive" type="text" name="user_addons_specialfive" value ="'.$user_addons_field_specialfive.'" >';
echo '</p>';
?>
</td>
</tr>
<tr>
<th><label for="address"><?php _e( '6. Spezialgebiet', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<input id="user_addons_specialsix" type="text" name="user_addons_specialsix" value ="'.$user_addons_field_specialsix.'" >';
echo '</p>';
?>
</td>
</tr>
<tr>
<th><label for="address"><?php _e( '7. Spezialgebiet', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<input id="user_addons_specialseven" type="text" name="user_addons_specialseven" value ="'.$user_addons_field_specialseven.'" >';
echo '</p>';
?>
</td>
</tr>
<tr>
<th><label for="address"><?php _e( '8. Spezialgebiet', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<input id="user_addons_specialeight" type="text" name="user_addons_specialeight" value ="'.$user_addons_field_specialeight.'" >';
echo '</p>';
?>
</td>
</tr>
<tr>
<th><label for="address"><?php _e( '9. Spezialgebiet', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<input id="user_addons_specialnine" type="text" name="user_addons_specialnine" value ="'.$user_addons_field_specialnine.'" >';
echo '</p>';
?>
</td>
</tr>
<tr>
<th><label for="address"><?php _e( '10. Spezialgebiet', 'user_addons' ); ?></label></th>
<td>
<?php
echo '<p>';
echo '<input id="user_addons_specialten" type="text" name="user_addons_specialten" value ="'.$user_addons_field_specialten.'" >';
echo '</p>';
?>
</td>
</tr>
<?php
foreach($traveltypes as $key => $value){
$user_addons_type_field_current = false;
$user_addons_type_field_current = get_user_meta( $user->ID, 'user_addons_type_'.$key, true );
$checked ='';
if($user_addons_type_field_current){
$checked = "checked='checked'";
}
print "<tr>";
print '<th><label for="address">'.__( $value, 'user_addons' ).'</label></th>';
print "<td>";
print "<input type='checkbox' id='user_addons_type_".$key."' name='user_addons_type_".$key."' ".$checked."/>";
print "</td>";
print "</tr>";
}
?>
</table>
<?php }
// Save the Fields
function user_addons_save_fields( $user_id ) {
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
$traveltypes = user_addons_get_travel_types();
if ( isset( $_POST['user_addons_city'] ) ) {
update_user_meta( $user_id, 'user_addons_city', $_POST['user_addons_city']);
}
if ( isset( $_POST['user_addons_zip'] ) ) {
update_user_meta( $user_id, 'user_addons_zip', $_POST['user_addons_zip']);
}
if ( isset( $_POST['user_addons_phone'] ) ) {
update_user_meta( $user_id, 'user_addons_phone', $_POST['user_addons_phone']);
}
if ( isset( $_POST['user_addons_agency'] ) ) {
update_user_meta( $user_id, 'user_addons_agency', $_POST['user_addons_agency']);
}
if ( isset( $_POST['user_addons_best'] ) ) {
update_user_meta( $user_id, 'user_addons_best', $_POST['user_addons_best']);
}
if ( isset( $_POST['user_addons_next'] ) ) {
update_user_meta( $user_id, 'user_addons_next', $_POST['user_addons_next']);
}
if ( isset( $_POST['user_addons_why'] ) ) {
update_user_meta( $user_id, 'user_addons_why', $_POST['user_addons_why']);
}
if ( isset( $_POST['user_addons_what'] ) ) {
update_user_meta( $user_id, 'user_addons_what', $_POST['user_addons_what']);
}
if ( isset( $_POST['user_addons_specialone'] ) ) {
update_user_meta( $user_id, 'user_addons_specialone', $_POST['user_addons_specialone']);
}
if ( isset( $_POST['user_addons_specialtwo'] ) ) {
update_user_meta( $user_id, 'user_addons_specialtwo', $_POST['user_addons_specialtwo']);
}
if ( isset( $_POST['user_addons_specialthree'] ) ) {
update_user_meta( $user_id, 'user_addons_specialthree', $_POST['user_addons_specialthree']);
}
if ( isset( $_POST['user_addons_specialfour'] ) ) {
update_user_meta( $user_id, 'user_addons_specialfour', $_POST['user_addons_specialfour']);
}
if ( isset( $_POST['user_addons_specialfive'] ) ) {
update_user_meta( $user_id, 'user_addons_specialfive', $_POST['user_addons_specialfive']);
}
if ( isset( $_POST['user_addons_specialsix'] ) ) {
update_user_meta( $user_id, 'user_addons_specialsix', $_POST['user_addons_specialsix']);
}
if ( isset( $_POST['user_addons_specialseven'] ) ) {
update_user_meta( $user_id, 'user_addons_specialseven', $_POST['user_addons_specialseven']);
}
if ( isset( $_POST['user_addons_specialeight'] ) ) {
update_user_meta( $user_id, 'user_addons_specialeight', $_POST['user_addons_specialeight']);
}
if ( isset( $_POST['user_addons_specialnine'] ) ) {
update_user_meta( $user_id, 'user_addons_specialnine', $_POST['user_addons_specialnine']);
}
if ( isset( $_POST['user_addons_specialten'] ) ) {
update_user_meta( $user_id, 'user_addons_specialten', $_POST['user_addons_specialten']);
}
foreach($traveltypes as $key => $value){
if(isset($_POST["user_addons_type_".$key])){
update_user_meta( $user_id, 'user_addons_type_'.$key, $value);
}
}
}
// The Shortcode for the User-Search
function user_addons_search_users( $args, $content ) {
$output = "";
$traveltypes = user_addons_get_travel_types();
$keywords = "";
if(isset($_GET['us'])){
$keywords = sanitize_text_field($_GET['us']);
}
if(isset($_GET['ut'])){
$type_key = sanitize_text_field($_GET['ut']);
if(isset($traveltypes[$type_key])){
$type_value = $traveltypes[$type_key];
$type = $type_value;
}
}
$output .= "<form method='GET'>";
$output .= "<input type='text' name='us' placeholder='".__("Reiseberater finden", 'user_addons')."' value='".$keywords."'/>";
$output .= "<input type='submit' value='Los' />";
$output .= "</form>";
$output .= "<p>oder</p>";
$output .= "<form method='GET'>";
$output .= "<select name='ut'>";
$output .= "<option value=''>- Bitte wählen! -</option>";
foreach($traveltypes as $key=>$value){
$selected = "";
if($type_key == $key){
$selected = "selected='selected'";
}
$output .= "<option value='".$key."' ".$selected.">".$value."</option>";
}
$ouptut .= "</select>";
$output .= "<input type='submit' value='Los' />";
$output .= "</form>";
if($keywords!=""){
// Generate the Query
$wp_user_query = new WP_User_Query( array(
'order' => 'ASC',
'orderby' => 'display_name',
'meta_query' => array(
'relation' => 'OR',
array( 'key' => 'description',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'first_name',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'last_name',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'nickname',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_city',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_zip',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_phone',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_agency',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_best',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_next',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_why',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_what',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_specialone',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_specialtwo',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_specialthree',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_specialfour',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_specialfive',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_specialsix',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_specialseven',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_specialeight',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_specialnine',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_specialten',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_allinclusive',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_flightonly',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_club',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_single',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_honeymoon',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_indivual',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_business',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_sport',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_adventure',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_family',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_cruise',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_flighttravel',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_group',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_roundtour',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_residence',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_hiking',
'value' => $keywords,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_lastminute',
'value' => $keywords,
'compare' => 'LIKE'
),
),
)
);
}
if($type !=""){
$wp_user_query = new WP_User_Query( array(
'order' => 'ASC',
'orderby' => 'display_name',
'meta_query' => array(
'relation' => 'OR',
array( 'key' => 'user_addons_type_allinclusive',
'value' => $type,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_flightonly',
'value' => $type,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_club',
'value' => $type,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_single',
'value' => $type,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_honeymoon',
'value' => $type,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_indivual',
'value' => $type,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_business',
'value' => $type,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_sport',
'value' => $type,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_adventure',
'value' => $type,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_family',
'value' => $type,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_cruise',
'value' => $type,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_flighttravel',
'value' => $type,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_group',
'value' => $type,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_roundtour',
'value' => $type,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_residence',
'value' => $type,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_hiking',
'value' => $type,
'compare' => 'LIKE'
),
array( 'key' => 'user_addons_type_lastminute',
'value' => $type,
'compare' => 'LIKE'
),
),
)
);
}
if($keywords !="" or $type !=""){
$authors = $wp_user_query->get_results();
// Check for results
if (!empty($authors)) {
$output .= '<ul class="author-list">';
// loop trough each author
foreach ($authors as $author)
{
// get all the user's data
$author_info = get_userdata($author->ID);
$output .= '<li><a href="'.get_bloginfo('home').'/author/'.$author->user_nicename.'">';
$output .= get_avatar( get_the_author_email(), '80' );
if(isset($author_info->first_name) and $author_info->first_name !=""){
$output .= '<span>'.$author_info->first_name." ";
}
if(isset($author_info->last_name) and $author_info->last_name !=""){
$output .= $author_info->last_name .'</span>';
if(isset($author_info->user_addons_zip) and $author_info->user_addons_zip !=""){
$output .= $author_info->user_addons_zip."&nbsp;";
}
if(isset($author_info->user_addons_city) and $author_info->user_addons_city !=""){
$output .= $author_info->user_addons_city." ";
}
}else{
$output .= $author->display_name;
}
$output .= '</a></li>';
}
$output .= '</ul>';
} else {
$output .= '<p>No authors found</p>';
}
}
return $output;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment