Advertisement
mbis

Regenerate the listing URI after it is added

Jan 23rd, 2019
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.64 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Please paste it to functions.php file in your child theme folder
  4.  */
  5. function pm_assign_listing_uri($post_ID, $post, $update) {
  6.     wp_schedule_single_event(time() + 60, 'pm_regenerate_uris_after_listing_added', array($post_ID));
  7. }
  8. add_action('wp_insert_post', 'pm_assign_listing_uri', 10, 1);
  9.  
  10. function pm_regenerate_uri($post_id) {
  11.     global $permalink_manager_uris;
  12.    
  13.     $permalink_manager_uris[$post_id] = Permalink_Manager_URI_Functions_Post::get_default_post_uri($post_id);
  14.     update_option('permalink-manager-uris', $permalink_manager_uris);
  15. }
  16. add_action('pm_regenerate_uris_after_listing_added', 'pm_regenerate_uri', 10, 1);
  17.  
  18. /**
  19.  * Support %listing-type% and %listing_type% tag
  20.  */
  21. function pm_replace_listing_type($uri, $post_name, $post, $slug, $native) {
  22.     if($post->post_type == 'job_listing') {
  23.         $listing_type = get_post_meta($post->ID, '_case27_listing_type', true);
  24.        
  25.         $uri = str_replace(array('%listing-type%', '%listing_type%'), $listing_type, $uri);
  26.     }
  27.  
  28.     return $uri;
  29. }
  30. add_filter('permalink_manager_filter_default_post_uri', 'pm_replace_listing_type', 9, 5);
  31.  
  32. /**
  33.  * Fix the problem with pending/draft listing URLs
  34.  */
  35. function pm_pending_listing_url($url, $post) {
  36.     global $permalink_manager_uris;
  37.    
  38.     if(class_exists('Permalink_Manager_Helper_Functions') && $post->post_type == 'job_listing' && $post->post_status !== 'publish' && !empty($permalink_manager_uris[$post->ID])) {
  39.         $url = sprintf('%s/%s', Permalink_Manager_Helper_Functions::get_permalink_base($post), $permalink_manager_uris[$post->ID]);
  40.        
  41.         // Control trailing slashes
  42.         $url = Permalink_Manager_Core_Functions::control_trailing_slashes($url);
  43.     }
  44.    
  45.     return $url;
  46. }
  47. add_filter('post_type_link', 'pm_pending_listing_url', 100, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement