2

I have a custom WordPress url which is generated by ID. I have to rewrite this url https://example.com/account/customer-bookings/?view-booking=4 to https://example.com/account/customer-bookings/view-booking/4. How Can I don it?

My Current .htaccess code is following,

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php56” package as the default “PHP” programming language.
<IfModule mime_module>
  AddType application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
1
  • first you need to find the real URL without rewriting of account/customer-bookings/?view-booking=4. it must be something like index.php?view-booking=4. edit your question to add this information.
    – Kaperto
    Aug 17, 2018 at 7:29

1 Answer 1

4

WordPress has its own system managing redirects and page routes, you don't need to edit the .htaccess file. You'll want to start with the function add_rewrite_rule().

It takes 3 arguments:

  1. route (as regex)
  2. query vars
  3. priority

So first, you need to find out the query vars of account/customer-bookings/. If it is a page, it can be page_id. To reproduce what WordPress already does, it could be (XXX being the specific page_id):

add_rewrite_rule(
    '^account/customer-bookings/?$',
    'index.php?page_id=XXX'
);

Now you just need to expand this: (don't forget to flush rewrite rules after adding this code!)

add_action('init', 'wpse_view_booking_rewrite');
function wpse_view_booking_rewrite() {
    add_rewrite_rule(
        '^account/customer-bookings/view-booking/([^/]+)/?$',
        'index.php?page_id=XXX&view-booking=$matches[1]',
        'top'
    );
}

This should already present the correct page. However, you won't be able to use get_query_var('view-booking'), because it is no default variable. To solve this, simply tell WP to watch out for it like so

add_filter('query_vars', 'wpse_view_bookings_filter');
function wpse_view_bookings_filter($vars) {
    $vars[] = 'view-booking';
    return $vars;
}

At this point WordPress knows about the variable, and by calling get_query_var('view-booking') you will get the proper variable.

6
  • I'm sorry. I'm little bit confused about page_id=XXX. Also the placement area of your codes. Let me describe about that page I'm talking, First I have create page from the WordPress backend page menu id with 221. Then I have create a template from the WordPress theme directory. And make some custom WordPress query using $wpdb. Aug 17, 2018 at 10:56
  • You can place this code in your theme's functions.php for it to work. You say the created page has the ID 221 (you see it in the URL when editing in the backend), so replace XXX by 221 making it 'index.php?page_id=221&view-booking=$matches[1]',. Now in the page template, use $booking = get_query_var('view-booking'); and you can use $booking when constructing the WP Query.
    – kero
    Aug 17, 2018 at 12:16
  • I have use following codes into function.php but, it's not working. Please find the following url prntscr.com/kjs3vy, prntscr.com/kjs4j9 Aug 17, 2018 at 13:13
  • @SonjoyDatta The first code was only an example, please delete lines 291-294 in the first screenshot
    – kero
    Aug 17, 2018 at 13:18
  • I have remove lines 291-294. Then flush rewrite rules but, still same problem. prntscr.com/kjsavk, prntscr.com/kjsb4s, prntscr.com/kjsbkm Aug 17, 2018 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.