3

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

6
  • Internal rules should point to index.php. Plugin files should not be loaded directly if you expect to use the API.
    – Milo
    Commented Jun 17, 2017 at 12:26
  • @Milo, does modifing .htaccess directly works? I am trying to make my .htaccess work on the other hand but couldn't achieve this too. Here: stackoverflow.com/questions/44604007/…
    – HOY
    Commented Jun 17, 2017 at 13:15
  • You can add rules directly to htaccess if you'd like, but note that WordPress internal rewrites do not use htaccess, it is a separate system. But again, plugins really should not be loading php files directly, WordPress is not loaded in that context.
    – Milo
    Commented Jun 17, 2017 at 14:27
  • @Milo is it possible to solve this with creating rewrite endpoints? I am not experienced with endpoints.
    – HOY
    Commented Jun 17, 2017 at 18:40
  • It's not clear what you're trying to accomplish. Content is normally viewed within the context of a theme file loaded to match the main query. What does your plugin do?
    – Milo
    Commented Jun 17, 2017 at 19:31

2 Answers 2

2
+50

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.

4
  • Hi, I have tried your solution and faced a problem, I have updated my question with it, can you please check ?
    – HOY
    Commented Jun 19, 2017 at 13:06
  • @HOY That's strange. Did you flush the permalinks? You can also get the value by using $wp_query->query_vars['cat'] instead of $_REQUEST['cat'].
    – Johansson
    Commented Jun 19, 2017 at 13:25
  • I used get_query_var('cat') function and it works and everything is fine now, one thing that I didn't understand from your code is why did you needed to use is_page_template, we know that it is already page-custom.php template because I choose it while creating my page. Also I didn't use isset function too.
    – HOY
    Commented Jun 19, 2017 at 16:13
  • @HOY It's an additional check, in case someone uses the function somewhere outside that page. Future proof!
    – Johansson
    Commented Jun 19, 2017 at 16:23
1

Rewrite rules expect the new location to go off index.php. If you change it to 'index.php?pagename=tisort-tasarla' it should work as long as that page (or post) exists and is published.

1
  • I edited my question to better reflect my problem, your solution works with my previous one (which I thought my sample space) but actually with real URLs it did not work again.
    – HOY
    Commented Jun 17, 2017 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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