Wordpress Ultimate Member: show profile info on account page - wordpress

I made a custom registration form with a few extra fields using the ultimate member plugin. I want to display these extra fields on the account page. I already created an extra tab for it with hooks (see code below), but I can’t seem to find how to get the data from the extra fields of the register form. Does anyone has an idea?
Kind regards
Davy
/* Ultimate member */
/* tab Stormbee registration */
add_filter('um_account_page_default_tabs_hook', 'my_custom_tab_in_um', 100 );
function my_custom_tab_in_um( $tabs ) {
$tabs[800]['mytab']['icon'] = 'um-faicon-pencil';
$tabs[800]['mytab']['title'] = 'Stormbee registration';
$tabs[800]['mytab']['custom'] = true;
return $tabs;
}
/* make our new tab hookable */
add_action('um_account_tab__mytab', 'um_account_tab__mytab');
function um_account_tab__mytab( $info ) {
global $ultimatemember;
extract( $info );
$output = $ultimatemember->account->get_tab_output('mytab');
if ( $output ) { echo $output; }
}
/* Finally we add some content in the tab */
add_filter('um_account_content_hook_mytab', 'um_account_content_hook_mytab');
function um_account_content_hook_mytab( $output ){
ob_start();
?>
<div class="um-field">
<?php echo um_user('display_name'); ?> <br />
<?php
?>
</div>
<?php
$output .= ob_get_contents();
ob_end_clean();
return $output;
}
/* Ultimate member */

Ultimate Member stores the data from the fields in the User Meta table. In order to display the saved field values in the front end, you can use Wordpress's built-in get_user_meta function:
echo get_user_meta( $user_ID, $key, true );
You could use get_current_user_id() to retrieve the current user's ID. But since you are planning on displaying this on the Ultimate Member Account Page (which admins can access), I recommend you use UM()->user()->target_id instead. This will pull the ID of the user whose profile/account page you are viewing.
Next, all you need to do is figure out the key of the field. Usually, this is all lowercase with underscores. So something like first_name.
Here's what the final code would look like:
$user_ID = UM()->user()->target_id;
echo get_user_meta( $user_ID, 'description', true );

Related

wordpress: Display last record of Custom post type on home page

