Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8762997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:40:46+00:00 2026-06-13T15:40:46+00:00

I’m attempting to write a plugin for WordPress but I’m having some issues with

  • 0

I’m attempting to write a plugin for WordPress but I’m having some issues with regards to the wp_rewrite functionality.

I want to make one page appear as its many by passing variables via the URL (such as: http://www.mysite.com/WordPress?variable=helloall)

However I want to keep the permalink structure intact, so i want the URL to appear as such:

www.mysite.com/WordPress/helloall

I then want to be able to take the slug and use that the search my database. (like you would using $_GET if I was using the general method i mentioned first)


I have found a few tutorials online and as of yet able to get this working. I believe my problem is due to a lack of understand with HOW you write the rules correctly.

I have used this tutorial:

http://www.prodeveloper.org/create-your-own-rewrite-rules-in-wordpress.html

and I have attempted to use the same code for the most part. I am able to set the rules, but they just dont seem to want to work for me

can anyone tell me the correct format to beable to do this?

Edit

this is my current function

function add_rewrite_rules( $wp_rewrite ) 
{
    $new_rules = array
    (
        '(.?.+?)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
        $wp_rewrite->preg_index(1).'&varname='.
        $wp_rewrite->preg_index(2).'&page='.
        $wp_rewrite->preg_index(3),

        '(.?.+?)/(.*?)/?$' => 'index.php?pagename='.
        $wp_rewrite->preg_index(1).'&varname='.
        $wp_rewrite->preg_index(2)
    );
    // Always add your rules to the top, to make sure your rules have priority
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

add_action(‘generate_rewrite_rules’, ‘add_rewrite_rules’);

Solution

I have figured it out, I was going to post this as an answer but it seems I am unable to answer my own questions at this moment in time, so instead I’m editing the original post:

First off, the code I post above is correct, however the reason it was not working is because I was not flushing the rules, I do this with the following code:

function ebi_flush_rewrite_rules()
{
global $wp_rewrite;

$wp_rewrite->flush_rules();
}

add_action( 'init', 'flush_rewrite_rules');

My new problem, was that my code worked a little to well, redirecting ALL pages instead of just the one I wanted, this meant that no child pages would display which is a bit of an issue, I have however solved the problem with one small edit:

function add_rewrite_rules( $wp_rewrite ) 
{
  $new_rules = array
  (
    '(testpage)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
    $wp_rewrite->preg_index(1).'&varname='.
    $wp_rewrite->preg_index(2).'&page='.
    $wp_rewrite->preg_index(3),

    '(testpage)/(.*?)/?$' => 'index.php?pagename='.
    $wp_rewrite->preg_index(1).'&varname='.
    $wp_rewrite->preg_index(2)
  );
  // Always add your rules to the top, to make sure your rules have priority
  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

So my final code regarding the wp_rewrite functionality is as follows:

function add_rewrite_rules( $wp_rewrite ) 
{
$new_rules = array
(
    '(testpage)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
    $wp_rewrite->preg_index(1).'&varname='.
    $wp_rewrite->preg_index(2).'&page='.
    $wp_rewrite->preg_index(3),

    '(testpage)/(.*?)/?$' => 'index.php?pagename='.
    $wp_rewrite->preg_index(1).'&varname='.
    $wp_rewrite->preg_index(2)
);
// Always add your rules to the top, to make sure your rules have priority
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

function query_vars($public_query_vars)
{
$public_query_vars[] = "varname";
return $public_query_vars;
}

function ebi_flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}

add_action( 'init', 'flush_rewrite_rules');
add_action('generate_rewrite_rules', 'add_rewrite_rules');
add_filter('query_vars', 'query_vars');

I hope this saves someone else some time in the future.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:40:47+00:00Added an answer on June 13, 2026 at 3:40 pm

    I figured out how to get it working on e a specific page name, this is for anyone who is having issues in the future:

    function add_rewrite_rules( $wp_rewrite ) 
    {
        $new_rules = array
        (
            '(testpage)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
            $wp_rewrite->preg_index(1).'&varname='.
            $wp_rewrite->preg_index(2).'&page='.
            $wp_rewrite->preg_index(3),
    
            '(testpage)/(.*?)/?$' => 'index.php?pagename='.
            $wp_rewrite->preg_index(1).'&varname='.
            $wp_rewrite->preg_index(2)
        );
        // Always add your rules to the top, to make sure your rules have priority
        $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }
    
    function query_vars($public_query_vars)
    {
        $public_query_vars[] = "varname";
    
        return $public_query_vars;
    }
    
    function ebi_flush_rewrite_rules()
    {
        global $wp_rewrite;
    
        $wp_rewrite->flush_rules();
    }
    
    add_action( 'init', 'flush_rewrite_rules');
    add_action('generate_rewrite_rules', 'add_rewrite_rules');
    add_filter('query_vars', 'query_vars');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.