//Suitable for/% post_id%. html pagination link correction class Rewrite_Inner_Page_Links_id{ var $separator; function __construct(){ $this->separator = '/page-'; if( ! is_admin() || defined( 'DOING_AJAX' ) ) : add_filter( 'wp_link_pages_link', array( $this, 'inner_page_link_format' ), 10, 2 ); add_filter( 'get_comments_pagenum_link', array( $this, 'comment_page_link_format' ) ); add_filter( 'redirect_canonical', array( $this, 'cancel_redirect_for_paged_posts' ), 10, 2 ); endif; if( is_admin() ) : add_filter( 'rewrite_rules_array', array( $this, 'pagelink_rewrite_rules' ) ); register_activation_hook( __FILE__, array( $this, 'flush_rewrite_rules' ) ) ; register_deactivation_hook( __FILE__, array( $this, 'flush_rewrite_rules' ) ); endif; } function flush_rewrite_rules(){ flush_rewrite_rules(); } //Modify the format of post pagination link function inner_page_link_format( $link, $number ){ if( $number > 1 ){ if( preg_match( '%<a href=".*\.html/\d*"%', $link ) ){ $link = preg_replace( "%(\.html)/(\d*)%", $this->separator. "$2$1", $link ); } } return $link; } //Add redirection rules for the new link format, remove the redirection rules for the original paging links, and prevent repeated inclusion function pagelink_rewrite_rules( $rules ){ foreach ($rules as $rule => $rewrite) { if ( $rule == '([0-9]+).html(/[0-9]+)?/?$' ) { unset($rules[$rule]); } } $new_rule['([0-9]+)('.$this->separator.'([0-9]+))?. html/?$'] = 'index.php?p=$matches[1]&page=$matches[3]'; return $new_rule + $rules; } //Forbid WordPress to jump page links to the original format function cancel_redirect_for_paged_posts( $redirect_url, $requested_url ){ global $wp_query; if( is_single() && $wp_query->get( 'page' ) > 1 ){ return false; } return true; } } new Rewrite_Inner_Page_Links_id();
$new_rule['([0-9]+)('.$this->separator.'([0-9]+))?. html/?$'] = 'index.php?p=$matches[1]&page=$matches[3]';
$new_rule['[^/]+/([0-9]+)('.$this->separator.'([0-9]+))?. html/?$'] = 'index.php?p=$matches[1]&page=$matches[3]';
//Fix. html Paging Links class Rewrite_Inner_Page_Links{ var $separator; function __construct(){ $this->separator = '/page-'; if( ! is_admin() || defined( 'DOING_AJAX' ) ) : add_filter( 'wp_link_pages_link', array( $this, 'inner_page_link_format' ), 10, 2 ); // for inner pages add_filter( 'get_comments_pagenum_link', array( $this, 'comment_page_link_format' ) ); add_filter( 'redirect_canonical', array( $this, 'cancel_redirect_for_paged_posts' ), 10, 2 ); endif; if( is_admin() ) : add_filter( 'rewrite_rules_array', array( $this, 'pagelink_rewrite_rules' ) ); register_activation_hook( __FILE__, array( $this, 'flush_rewrite_rules' ) ) ; register_deactivation_hook( __FILE__, array( $this, 'flush_rewrite_rules' ) ); endif; } function flush_rewrite_rules(){ flush_rewrite_rules(); } /** *Modify the format of post pagination link * @param string $link * @param int $number * @return string */ function inner_page_link_format( $link, $number ){ if( $number > 1 ){ if( preg_match( '%<a href=".*\.html/\d*"%', $link ) ){ $link = preg_replace( "%(\.html)/(\d*)%", $this->separator. "$2$1", $link ); } } return $link; } /** *Modify comment pagination link * @param string $result * @return string */ function comment_page_link_format( $result ){ // From hello-world.html/comment-page-1#comments to hello-world/comment-page-1.html#comments if( strpos( $result, '.html/' ) !== false ){ $result = preg_replace( '=([^/]+)(.html)/comment-page-([0-9]{1,})=', "$1/comment-page-$3$2" ,$result ); } return $result; } /** *Add redirection rules for the new link format, remove the redirection rules for the original paging links, and prevent repeated inclusion * *Visiting the original link will return 404 * @param array $rules * @return array */ function pagelink_rewrite_rules( $rules ){ foreach ($rules as $rule => $rewrite) { if ( $rule == '([^/]+).html(/[0-9]+)?/?$' || $ rule == '([^/]+).html/comment-page-([0-9]{1,})/?$' ) { unset($rules[$rule]); } } $new_rule['([^/]+)('.$this->separator.'([0-9]+))?. html/?$'] = 'index.php?name=$matches[1]&page=$matches[3]'; $new_rule['([^/]+)/comment-page-([0-9]{1,}).html(\#[^\s])?$'] = ' index.php?name=$matches[1]&cpage=$matches[2]'; return $new_rule + $rules; } /** *Forbid WordPress to jump page links to the original format * @param string $redirect_url * @param string $requested_url * @return bool */ function cancel_redirect_for_paged_posts( $redirect_url, $requested_url ){ global $wp_query; if( is_single() && $wp_query->get( 'page' ) > 1 ){ return false; } return true; } } new Rewrite_Inner_Page_Links();
-
//if ( ! isset( $rules['(.+?)-cat/?$'] ) ) { // have to comment this in order to refresh the rules -
global $wp_rewrite ; -
$wp_rewrite ->flush_rules(); -
//}
-
if ( ! isset( $rules ['(.+?)-cat/?$'] ) ) { // have to comment this in order to refresh the rules -
global $wp_rewrite ; -
$wp_rewrite ->flush_rules(); -
}
add_filter( 'rewrite_rules_array', 'show_rewrite_rules' ); function show_rewrite_rules( $rules ) { echo nl2br( var_export( $rules, true ) ); die; }