I am a novice developer of WordPress.
I created a custom post type wp_locations to practice.
It has three numeric fields that are entered as <input type="number">: a phone_number; a fax_number and a zip_code
$wp_location_phone = get_post_meta($post->ID,'wp_location_phone',true);
$wp_location_fax = get_post_meta($post->ID,'wp_location_fax',true);
$wp_location_zipcode = get_post_meta($post->ID,'wp_location_zipcode',true);
That works properly and stores the information.
I have a plugin to create the Custom Post Type and save a new record in WordPress.
Now I want to display this record on the home page in a table.
I searched but I could not run any suggestions. Please guide me with an example. Thanks
class wp_simple_location{
private $wp_location_trading_hour_days = array();
public function __construct(){
add_action('init', array($this,'set_location_trading_hour_days')); //sets the default trading hour days (used by the content type)
add_action('init', array($this,'register_location_content_type'));
add_action('add_meta_boxes', array($this,'add_location_meta_boxes'));
add_action('save_post_wp_locations', array($this,'save_location')); //save location
register_activation_hook(__FILE__, array($this,'plugin_activate'));
register_deactivation_hook(__FILE__, array($this,'plugin_deactivate'));
}
//display function used for our custom location meta box*/
public function location_meta_box_display($post){
wp_nonce_field('wp_location_nonce', 'wp_location_nonce_field');
//collect variables
$wp_location_phone = get_post_meta($post->ID,'wp_location_phone',true);
$wp_location_fax = get_post_meta($post->ID,'wp_location_fax',true);
$wp_location_zipcode = get_post_meta($post->ID,'wp_location_zipcode',true);
/*
// information is gathered here in a form. The relevant inputs are:
<input type="number" name="wp_location_phone" id="wp_location_phone" value=" <?php echo $wp_location_phone;"?> />
<input type="number" name="wp_location_fax" id="wp_location_fax" value=" <?php echo $wp_location_fax; ?>"/>
<input type="number" name="wp_location_zipcode" id="wp_location_zipcode" value=" <?php echo $wp_location_zipcode;" ?> />
*/
}
//triggered when adding or editing a location
public function save_location($post_id){
// nonce & autosave checks removed from example as they are irrelevant
//get our phone, email and address fields
$wp_location_phone = isset($_POST['wp_location_phone']) ? sanitize_text_field($_POST['wp_location_phone']) : '';
$wp_location_fax = isset($_POST['wp_location_fax']) ? sanitize_text_field($_POST['wp_location_fax']) : '';
$wp_location_zipcode = isset($_POST['wp_location_zipcode']) ? sanitize_text_field($_POST['wp_location_zipcode']) : '';
//update phone, email and address fields
update_post_meta($post_id, 'wp_location_phone', $wp_location_phone);
update_post_meta($post_id, 'wp_location_fax', $wp_location_fax);
update_post_meta($post_id, 'wp_location_zipcode', $wp_location_zipcode);
//search for our trading hour data and update
foreach($_POST as $key => $value){
//if we found our trading hour data, update it
if(preg_match('/^wp_location_trading_hours_/', $key)){
update_post_meta($post_id, $key, $value);
}
}
do_action('wp_location_admin_save',$post_id);
}
} // end class
Other notes:
The customer post type name is "wp_location" but the slug is "locations":
$args = array(
[...]
'rewrite' => array('slug' => 'locations', 'with_front' => 'true')
);
register_post_type('wp_locations', $args);
A quick Google search for "wp_simple_location" shows that you have taken this example from SitePoint and changed it.
Based on that, I'm going to amend the rest of that example to show you how to create a shortcode you can use.
Assumptions:
I presume the code you posts isn't your from your test site, as it is
not valid (e.g. you are missing many )
I'm also going to assume that you only have 1 location - the
sitepoint example allowed you add many but you appear to have removed
that code
Code:
1. Add back in the get_locations_output function.
You deleted the get_locations_output function, which is how you actually display the information - which is what you want! So you need to add it back into your wp_simple_location class. I have changed it so it should work with your changes
// main function for displaying locations (used for our shortcodes and widgets)
public function get_locations_output(){
//find locations
$location_args = array(
'post_type' => 'wp_locations',
'posts_per_page'=> 999,
'post_status' => 'publish'
);
//output
$html = '';
$locations = get_posts($location_args);
//if we have a location it will be returned in an array, so loop through it
if($locations){
$html .= '<article class="location_list cf">';
//foreach location
foreach($locations as $location){
$html .= '<section class="location">';
//collect location data
$wp_location_id = $location->ID;
$wp_location_phone = get_post_meta($wp_location_id,'wp_location_phone',true);
$wp_location_fax= get_post_meta($wp_location_id,'wp_location_fax',true);
$wp_location_zipcode= get_post_meta($wp_location_id,'wp_location_zipcode',true);
// output details only if any were entered
if($wp_location_phone || $wp_location_fax || $wp_location_zipcode){
$html .= '<table>';
if(!empty($wp_location_phone)){
$html .= '<tr><td>Phone: </td><td>' . $wp_location_phone . '</td></tr>';
}
if(!empty($wp_location_fax)){
$html .= '<tr><td>Fax: </td><td>' . $wp_location_fax. '</td></tr>';
}
if(!empty($wp_location_zipcode)){
$html .= '<tr><td>Zipcode: </td><td>' . $wp_location_zipcode. '</td></tr>';
}
$html .= '</table>';
}
//apply the filter after the main content, before it ends
//(lets third parties hook into the HTML output to output data)
$html = apply_filters('wp_location_after_main_content', $html);
$html .= '</section>';
}
$html .= '</article>';
}
return $html;
} // end function
**2. Create a new class that adds a shortcode to let you include the information in pages **
Note: Proper practice is create the new new class in a separate php file and include it in your wp_simple_location class file, but to keep this example simple, add this to the bottom of the file containing your wp_simple_location class:
class wp_location_shortcode{
//on initialize
public function __construct(){
add_action('init', array($this,'register_location_shortcodes')); //shortcodes
}
//location shortcode
public function register_location_shortcodes(){
add_shortcode('wp_locations', array($this,'location_shortcode_output'));
}
//shortcode display
public function location_shortcode_output($atts, $content = '', $tag){
//get the global wp_simple_locations class
global $wp_simple_locations;
//uses the main output function of the location class
$html = $wp_simple_locations->get_locations_output();
return $html;
}
}
$wp_location_shortcode = new wp_location_shortcode;
**3. Use the shortcode to include the information **
To include the information in any page, you just need to use the following shortcode in the post editor:
[wp_locations]
Note:
You should also clean up your plugin code - for example the code starting //search for our trading hour data and update is not needed as you deleted the hours from the plugin. Also make sure all your php tags are correct!
As I mentioned, I have just changed the SitePoint example to work with your changes, so I haven't tested any of this. If you have any questions it might be a good idea to re-read the SitePoint tutorial because it explains the code in detail.

