0

I'm creating a plugin that has a view in front-end and i have a link in home page to access it:
/wp-content/plugins/wp-test-plugin/public/partials/wp-test-plugin-public-display.php.

But I want a rewrite rule to access it using a short url like:
/wp-test-plugin

My rewrite funnctions that doesn't work:

public function my_rewrite_roles() {
    global $wp_rewrite;
    $new_rules = array('^wp-test-plugin/?$' => 'wp-content/plugins/wp-test-plugin/public/partials/wp-test-plugin-public-display.php');
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    //return $wp_rewrite->rules;
}
1
  • What wp-test-plugin-public-display.php does? What do you mean with "it doesn't work? As it is, your rewrite rule is saying to WordPress: if someone requests /wp-test-plugin then serve a PHP file that is outside WordPress. Can you explain what exactly are you trying to do? It seems to me that you are asking the wrong question and may be falling into a XY problem. I guess what you really want is to use that PHP file like a template or a template part, which is not waht rewrite rules does. Can you explain please?
    – cybmeta
    Dec 16, 2015 at 16:34

1 Answer 1

0

This will create / handle your endpoint and attempt to load your php if the file is readable. Note: you won't want to refresh the rewrite rules here but it'll make sure you can test quickly.

if (!class_exists('WPTestPluginEndpoint')):

    class WPTestPluginEndpoint
    {
        const ENDPOINT_QUERY_NAME = '__my_test_plugin';

        // WordPress hooks

        public function init()
        {
            add_filter('query_vars', array($this, 'add_query_vars'), 0);
            add_action('parse_request', array($this, 'sniff_requests'), 0);
            add_action('init', array($this, 'add_endpoint'), 0);
        }

        // Add public query vars
        public function add_query_vars($vars)
        {
            $vars[] = static::ENDPOINT_QUERY_NAME;

            return $vars;
        }

        // Add API Endpoint

        public function add_endpoint()
        {
            $query_name = self::ENDPOINT_QUERY_NAME;
            add_rewrite_rule('^wp-test-plugin/?', "index.php?{$query_name}=1", 'top');

//////////////////////////////////////
//////////////////////////////////////
// don't do this all the time!!!
//////////////////////////////////////
flush_rewrite_rules(false);
//////////////////////////////////////
//////////////////////////////////////
        }

        // Sniff Requests

        public function sniff_requests()
        {
            global $wp;
            if (isset($wp->query_vars[ self::ENDPOINT_QUERY_NAME ])) {
                $this->handle_request(); // handle it
                exit;
            }
        }

        // Handle Requests

        protected function handle_request()
        {
            $public_display = WP_PLUGIN_DIR . '/wp-test-plugin/public/partials/wp-test-plugin-public-display.php';

            if( is_readable( $public_display )) {

                require $public_display;

            }
            else {

                wp_die("Can't find Render Page ". $public_display );

            }

            die(); // nothing to be done
        }
    }

    $wptept = new WPTestPluginEndpoint();
    $wptept->init();

endif; // WPTestPluginEndpoint
2
  • Just a tip, filter and actions should not be executed inside constructors. Constructors should not be doing any work, it should initialize Dec 16, 2015 at 16:54
  • Fixed and the example has been updated... You're correct, that's usually the case for all languages.
    – jgraup
    Dec 16, 2015 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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