Advertisement
Beee

raw rewrite

Jan 17th, 2017
1,159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.90 KB | None | 0 0
  1. <?php
  2. function custom_rewrite_tag() {
  3.     add_rewrite_tag( '%city%', '([^&]+)', 'city=' );
  4.     add_rewrite_tag( '%postname%', '([^&]+)', 'name=' );
  5. }
  6. add_action( 'init', 'custom_rewrite_tag', 10, 0 );
  7.  
  8. function add_vars( $vars ) {
  9.     $vars[] = "city";
  10.     $vars[] = "postname";
  11.     return $vars;
  12. }
  13. add_filter( 'query_vars', 'add_vars' );
  14.  
  15. function wpa_course_post_link( $post_link, $id = 0 ) {
  16.     $post = get_post( $id );
  17.     if ( is_object( $post ) ) {
  18.         $city = get_field( 'sd_city', $post->ID );
  19.         if ( $city ) {
  20.             // $post_link = home_url( 'sexdate/' . strtolower($city) . '/' . $post->post_name . '-' . $post->ID);
  21.         }
  22.     }
  23.     // var_dump($post_link);
  24.     // exit;
  25.     return $post_link;
  26. }
  27. // add_filter( 'post_link', 'wpa_course_post_link', 10, 3 );
  28. // add_filter( 'post_type_link', 'wpa_course_post_link', 10, 3 );
  29.  
  30. function mycptonomy_permalink( $permalink, $post_id, $leavename = false ) {
  31.     if ( strpos( $permalink, '%city%' ) === FALSE ) {
  32.         return $permalink;
  33.     }
  34.  
  35.     // Get post
  36.     $post = get_post( $post_id );
  37.     if ( ! $post ) {
  38.         return $permalink;
  39.     }
  40.  
  41.     // Get custom info
  42.     $city_info  = get_field( 'sd_city_selector', $post->ID );
  43.     $post_slug  = $post->post_name;
  44.     if ( ! is_wp_error( $city_info ) && !empty( $city_info ) ) {
  45.         $city_replace = str_replace('\'', '', $city_info['cityNameAscii'] );
  46.         $city_replace = str_replace(' ', '-', $city_replace );
  47.         $city_slug = strtolower( $city_replace );
  48.         // echo '<pre>'; var_dump($city_slug ); echo '</pre>';
  49.     } else {
  50.         $city_slug = ''; // not really necessary since every post has obligated city field
  51.     }
  52.     $new_permalink = str_replace( array( '%city%', '%postname%', '%post_id%' ), array( $city_slug, $post_slug, $post->ID ), $permalink );
  53.     return $new_permalink;
  54. }
  55. add_filter( 'post_link', 'mycptonomy_permalink', 10, 3 );
  56. add_filter( 'post_type_link', 'mycptonomy_permalink', 10, 3 );
  57.  
  58. function force_mycpt_rewrite() {
  59.     global $wp_rewrite;
  60.     $wp_rewrite->add_permastruct( 'sexdate', 'sexdate/%city%/%postname%-%post_id%/', false );
  61.  
  62.     // add_rewrite_rule( 'sexdate/([a-z])/(.+)/?$', 'index.php?post_type=sexdate&p=$matches[2]', 'top' );
  63.     // add_rewrite_rule( 'sexdate\/([a-z]+)\/(.+)-[0-9]+\/?$', 'index.php?post_type=sexdate&city=$matches[1]&p=$matches[3]', 'top' ); // lists posts
  64.     add_rewrite_rule( 'sexdate\/([a-z-]+)\/(.+)-[0-9]+\/?$', 'index.php?post_type=sexdate&p=$matches[2]&city=$matches[1]&name=$matches[2]', 'top' );
  65.  
  66.     // add_rewrite_rule(
  67.     //     sprintf( '^%s/([a-z])/([^/]*)?$', 'sexdate' ),
  68.     //     sprintf( 'index.php?post_type=%s&%s=$matches[1]', 'sexdate', 'sexdate' ),
  69.     //     'top'
  70.     // );
  71.  
  72.  
  73.  
  74.     // $wp_rewrite->flush_rules( true ); // !!!
  75.  
  76.     // echo '<pre>'; var_dump( $wp_rewrite ); echo '</pre>';
  77.     // exit;
  78. }
  79. add_action( 'init', 'force_mycpt_rewrite' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement