Advertisement
bcworkz

tg-add-to-functions.php

Jun 16th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.96 KB | None | 0 0
  1. /**
  2.  * Add the following lines to the end of existing add_custom_taxonomies() function declaration
  3.   add_rewrite_tag('%location%', '^([^&/]+)', 'location=');
  4.   add_rewrite_rule('^([^/]*)/([^/]*)/?','index.php?location=$matches[1]&name=$matches[2]','top');
  5.  *
  6.  * The permalink structure must be similar to http://domain.com/%location%/%postname%/
  7.  *
  8.  * Always go to Settings > Permalinks and click Save Changes when altering any rewrite code
  9.  *
  10.  * Additionally, all pages MUST be children of either home page named "benelux" or "worldwide"
  11.  *   in addition to having a similar term assigned from the "location" taxonomy
  12.  */
  13.  
  14. //strip page query vars from cat and tag requests, allow region home pages
  15. add_filter('request', 'tg_show_query_vars');
  16. function tg_show_query_vars( $vars ) {
  17.    // All regional home page slugs must be in this array
  18.    $regions = array('benelux', 'worldwide',);
  19.    if ( array_key_exists('location', $vars ) && in_array( $vars['location'], $regions ) &&
  20.          (( array_key_exists('category_name', $vars ) && ''!= $vars['category_name']) ||
  21.          ( array_key_exists('tag', $vars ) && ''!= $vars['tag']))
  22.       ) {
  23.       unset( $vars['location']);
  24.    }
  25.    // Handle region home page requests
  26.    if ( ! array_key_exists('pagename', $vars ) && ! array_key_exists('name', $vars ) &&
  27.          array_key_exists('location', $vars ) && in_array( $vars['location'], $regions )
  28.       ) {
  29.       $vars['pagename'] = $vars['location'];
  30.       unset( $vars['location']);
  31.    }
  32.    return $vars;
  33. }
  34.  
  35. // add region term to main queries
  36. add_action('pre_get_posts', 'tg_set_region');
  37. function tg_set_region( $query ) {
  38.   if ( get_option('page_on_front') == $query->get('page_id')) return;
  39.   if ( !is_admin() && $query->is_main_query()) {
  40.     if (''== $query->get('post_type') && ''== $query->get('tag') && ''== $query->get('category_name'))
  41.       $query->set('post_type', array('post', 'page'));
  42.     global $region;
  43.     $explode = explode('/', $_SERVER['REQUEST_URI']);
  44.     $region = $explode[1];
  45.     $location = $query->get('location');
  46.     if (''== $location) $query->set('location', $region );
  47.   }
  48. }
  49.  
  50. // go to region home page if a home page request from same site
  51. // region determined from referrer URL
  52. /* ***Functionality disabled***
  53. add_action('init', 'tg_home_by_region');
  54. function tg_home_by_region( $query ) {
  55.   if ( array_key_exists('HTTP_REFERER', $_SERVER ) && 0 === strpos( $_SERVER['HTTP_REFERER'], site_url('/'))) {
  56.     $explode = explode('/', $_SERVER['HTTP_REFERER']);
  57.     $region = $explode[3];
  58.     if ( "/$region/" != $_SERVER['REQUEST_URI'] && '/' == $_SERVER['REQUEST_URI']) {
  59.       wp_redirect( site_url("/$region/"));
  60.       exit;
  61.     }
  62.   }
  63. }
  64. */
  65.  
  66. // Assign value to %location% rewrite tag - doesn't work for pages
  67. add_filter( 'post_link', 'tg_filter_post_link', 10, 2 );
  68. function tg_filter_post_link( $permalink, $post ) {
  69.     // bail if %location% tag is not present in the url:
  70.     if ( false === strpos( $permalink, '%location%'))
  71.         return $permalink;
  72.  
  73.     $terms = wp_get_post_terms( $post->ID, 'location');
  74.     // set location, if no location is found, provide a default value.
  75.     if ( 0 < count( $terms ))
  76.         $location = $terms[0]->slug;
  77.     else
  78.         $location = 'benelux';   // this is the default value
  79.     $location = urlencode( $location );
  80.     $permalink = str_replace('%location%', $location , $permalink );
  81.  
  82.     return $permalink;
  83. }
  84.  
  85. // alter category and tag links to be compatible with global $region category
  86. add_filter('term_link', 'tg_alter_term_links', 999, 3 );
  87. function tg_alter_term_links( $termlink, $term, $taxonomy ) {
  88.    global $region;
  89.    if ('category' == $taxonomy ) {
  90.       $termlink = site_url('/') . $region . '?category_name=' . $term->slug;
  91.    }
  92.    if ('post_tag' == $taxonomy ) {
  93.       $termlink = site_url('/') . $region . '?tag=' . $term->slug;
  94.    }
  95.    return $termlink;
  96. }
  97.  
  98. /**
  99.  * Outputs prev/next nav links for custom query pages
  100.  *
  101.  * @param array, $args, the same $args used for the page's main WP_Query
  102.  *          The args array must include an 'offset' argument, relying on 'paged' will not work.
  103.  *      string, $next, text for next page link (optional) default "Older Posts"
  104.  *      string, $prev, text for previous page link (optional) default "Newer Posts"
  105.  * @return none, outputs prev/next nav links with region_page URL parm inside a div with
  106.  *      class 'search_page_nav', anchor links have class 'search_page_next' and 'search_page_prev'
  107.  * @globals $_GET['region_page'], used to determine current page number. Page 1 assumed
  108.  *      if 'region_page' is not set.
  109.  */
  110. function tg_custom_query_nav( $args, $next = 'Older Posts', $prev = 'Newer Posts' ) {
  111.     // determine pagination parms
  112.     $s_page = isset( $_GET['region_page']) ? sanitize_text_field( $_GET['region_page']) : 1;
  113.     $pppage = isset( $args['posts_per_page']) ? $args['posts_per_page'] : get_option('posts_per_page');
  114.     if ( -1 == $pppage ) return;
  115.     $prev_off = ( $s_page - 2 ) * $pppage;
  116.     $next_off = $s_page * $pppage;
  117.  
  118.     // check for more posts
  119.     $args['no_found_posts'] = true;     // just get a count
  120.     $args['ignore_sticky_posts'] = true;
  121.     $args['offset'] = $next_off;
  122.     $query = new WP_Query( $args );     // get next page post count
  123.     $ct_next = $query->post_count;
  124.     $args['offset'] = $prev_off;
  125.     $query = new WP_Query( $args );     // get prev page post count
  126.     $ct_prev = 1 < $s_page ? $query->post_count : 0;
  127.  
  128.     // output links
  129.     echo "\n<div class=\"paging-navigation\"><div class=\"nav-links\">";
  130.     $link = remove_query_arg('region_page');
  131.     $next .= '<i class="fa fa-chevron-right" aria-hidden="true"></i>';
  132.     $nxt_link = esc_url( add_query_arg('region_page', $s_page+1, $link ));
  133.     if ( $ct_next ) echo "\n<a class=\"nav-next\" href=\"$nxt_link\">$next</a>";
  134.     $prev = '<i class="fa fa-chevron-left" aria-hidden="true"></i>'. $prev;
  135.     $prv_link = esc_url( add_query_arg('region_page', $s_page-1, $link ));
  136.     if ( $ct_prev ) echo "\n<a class=\"nav-previous\" href=\"$prv_link\">$prev</a>\n";
  137.     echo "</div></div>\n";
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement