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 673491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:36:06+00:00 2026-05-14T00:36:06+00:00

Ok, I’ve got this code that I’ve been using to spit out news to

  • 0

Ok, I’ve got this code that I’ve been using to spit out news to an application of mine. It was working until today. I’ve cutout all the logic in the following code to make it simpiler. But it should “WORK” Can someone help me fix this code to where it works, and is done right? I know it’s hacked together , but it didn’t seem to have any problems until today. I have not updated anything, don’t know what the deal is.



 Plugin Name:   MyPlugin Example
 Version:       1.0.1


If ( ! class_exists("MyPlugin") )
{
    class MyPlugin
    {
        var $db_version = "1.0"; //not used yet

        function init()
        {
   //Nothing as of now.
        }
        function activate()
        {
            global $wp_rewrite;
            $this->flush_rewrite_rules();
        }

        function pushoutput( $id )
        {
            $output->out =' The output worked!';
            $this->output( $output );

        }
        function output( $output )
        {
            ob_start();
            ob_end_clean();
            header( 'Cache-Control: no-cache, must-revalidate' );
            header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
            header( 'Content-type: application/json' );

            echo json_encode( $output );
            //Must encode this...
        }

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

        function createRewriteRules( $rewrite )
        {
            global $wp_rewrite;
            $new_rules = array( 'MyPlugin/(.+)' => 'index.php?MyPlugin=' . $wp_rewrite->preg_index(1) );
            if ( ! is_array($wp_rewrite->rules) )
            {
                $wp_rewrite->rules = array();
            }
            $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
            return $wp_rewrite;
        }


        function add_query_vars( $qvars )
        {
            $qvars[] = 'MyPlugin';
            return $qvars;
        }
        function template_redirect_intercept()
        {
            global $wp_query;
            if ( $wp_query->get('MyPlugin') )
            {
                $id = $wp_query->query_vars['MyPlugin'];
                $this->pushoutput( $id );


                exit;
            }
        }
    }
}
If ( class_exists("MyPlugin") )
{
    $MyPluginCode = new MyPlugin();
}
If ( isset($MyPluginCode) )
{
    register_activation_hook( __file__, array($MyPluginCode, 'activate') );
    add_action( 'admin-init', array(&$MyPluginCode, 'flush_rewrite_rules') );
    //add_action( 'init', array(&$MyPluginCode, 'init') );
    add_action( 'generate_rewrite_rules', array(&$MyPluginCode, 'createRewriteRules') );

    add_action( 'template_redirect', array(&$MyPluginCode, 'template_redirect_intercept') );
    // add_filter( 'query_vars', array(&$MyPluginCode, 'add_query_vars') );
}

  • 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-14T00:36:06+00:00Added an answer on May 14, 2026 at 12:36 am

    I changed a little bit of your code in the process, but this worked for me:

    <?php
    
    /**
    * Plugin Name:   MyPlugin Example
    * Version:       1.0.1
    **/
    class MyPlugin {
    
        function activate() {
            global $wp_rewrite;
            $this->flush_rewrite_rules();
        }
    
        // Took out the $wp_rewrite->rules replacement so the rewrite rules filter could handle this.
        function create_rewrite_rules($rules) {
            global $wp_rewrite;
            $newRule = array('MyPlugin/(.+)' => 'index.php?MyPlugin='.$wp_rewrite->preg_index(1));
            $newRules = $newRule + $rules;
            return $newRules;
        }
    
        function add_query_vars($qvars) {
            $qvars[] = 'MyPlugin';
            return $qvars;
        }
    
        function flush_rewrite_rules() {
            global $wp_rewrite;
            $wp_rewrite->flush_rules();
        }
    
        function template_redirect_intercept() {
            global $wp_query;
            if ($wp_query->get('MyPlugin')) {
                $this->pushoutput($wp_query->get('MyPlugin'));
                exit;
            }
        }
    
        function pushoutput($message) {
            $this->output($message);
        }
    
        function output( $output ) {
            header( 'Cache-Control: no-cache, must-revalidate' );
            header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
    
            // Commented to display in browser.
            // header( 'Content-type: application/json' );
    
            echo json_encode( $output );
        }
    }
    
    $MyPluginCode = new MyPlugin();
    register_activation_hook( __file__, array($MyPluginCode, 'activate') );
    
    // Using a filter instead of an action to create the rewrite rules.
    // Write rules -> Add query vars -> Recalculate rewrite rules
    add_filter('rewrite_rules_array', array($MyPluginCode, 'create_rewrite_rules'));
    add_filter('query_vars',array($MyPluginCode, 'add_query_vars'));
    
    // Recalculates rewrite rules during admin init to save resourcees.
    // Could probably run it once as long as it isn't going to change or check the
    // $wp_rewrite rules to see if it's active.
    add_filter('admin_init', array($MyPluginCode, 'flush_rewrite_rules'));
    add_action( 'template_redirect', array($MyPluginCode, 'template_redirect_intercept') );
    

    I’ve commented the important parts, but what I did was basically to move your hooks over to use_filter rather than add_action. I also moved the filters into the order that they are actually used in WordPress. Seemed the thing to do at the time.

    Finally, make sure that your permalinks are set to use pretty URLs. I had an issue where mine were set to default, which makes WordPress ignore any rewrite conditions that it would otherwise need to parse. Change over to some pretty URLs and your conditions will refresh.

    Let me know if that works for you. Hope that helps.

    Thanks, Joe

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 380k
  • Answers 381k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer center is not a valid value for vertical-align (you're probably… May 14, 2026 at 10:01 pm
  • Editorial Team
    Editorial Team added an answer As said I would like to get a list of… May 14, 2026 at 10:01 pm
  • Editorial Team
    Editorial Team added an answer Another idea is to move the strings into a "configuration"… May 14, 2026 at 10:01 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.