BOUNTY EDIT :

I have a plugin which does have links similar to below:

mydomain.com/wp-content/plugins/myplugin/includes/categories.php?c=9

I want these links to be more SEO friendly and to work like this:

mydomain.com/wp-content/plugins/myplugin/includes/categories/9/

SOLUTIONS I HAVE TRIED AND FAILED:

The plugin uses ajax. I tried using add_rewrite_rule as below
My plugin has following function:

add_action( 'init', 'add_alexes_rules' );
function add_alexes_rules() {
    add_rewrite_rule('^categories/([0-9]+)/?$', 'categories.php?c=$1', 'top');
    flush_rewrite_rules();
}

and learned that it won’t work after a reply from @Milo:

Internal rules should point to index.php

and tried to edit .htaccess directly but stuck after a reply from @Deadooshka:

WP uses php-server-variables to handle request thru the index.php, so
rule must change that variable.

So what is the way to achieve this?

EDIT: Jack Johansson’s SOLUTION

Created a php file: page-custom.php as below:

<?php
/**
 * Template Name: My Custom Template
 */

if (is_page_template( 'page-custom.php' ) && isset($_REQUEST['cat'])){
    ...
}
?>

Created a page with ID: 281 which uses page-custom.php template.

My functions.php:

function my_rewrite_tag() {
    add_rewrite_tag('%cat%', '([^&]+)');
}
function my_rewrite_rule() {
    add_rewrite_rule('^categories/([^/]*)/?','index.php?page_id=281&cat=$matches[1]','top');
}
add_action('init', 'my_rewrite_tag', 10, 0);
add_action('init', 'my_rewrite_rule', 10, 0);

Now when I navigate to an example url like the one below

http://localhost/mydomain/categories/123/

Problem is $_REQUEST['cat'] is empty so it never enters to the condition inside page-custom.php

2 Answers
2

Why don’t we create our own template file, and do our stuff there? Here’s what I’m going to do:

  1. Create a custom page template
  2. Call the required plugin’s functions inside the page
  3. Redirect the plugins URLs to the page
  4. Get the query var and process the request

– The page template

Let’s create a simple custom page template. We call it page-custom.php. We will use this page to interact with our plugin.

<?php
/**
 * Template Name: My Custom Template
 */
 // Our function to get the data
 if (function_exists('sample_function')) sample_function();
?>

We also create a page in the back-end, its slug is /my-custom-page/ and its ID is 123.

– The rewrite rules

Now let’s check if we are on that page and redirect the data to it.

function my_rewrite_tag() {
    add_rewrite_tag('%cat%', '([^&]+)');
}
function wallpaperinho_subcat_rewrite_rule() {
    add_rewrite_rule('^categories/([^/]*)/?','index.php?page_id=123&cat=$matches[1]','top');
}
add_action('init', 'my_rewrite_tag', 10, 0);
add_action('init', 'my_rewrite_rule', 10, 0);

What does this piece of code do? It checks if we are visiting /categories/321/ and redirects it to our page. Now we have the data we need. Let’s check the template.

– The conditional for template

We check if we are on page-custom.php and return our data to the page.

if (is_page_template( 'templates/about.php' ) && isset($_REQUEST['cat'])){
    function sample_function() { 
        $c = $_REQUEST['cat'];
        // We have the c value, call the internal plugin's functions and return its value to the page
        return $data;
    }
}

This serves as an example, to create our own template and use the redirects to make it look like whatever we want. It can and needs to be changed to fit your needs.

Leave a Reply

Your email address will not be published. Required fields are marked *