marstheme

Untitled

May 11th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.85 KB | None | 0 0
  1. if( ! function_exists( 'videotube_child_add_rel_custom_page' ) ){
  2.    
  3.     /**
  4.      * Add the rel meta tag into header.
  5.      */
  6.    
  7.     function videotube_child_add_rel_custom_page() {
  8.        
  9.         global $post;
  10.        
  11.         if( ! is_singular() || ! isset( $post->post_content ) ){
  12.             return;
  13.         }
  14.        
  15.         if( defined( 'PHP_VERSION' ) && version_compare( PHP_VERSION , '5.3.0', '<' ) ){
  16.             return;
  17.         }
  18.        
  19.         if( has_shortcode( $post->post_content , 'videotube' ) && strpos( $post->post_content , 'navigation="on"') ){
  20.            
  21.             $max_num_pages  =   (int)get_post_meta( $post->ID, 'custom_max_num_pages', true );
  22.            
  23.             if( $max_num_pages == 0 ){
  24.                 return;
  25.             }
  26.            
  27.             add_filter( 'wpseo_canonical' , '__return_null' );
  28.  
  29.             $page_var = is_front_page() ? 'page' : 'paged';
  30.            
  31.             $paged = get_query_var( $page_var ) ? get_query_var( $page_var ) : 2;
  32.  
  33.             if( ! is_paged() ){
  34.                 // add the next tag
  35.                 add_action( 'wpseo_head' , function() use( $paged, $max_num_pages ){
  36.                     echo '<link rel="canonical" href="'. esc_url( get_permalink( $post->ID ) ) .'">';
  37.                     if( $max_num_pages > 1 ){
  38.                         echo '<link rel="next" href="'. esc_url( add_query_arg( array( 'paged' => $paged ), get_permalink( $post->ID ) ) ) .'">';
  39.                     }
  40.                    
  41.                 });
  42.             }
  43.             else{
  44.                 add_action( 'wpseo_head' , function() use( $paged, $max_num_pages ){
  45.  
  46.                     echo '<link rel="canonical" href="'. esc_url( add_query_arg( array( 'paged' => $paged ), get_permalink( $post->ID ) ) ) .'">';
  47.                     echo '<link rel="prev" href="'. esc_url( add_query_arg( array( 'paged' => $paged - 1 ), get_permalink( $post->ID ) ) ) .'">';
  48.                    
  49.                     if( $max_num_pages >= ( $paged + 1 ) ){
  50.                         echo '<link rel="next" href="'. esc_url( add_query_arg( array( 'paged' => $paged + 1 ), get_permalink( $post->ID ) ) ) .'">';
  51.                     }
  52.                    
  53.                 });
  54.             }
  55.            
  56.         }
  57.     }
  58.    
  59.     add_action( 'wp', 'videotube_child_add_rel_custom_page', 21 );
  60. }
Advertisement
Add Comment
Please, Sign In to add comment