Function to Change Page Title In Wordpress

I am using the below function to change the page title in wordpress. It is working on a single page (page.php), but is not working on my static home page or individual posts (single.php).
What do I need to change in order to make this work across the entire site?
<?php
function wpse46249_filter_wp_title( $title ) {
$some_custom_title_content = 'This is added to title';
$custom_title = $title . $some_custom_title_content;
return $custom_title;
}
add_filter( 'wp_title', 'wpse46249_filter_wp_title' );
?>
function wpse46249_filter_wp_title( $title ) {
$some_custom_title_content = 'This is added to title';
$custom_title = $title . $some_custom_title_content;
return $custom_title;
}
add_filter( 'the_title', 'wpse46249_filter_wp_title' );
please note the title is saved in wp_posts table. The filter you used above will change all new posts titles being saved. This filter just modifys the title after pulling it from the db and doesn't actually change the db value.
Also will only work on pages where the_title() is called.

how to save meta box value?

I've created a meta box. The code is:
// Create your custom meta box
add_action( 'add_meta_boxes', 'hotel_amenities' );
// Add a custom meta box to a post
function hotel_amenities( $post ) {
add_meta_box(
'Meta Box Amenities', // ID, should be a string
'Amenities', // Meta Box Title
'amenities_content', // Your call back function, this is where your form field will go
'post', // The post type you want this to show up on, can be post, page, or custom post type
'normal', // The placement of your meta box, can be normal or side
'high' // The priority in which this will be displayed
);
}
// Content for the custom meta box
function amenities_content( $post ) {
echo '<label>Bed room</label>';
echo '<input type="text" name="amenity_bed_room" value="" />';
}
// Save your meta box content
add_action( 'save_post', 'save_amenities' );
// save newsletter content
function save_amenities(){
global $post;
// Get our form field
if( $_POST ) :
$amenities_meta = esc_attr( $_POST['amenity_bed_room'] );
// Update post meta
update_post_meta($post->ID, '_amenities_custom_meta', $amenities_meta);
endif;
}
It shows a meta box on admin post page with a text field. but it gets blank if I save or update the post after I put some thing on the text field.
Seems function save_amenities() is not working. What I am doing wrong in this code?
Also for getting that value I use the function below. Is that correct?
//get amenities meta box values
function get_amenities_meta_box() {
global $post;
$meta_values = get_post_meta($post->ID, '_amenities_custom_meta', true);
}
There are a few things going wrong there. The final value that you want to see will be displayed by the value attribute in the amenities_content function. Right now it is just displaying an empty string (""). Try putting any value in that attribute and you should see it show up in the meta box (value="this is a test").
The save_amenities function should take $post_id as a parameter. You'll need that to update the post meta-data and give a real value for the amenities_content function to echo back to the admin screen.
The amenities_content function should really have a nonce field that should then be verified by the save_amenities function. And user input should be sanitized before it is saved (I'm doing it both when I save it and when I display it. I'm not sure if that's necessary.)
try this out for the amenities_content function:
function amenities_content( $post ) {
// This is the value that was saved in the save_amenities function
$bed_room = get_post_meta( $post->ID, '_amenity_bed_room', true );
wp_nonce_field( 'save_amenity', 'amenity_nonce' );
echo '<label>Bed room</label>';
echo '<input type="text" name="amenity_bed_room"
value="' . sanitize_text_field( $bed_room ) . '" />';
}
and this for the save_amenities function:
function save_amenities( $post_id ) {
// Check if nonce is set
if ( ! isset( $_POST['amenity_nonce'] ) ) {
return $post_id;
}
if ( ! wp_verify_nonce( $_POST['amenity_nonce'], 'save_amenity' ) ) {
return $post_id;
}
// Check that the logged in user has permission to edit this post
if ( ! current_user_can( 'edit_post' ) ) {
return $post_id;
}
$bed_room = sanitize_text_field( $_POST['amenity_bed_room'] );
update_post_meta( $post_id, '_amenity_bed_room', $bed_room );
}

Use postmeta in wordpress shortcode

Trying to get my post meta from posts using shrotcodes and then displaying it on the content.This is the code that's trying to do this:
$string = '';
$custom_content = get_post_custom($post->ID);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$content = get_the_content();
$bonus = $custom_content["bonus"];
$string .= $content . $bonus . '<br>';
endwhile;
}
return $string;
It's not working as the custom content returns empty. Whats wrong? Thanks in advance
I don't think you got the shortcode idea, you should read some info about add_shortcode() also you can use get_post_meta() to retriev the metadata from the database.
Here is an example on how you can achieve this but this will only work with the main loop (as default):
<?php
//you can put this code in functions.php or you can build a plugin for it
function metadata_in_content($attr) {
//this is the function that will be triggerd when the code finds the proper shortcode in the content; $attr is the parameter passed throw the shortcode (leave null for now)
global $wpdb, $wp_query;
//we need global $wpdb to query the database and to get the curent post info
if (is_object($wp_query->post)) {
$post_id = $wp_query->post->post_id;// here we save the post id
$metadata = get_post_meta( $post_id, $key, $single ); // here we get the needed meta, make sure you place the correct $key here, also if you don't want to get an array as response pass $single as "true"
return $metadata; // this finally replaces the shortcode with it's value
}
}
add_shortcode('insert_metadata', 'metadata_in_content');
//the above code hooks the metadata_in_content function to the [insert_metadata] shortcode
?>
Now all it's left to do is to place [insert_metadata] in the post content and things should work.

