Advertisement
mbis

Permalink Manager - Custom Landing Pages Taxonomy Permalinks

Dec 6th, 2022
1,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.64 KB | None | 0 0
  1.  /**
  2.   * Rewrite listing taxonomy permalinks (if custom landing page selected)
  3.   **/
  4. function pm_rewrite_listing_tax_permalinks($link, $term, $taxonomy) {
  5.     if(strpos($taxonomy, 'job_listing') !== false && function_exists('get_field')) {
  6.         // A. Use "Custom Landing Page" ACF field
  7.         $region_page = get_field('_landing_page', $term->taxonomy . '_' . $term->term_id);
  8.        
  9.         // Change the term permalink to page permalink
  10.         if(!empty($region_page->ID)) {
  11.             $region_page_link = get_permalink($region_page->ID);
  12.         }
  13.     }
  14.    
  15.     return (!empty($region_page_link)) ? $region_page_link : $link;
  16. }
  17. add_filter('term_link', 'pm_rewrite_listing_tax_permalinks', 1000, 3);
  18.  
  19. /**
  20.  * Redirect listing category permalinks (if custom landing page selected)
  21.  **/
  22. function pm_redirect_listing_tax_permalinks($query, $old_query, $uri_parts, $pm_query, $content_type) {
  23.     if(!empty($pm_query['id']) && strpos($content_type, 'job_listing') !== false && strpos($content_type, 'Taxonomy:') !== false && function_exists('get_field')) {
  24.         $taxonomy = preg_replace('/(.+?)([\S]+)$/i', '$2', $content_type);
  25.         $term_id = preg_replace('/(tax-)([\S]+)$/i', '$2', $pm_query['id']);
  26.        
  27.         $region_page = get_field('_landing_page', $taxonomy . '_' . $term_id);
  28.        
  29.         // If custom landing page is set trigger the redirect from term URL
  30.         if(!empty($region_page)) {
  31.             $region_page_link = get_permalink($region_page->ID);
  32.            
  33.             if($region_page_link) {
  34.                 wp_safe_redirect($region_page_link, 301);
  35.                 exit();
  36.             }
  37.         }
  38.     }
  39.    
  40.     return $query;
  41. }
  42. add_filter('permalink_manager_filter_query', 'pm_redirect_listing_tax_permalinks', 3, 5);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement