Ultimate Member is unable to output Post Title with Shortcode
-
Hi! I am using a shortcode in my Post Titles. The shortcode works great in all of my pages except the Ultimate Member User Profile page.
How do I activate or allow shortcodes in Post Title?
-
This topic was modified 4 years ago by
Hazel Z..
-
This topic was modified 4 years ago by
-
Hi @cadencehart
Could you please provide a screenshot of the Post Title you’re referring to?
Regards,
I am using Ultimate Member and WP User Frontend together. WP User Frontend takes care of the frontend posting and Ultimate Member takes care of content and page restrictions.
On my WP User Frontend form that users use to create and publish a post, the default value of the Post Title field is a shortcode.
Ultimate Member is unable to read this shortcode and outputs the Post Title this way – [name_of_shortcode] – on the user profile page.
I found that UM is actually reading and outputting the shortcode. The problem is that UM is not reading the custom meta key values from the WP Frontend Form.
Here is my code in functions.php
//* Post Title Shortcode add_shortcode( 'date_of_progress_report', 'displayPostTitle'); function displayPostTitle() { global $post; $dayoftheweek = get_post_meta( get_the_ID(), 'day_of_the_week', true ); // old setting $monthoftheyear = get_post_meta( get_the_ID(), 'month_of_the_year', true ); //new setting $dayoftheweek2 = get_post_meta( get_the_ID(), 'day_of_the_week_2', true ); // new setting $yearnow = get_post_meta( get_the_ID(), 'year_now', true ); // new setting if ( !empty($monthoftheyear) ) { // new setting // Convert Date String if ($monthoftheyear == "January") { $monthnow = '01'; } if ($monthoftheyear == "February") { $monthnow = '02'; } if ($monthoftheyear == "March") { $monthnow = '03'; } if ($monthoftheyear == "April") { $monthnow = '04'; } if ($monthoftheyear == "May") { $monthnow = '05'; } if ($monthoftheyear == "June") { $monthnow = '06'; } if ($monthoftheyear == "July") { $monthnow = '07'; } if ($monthoftheyear == "August") { $monthnow = '08'; } if ($monthoftheyear == "September") { $monthnow = '09'; } if ($monthoftheyear == "October") { $monthnow = '10'; } if ($monthoftheyear == "November") { $monthnow = '11'; } if ($monthoftheyear == "December") { $monthnow = '12'; } //Our YYYY-MM-DD date string. $date = "2002-12-02"; $date = $yearnow .'-'.$monthnow .'-'. $day_of_the_week_2; //Convert the date string into a unix timestamp. $unixTimestamp = strtotime($date); //Get the day of the week using PHP's date function. $nameOfDay = date("l", $unixTimestamp); $post->post_title = $monthoftheyear .' '. $dayoftheweek2 .', '. $yearnow .' ('. $nameOfDay .')'; return $post->post_title; } elseif ( !empty($dayoftheweek) ) { // old setting return $post->post_title .' ('. $dayoftheweek .')'; } else { return 'Test'; } }
The output is Test… which means that UM is not reading any of the custom fields at all. @champsupertramp
Hi @cadencehart
I’m confused as to how this will be rendered. Are you using the posts-single.php template to render this? Can you please provide a screenshot of the profile where it displays the “Test” so I can see it clearly?
Regards,
@champsupertramp This is code from my posts-single.php file (which is created to override the default plugin file):
<div class="um-item-link"> <i class="um-icon-ios-paper"></i> <a href="<?php echo esc_url( get_permalink( $post ) ); ?>"><?php echo do_shortcode('[date_of_progress_report]'); ?></a> </div>
Note that instead of the $post->post_title, it is set to output the shortcode [date_of_progress_report]. The shortcode function in functions.php is:
//* Post Title Shortcode add_shortcode( 'date_of_progress_report', 'displayPostTitle'); function displayPostTitle() { global $post; $dayoftheweek = get_post_meta( get_the_ID(), 'day_of_the_week', true ); // old setting $monthoftheyear = get_post_meta( get_the_ID(), 'month_of_the_year', true ); //new setting $dayoftheweek2 = get_post_meta( get_the_ID(), 'day_of_the_week_2', true ); // new setting $yearnow = get_post_meta( get_the_ID(), 'year_now', true ); // new setting if ( !empty($monthoftheyear) ) { // new setting // Convert Date String if ($monthoftheyear == "January") { $monthnow = '01'; } if ($monthoftheyear == "February") { $monthnow = '02'; } if ($monthoftheyear == "March") { $monthnow = '03'; } if ($monthoftheyear == "April") { $monthnow = '04'; } if ($monthoftheyear == "May") { $monthnow = '05'; } if ($monthoftheyear == "June") { $monthnow = '06'; } if ($monthoftheyear == "July") { $monthnow = '07'; } if ($monthoftheyear == "August") { $monthnow = '08'; } if ($monthoftheyear == "September") { $monthnow = '09'; } if ($monthoftheyear == "October") { $monthnow = '10'; } if ($monthoftheyear == "November") { $monthnow = '11'; } if ($monthoftheyear == "December") { $monthnow = '12'; } //Our YYYY-MM-DD date string. $date = "2002-12-02"; $date = $yearnow .'-'.$monthnow .'-'. $day_of_the_week_2; //Convert the date string into a unix timestamp. $unixTimestamp = strtotime($date); //Get the day of the week using PHP's date function. $nameOfDay = date("l", $unixTimestamp); $post->post_title = $monthoftheyear .' '. $dayoftheweek2 .', '. $yearnow .' ('. $nameOfDay .')'; return $post->post_title; } elseif ( !empty($dayoftheweek) ) { // old setting return $post->post_title .' ('. $dayoftheweek .')'; } else { return 'Test'; } }
Note that at the bottom of the code, I set it to return ‘Test’… so that if Ultimate Member is reading and outputting the shortcode but without acquiring the custom meta key values ($monthoftheyear, $dayoftheweek, $dayoftheweek2, $yearnow)… the post titles on the UM User Profile Page will be displayed as “Test”.
Upon checking the UM User Profile Page… the post titles are indeed displaying as “Test” (instead of outputting the values of $monthoftheyear, $dayoftheweek, $dayoftheweek2, $yearnow). Here is the screenshot:
https://drive.google.com/file/d/1R3LNfiNQSJ0MRPdm1APGfNRUfkjMVY1S/view?usp=sharing
UM is not able to recognize these variables — $monthoftheyear, $dayoftheweek, $dayoftheweek2, $yearnow.
How do I make UM recognize these variables so it can output these custom meta keys?
Hi @cadencehart
Can you please try dumping this in the shortcode and see if it is returning correct post ID?
add_shortcode( 'date_of_progress_report', 'displayPostTitle'); function displayPostTitle() { global $post; var_dump( $post->ID." = ". get_the_ID() ); }
Regards,
@champsupertramp I did as you asked. It showed that the correct post IDs are not returning correctly. The page id is returning instead. Please see screenshot:
https://drive.google.com/file/d/1Vd55GmVQktu5OcNsFPSGhFMxfRYBcJ9E/view?usp=sharing
Hi @cadencehart
Have you tried adding a parameter to your shortcode?
function date_of_progress_report( $atts ) { var_dump( $atts['post_id'] ); } add_shortcode( 'date_of_progress_report', 'date_of_progress_report' );
And in the title, you will use the shortcode like this:
<a href="<?php echo esc_url( get_permalink( $post ) ); ?>"><?php echo do_shortcode('[date_of_progress_report post_id='.$post->ID.']'); ?></a>
Let’s see if this will return the correct IDs.
Regards,
Hi @cadencehart
Please feel free to re-open this thread by changing the Topic Status to ‘Not Resolved’ if any other questions come up and we’d be happy to help. ??Regards,
Hi @champsupertramp. Ultimate Member is still not detecting my custom meta keys. The posts on the Profile page are outputted as:
string(3) “654” Test
string(3) “631” Test
string(3) “629” TestIt outputs the correct post ID but the title still shows “Test” which means the custom meta keys are not being read.
-
This reply was modified 4 years ago by
Hazel Z..
Hi @cadencehart
Have you tried the following?
function date_of_progress_report( $atts ) { return get_the_title( $atts['post_id'] ); } add_shortcode( 'date_of_progress_report', 'date_of_progress_report' );
Regards,
Hi @cadencehart
This thread has been inactive for a while so we’re going to go ahead and mark it Resolved.
Please feel free to re-open this thread by changing the Topic Status to ‘Not Resolved’ if any other questions come up and we’d be happy to help. ??
Regards,
-
This reply was modified 4 years ago by
- The topic ‘Ultimate Member is unable to output Post Title with Shortcode’ is closed to new replies.