Custom taxonomy & images fields on user profile.
Support › MB User Meta › Custom taxonomy & images fields on user profile.
- This topic has 1 reply, 2 voices, and was last updated 3 years, 7 months ago by
Long Nguyen.
-
AuthorPosts
-
September 21, 2021 at 5:44 PM #30871
PAUL TUNNICLIFFE
ParticipantI have built custom fields attached to the user profile
<?php add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' ); function your_prefix_function_name( $meta_boxes ) { $prefix = ''; $meta_boxes[] = [ 'title' => __( 'User Dealer Details', 'your-text-domain' ), 'id' => 'user-to-dealer-rvtbbh8q', 'type' => 'user', 'fields' => [ [ 'name' => __( 'Trade Dealer', 'your-text-domain' ), 'id' => $prefix . 'switch_trade_dealer_k1gbbek8ui', 'type' => 'switch', 'style' => 'rounded', 'columns' => 1, ], [ 'name' => __( 'Dealer Company Name', 'your-text-domain' ), 'id' => $prefix . 'dealer_company_name_g8WJiS1A', 'type' => 'text', 'columns' => 8, 'visible' => [ 'when' => [['switch_trade_dealer_k1gbbek8ui', '=', 1]], 'relation' => 'and', ], ], [ 'name' => __( 'Company Logo', 'your-text-domain' ), 'id' => $prefix . 'image_company_logo_90ys6lit', 'type' => 'single_image', 'image_size' => 'large', 'columns' => 4, 'visible' => [ 'when' => [['switch_trade_dealer_k1gbbek8ui', '=', 1]], 'relation' => 'and', ], ], [ 'name' => __( 'Company Banner Image', 'your-text-domain' ), 'id' => $prefix . 'single_banner_image_d9taj2h1', 'type' => 'single_image', 'image_size' => 'large', 'columns' => 4, 'visible' => [ 'when' => [['switch_trade_dealer_k1gbbek8ui', '=', 1]], 'relation' => 'and', ], ], [ 'type' => 'divider', 'visible' => [ 'when' => [['switch_trade_dealer_k1gbbek8ui', '=', 1]], 'relation' => 'and', ], ], [ 'name' => __( 'Address 1', 'your-text-domain' ), 'id' => $prefix . 'text_address_1_li90xxy1', 'type' => 'text', 'columns' => 3, 'visible' => [ 'when' => [['switch_trade_dealer_k1gbbek8ui', '=', 1]], 'relation' => 'and', ], ], [ 'name' => __( 'Address 2', 'your-text-domain' ), 'id' => $prefix . 'text_address_2_o3g2h0bz', 'type' => 'text', 'columns' => 3, 'visible' => [ 'when' => [['switch_trade_dealer_k1gbbek8ui', '=', 1]], 'relation' => 'and', ], ], [ 'name' => __( 'Town', 'your-text-domain' ), 'id' => $prefix . 'text_town_ia97rc0d', 'type' => 'text', 'admin_columns' => 'after title', 'columns' => 3, 'visible' => [ 'when' => [['switch_trade_dealer_k1gbbek8ui', '=', 1]], 'relation' => 'and', ], ], [ 'name' => __( 'County', 'your-text-domain' ), 'id' => $prefix . 'text_county_2j9juaxo', 'type' => 'text', 'columns' => 3, 'visible' => [ 'when' => [['switch_trade_dealer_k1gbbek8ui', '=', 1]], 'relation' => 'and', ], ], [ 'name' => __( 'Postcode', 'your-text-domain' ), 'id' => $prefix . 'text_postcode_8kh9n7sb', 'type' => 'text', 'columns' => 3, 'visible' => [ 'when' => [['switch_trade_dealer_k1gbbek8ui', '=', 1]], 'relation' => 'and', ], ], [ 'name' => __( 'Country', 'your-text-domain' ), 'id' => $prefix . 'tax_country_oggm418p', 'type' => 'taxonomy_advanced', 'taxonomy' => ['country'], 'field_type' => 'select_advanced', 'columns' => 3, 'visible' => [ 'when' => [['switch_trade_dealer_k1gbbek8ui', '=', 1]], 'relation' => 'and', ], ], [ 'type' => 'divider', 'visible' => [ 'when' => [['switch_trade_dealer_k1gbbek8ui', '=', 1]], 'relation' => 'and', ], ], [ 'name' => __( 'Telelphone', 'your-text-domain' ), 'id' => $prefix . 'phone_telelphone_d5s0rwx5', 'type' => 'tel', 'admin_columns' => 'after text_town_ia97rc0d', 'columns' => 3, 'visible' => [ 'when' => [['switch_trade_dealer_k1gbbek8ui', '=', 1]], 'relation' => 'or', ], ], [ 'name' => __( 'Mobile', 'your-text-domain' ), 'id' => $prefix . 'phone_mobile_oy4vff78', 'type' => 'tel', 'columns' => 3, 'visible' => [ 'when' => [['switch_trade_dealer_k1gbbek8ui', '=', 1]], 'relation' => 'and', ], ], [ 'name' => __( 'Email', 'your-text-domain' ), 'id' => $prefix . 'email_email_slrokf3j', 'type' => 'email', 'admin_columns' => 'after phone_telelphone_d5s0rwx5', 'columns' => 3, 'visible' => [ 'when' => [['switch_trade_dealer_k1gbbek8ui', '=', 1]], 'relation' => 'and', ], ], [ 'name' => __( 'Website', 'your-text-domain' ), 'id' => $prefix . 'url_website_fwv4avtxdjg', 'type' => 'url', 'placeholder' => __( 'https://www.example.co.uk', 'your-text-domain' ), 'admin_columns' => 'after email_email_slrokf3j', 'columns' => 3, 'visible' => [ 'when' => [['switch_trade_dealer_k1gbbek8ui', '=', 1]], 'relation' => 'and', ], ], [ 'type' => 'divider', 'visible' => [ 'when' => [['switch_trade_dealer_k1gbbek8ui', '=', 1]], 'relation' => 'and', ], ], [ 'name' => __( 'Description', 'your-text-domain' ), 'id' => $prefix . 'text_description_850p4eR1', 'type' => 'textarea', 'visible' => [ 'when' => [['switch_trade_dealer_k1gbbek8ui', '=', 1]], 'relation' => 'and', ], ], ], ]; return $meta_boxes; }
My template file is
<?php /* Template Name: Users List This template queries the wordpress users and returns all with the switch on for Trade Dealer. */ get_header(); ?> <style> /* Tidy List */ li, ul { text-decoration: none; list-style: none; padding: 0px; margin: 0px; } /* Page Loyout */ header { margin-bottom: 20px; } article { padding: 10px; } loopcontainer.loopcontainer { border-color: black; border-width: 1px; border-style: solid; border-radius: 5px; width: 100%; display: block; margin-bottom: 15px; } loop-header { padding: 10px; text-align: center; font-weight: 800; font-size: larger; width: 100%; display: block; } .column { float: left; width: 31%; margin: 1%; height: fit-content; } loop-header { padding: 10px; text-align: center; font-weight: 800; font-size: larger; background-image: url(https://dev.busandcoachbuyer.com/2021/09/1096598….gif); } column-container { display: flex; } .dealer-address, .dealer-postcode, .dealer-country, .dealer-communications, .dealer-country { float: none; width: 100%; display: block; } /* Tidy Text <em>/ .dealer-address:after { content: ", "; } .dealer-address, .dealer-postcodem, .dealer-country, .dealer-communications { color: black; } loop-header { color: var(--e-global-color-primary); } .dealer-communications:before { font-weight: 800; color: var(--e-global-color-primary); } dealer-phone:before { content: "Tel: "; } dealer-mobile:before { content: "Mob: "; } dealer-email:before { content: "Email: "; } dealer-website:before { content: "Web: "; } /</em> Hide the :before text if field is empty */ .dealer-address:empty, .dealer-country:empty, .dealer-communications:empty { display: none; } </style> <div id="primary" class="site-content"> <div id="content" role="main"> <article> <header class="entry-header"> <div class="elementor-element elementor-element-d985ca8 elementor-widget elementor-widget-heading" data-id="d985ca8" data-element_type="widget" data-widget_type="heading.default"> <div class="elementor-widget-container"> <h2 class="elementor-heading-title elementor-size-default">Dealer Directory</h2> </div> </div> </header> <div class="entry-content"> <ul id="ulfriends"> <?php $no=12;// total no of author to display $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; if($paged==1){ $offset=0; }else { $offset= ($paged-1)*$no; } $user_query = new WP_User_Query( array( 'meta_key' => 'switch_trade_dealer_k1gbbek8ui', 'meta_value' => 1 ) ); if ( ! empty( $user_query->results ) ) { foreach ( $user_query->results as $user ) { echo "<li class=dealer-list>" . "<tr align='center'>" . "<company-name class=dealer-info><td>" . $user->single_banner_image_d9taj2h1 . "</td></company-name>" . "<company-name class=dealer-info><td>" . $user->image_company_logo_90ys6lit . "</td></company-name>" . "</tr>" . "<loopcontainer class=loopcontainer> <loop-header>" . $user->dealer_company_name_g8WJiS1A . "</loop-header> <column-container> <column1 class=column> LOGO TO GO HERE </column1> <column2 class=column> <address-1 class=dealer-address>" . $user->text_address_1_li90xxy1 . "</address-1> <address-2 class=dealer-address>" . $user->text_address_2_o3g2h0bz . "</address-2> <town class=dealer-address>" . $user->text_town_ia97rc0d . "</town> <county class=dealer-address>" . $user->text_county_2j9juaxo . "</county> <postcode class=dealer-postcode>" . $user->text_postcode_8kh9n7sb . "</postcode> <country class=dealer-country>" . $user->tax_country_oggm418p . "</country> <?php $term2 = get_term( $user->tax_country_oggm418p, country ); echo $term2 ?> </column2> <column3 class=column> <dealer-phone class=dealer-communications>" . $user->phone_telelphone_d5s0rwx5 . "</dealer-phone> <dealer-mobile class=dealer-communications>" . $user->phone_mobile_oy4vff78 . "</dealer-mobile> <dealer-email class=dealer-communications>" . $user->email_email_slrokf3j . "</dealer-email> <dealer-website class=dealer-communications>" . $user->url_website_fwv4avtxdjg . "</dealer-website> </column3> </column-container> <base-container> <company-description class=dealer-description><td>" . $user->text_description_850p4eR1A . "</td></company-description> </base-container> </loopcontainer>" . "</li>" ; } } else { echo 'No users found.'; } ?> </ul> <?php $total_user = $user_query->total_users; $total_pages=ceil($total_user/$no); echo paginate_links(array( 'base' => get_pagenum_link(1) . '%_%', 'format' => '?paged=%#%', 'current' => $paged, 'total' => $total_pages, 'prev_text' => 'Previous', 'next_text' => 'Next', 'type' => 'list', )); ?> </div> </article> </div><!-- #content --> </div><!-- #primary --> <?php get_sidebar(); ?> <?php get_footer(); ?>
No I need to return the the attached term for the image fields and the taxonomy value for the country field. Currently the country field returns the taxonomy id.
I have reviewed
https://docs.metabox.io/displaying-fields/
https://docs.metabox.io/extensions/mb-user-meta/#getting-field-value
https://developer.wordpress.org/reference/functions/get_term/But am now going around in circles!
September 21, 2021 at 9:56 PM #30876Long Nguyen
ModeratorHi,
To show the user meta image, you can use this code
$image = rwmb_meta( 'image', ['object_type' => 'user'], $user->ID ); //in your case user ID should be $user->ID echo '<img src='. $image['url'] .' />';
to show the term name (country), you can use this code
$country = rwmb_meta( 'tax_country_oggm418p', ['object_type' => 'user'], $user->ID ); echo $country->name;
Please get more details on two fields document
https://docs.metabox.io/fields/single-image/#template-usage
https://docs.metabox.io/fields/taxonomy/#template-usage -
AuthorPosts
- You must be logged in to reply to this topic.