Change the page title using a short code

I am coding a plugin and I use a short code to render a page content. I want to change the page title. I used this:
// Change the title of the page of the charts to add the current month.
add_filter('the_title', 'charts_title', 10, 2);
function charts_title($title, $id) {
$title .= ' ' . date('F Y');
return $title;
}
But it does that for all the posts and pages. Can I do that only for the pages that contains the short code I created ? I tried to do that, but it doesn't work.
add_shortcode('charts-vote', 'charts_vote');
function charts_vote($atts) {
// Add the filter only in the short code function callback.
add_filter('the_title', 'charts_title', 10, 2);
// ... //
return $content;
}
Can someone please help me ?
I understand that the specifics of your set up may require checking for the Shortcode, but maybe a Custom Field could be used for that:
add_filter( 'the_title', 'charts_title_so_15312385', 10, 2 );
function charts_title_so_15312385( $title, $post_id )
{
// Admin area, bail out
if( is_admin() )
return $title;
// Custom field not set, bail out
$mod_title = get_post_meta( $post_id, 'mod_title', true );
if( !$mod_title )
return $title;
// Ok, modify title
$title .= ' ' . date( 'F Y' );
return $title;
}
The Shortcode could even "talk" with the Custom Field for extended configurations.
For ease of use, you could make a Custom Meta Box or use a plugin like Advanced Custom Fields.

Resources