SEO Friendly URLs for my plugin categories

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 Comment