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

  • Home
  • SEARCH
  • 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 7218169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:26:52+00:00 2026-05-28T21:26:52+00:00

I’m developping my first real big pluggin for wordpress and so far almost everything

  • 0

I’m developping my first real big pluggin for wordpress and so far almost everything is going smootly. The only thing i’m having, is difficulty spoofing the current post so that the layout of the user continues working fine. Let me explain.

I’m using the Wrox Profesionnal WordPress Plugin Development book’s permalink structure strategy. To this end, i do the following:

//Used to detect when the plugin gets actuvated
public function pluginActivatedAction(){

    //Send commands to install the rewrite rules
    add_rewrite_tag('%sgmpage%', '([^/]+)');
    add_permastruct('sgmpage', 'sgm/%sgmpage%');
    flush_rewrite_rules();

}

And thus, my plugin responds to all /sgm/** urls. Which is fine cause i want to be able to do some whacky url rewritting to get something nice going. Problem is, with that technique, wordpress can’t say under which page it is and this messes up the layout done by the integrator such as the header image, the active menu item, the sidebar.

I tried hooking to several hooks without success such as:

  1. wp (global $post and override it)
  2. pre_post_selection (And change the query)
  3. template_redirect (global $post and override it)

And nothing changes… The template is using The_Id() as a function to retrieve the id of the current post which effectively uses $post->id (i think, closed the source now) but either there is something overriding the $post AFTER i change it, or my technique is not working right.

So my question is, can you hook or override in anyway the current post the url matches using the “add_rewrite_tag” technique… I’d want to do something like that:

global $post;
$post_id = 440; //Or get_option() later obviously
$post = get_post($post_id);

But it doesn’t work.

Thanks for your help…


UPDATE

//Used to detect when the plugin gets actuvated
public function pluginActivatedAction(){

    //Send commands to install the rewrite rules
    add_rewrite_rule('sgm(/(([a-z0-9]+)(/([a-z]+)/?)?)?)', 'index.php?p=440&sgmevent=$matches[3]&sgmpage=$matches[5]');
    flush_rewrite_rules();

}

I removed everything else and just added a rewrite rule to get that p=440 going, everything is still working fine except the P query var doesn’t seem to be there, something tells me the rewrite is not working. I pasted that same thing in both my rewriter and in the activation function and i deactivated/reactivated my pluggin.

  • 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-05-28T21:26:53+00:00Added an answer on May 28, 2026 at 9:26 pm

    If that function is run only on activation, that’s going to be one major problem. add_rewrite_tag and add_permastruct both need to be run on every page load. flush_rewrite_rules should only be run on activation, as you have it here.

    I’m not sure if that will fix the 404 problem, since WordPress doesn’t know what to do with the 'sgmpage' query variable. One thing you might do is, rather than using add_rewrite_tag, add something like this:

    function your_own_add_rewrite_tag_function(){
        global $wp_rewrite, $wp;
        $wp->add_query_var('sgmpage');
        $wp_rewrite->add_rewrite_tag('%sgmpage%', '([^/]+)', 'p=440&sgmpage=');
    }
    

    That tells WordPress exactly what using add_rewrite_tag does, except it tells WP to also interpret all sgmpage requests as having a post id of 440. No extra work for WordPress to do, no extra filtering, actions, etc., while still preserving the sgmpage query vars.

    Does that help?

    EDIT

    To stop it from redirecting to the post, you will need to hook into 'redirect_canonical'. I totally forgot about canonical… It’s always the Daleks … er … canonical redirect!

    Adding this snippet should help:

    add_filter( 'redirect_canonical', 'redirect_canonical_8886079', 10, 2 );
    
    function redirect_canonical_8886079( $redirect_url, $requested_url ){
        global $wp;
        if(!empty($wp->query_vars['sgmpage']))
            return false;
        return $redirect_url;
    }
    

    redirect_canonical looks for requested objects and figures out if we’re on the canonical page for that object. If not, it 301 redirects the user to the canonical url for that data. The search engines love that stuff.

    Like the honey badger, redirect_canonical don’t care and does what it wants. Unlike the honey badger, you can tell it to stop by filtering the redirect value and either returning false or $requested_url, saying, essentially: either canonical doesn’t apply here or canonical is wrong and the requested URL is, in fact, the canonical url in this case.

    • 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
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
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
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.