SPONSOR ADS

Wednesday, 11 October 2017

Add a Status Text Next to Your WordPress Post Title

In this instructional exercise I will demonstrate to you how you can add a status message alongside the post title in view of a condition. 

There are two diverse ways you can deal with this: 

Demonstrate the status message progressively when WordPress is rendering the post title. 

Refresh the post title message in the wp_posts database table utilizing a cronjob. 

For this instructional exercise we will demonstrate a "Terminated" message before the post title if the post's distributed date is more seasoned than 14 days. A similar idea can be connected to do any sort of contingent check and refresh the post title appropriately. 

Technique #1) Dynamically Update the Post Title 

We can utilize the "the_title" channel to change the post title when it is being rendered. 

The accompanying code case demonstrates to do it (read the remark in the code for clarification): 


add_filter('the_title', 'tthq_add_expired_status_to_title'); 

work tthq_add_expired_status_to_title($title) { 

/Check if this is a page or not 

in the event that (is_page()){ 

/We would prefer not to do it for pages. 

return $title; 



/Check if this is a connection 

on the off chance that (is_attachment()){ 

/is_single() can be valid for connection so it regards check is_attachment() 

return $title; 



/Check on the off chance that we are insider savvy 

on the off chance that (!in_the_loop()){ 

return $title; 



/Initialize the date objects 

$created = new DateTime(get_the_date('Y-m-d g:i:s')); 

$current = new DateTime(date('Y-m-d g:i:s')); 

/The date_diff objects 

$created_to_today = date_diff($created, $current); 

/Check if the post distributed date is more seasoned than 14 days. 

$has_expired = ($created_to_today && $created_to_today->days > 14 ) ? genuine : false; 

/Adds status message before the post title 

if($has_expired){ 

/Modify the post title to include the terminated message in there. 

$markup = 'Terminated'; 

$title = $markup . " . $title; 



/Return post title 

return $title; 



You can likewise make it one stride advance by gathering a custom incentive in the post manager at that point utilizing that incentive in the above code. This instructional exercise demonstrates to you how you can include a meta enclose the post manager so you can spare custom incentive for each of your posts. 

Technique #2) Update Post Title in the Database 

You can make a little module that makes a "day by day" or "week by week" cronjob for your site. At the point when the cronjob is activated, you can do your check at that point refresh the post title utilizing the wp_update_post() work. 

The following is a case of how you can plan a day by day cronjob when your custom module is initiated: 

work tthq_custom_plugin_activate() { 

wp_schedule_event(time(), 'day by day', 'tthq_custom_daily_cron_event'); 



register_activation_hook(__FILE__, 'tthq_custom_plugin_activate'); 

You will then compose code in the tthq_custom_daily_cron_event() work which will do the checks in a circle and refresh post title in like manner. 

The accompanying code square demonstrates to a case of proper methodologies to refresh the post title in the wp_posts database table: 

/Lets say we need to refresh the post title of post ID 20 

$my_post = exhibit( 

"ID" => 20, 

"post_title" => 'This is the UPDATED post title.', 

); 

/Update the post title in the database 

wp_update_post( $my_post ); 

Check the WordPress codex page to take in more about the wp_update_post() work.