WordPress插件中的wp_rewrite [英] wp_rewrite in a WordPress Plugin

查看:52
本文介绍了WordPress插件中的wp_rewrite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我已经有了用来向我的应用程序发送新闻的这段代码.一直工作到今天.我在下面的代码中剪切了所有逻辑,使其变得更加简单.但是它应该工作"有人可以帮助我将此代码修复为可以工作的地方,并且正确完成吗?我知道它已经被黑客入侵了,但是直到今天似乎还没有任何问题.我还没有更新任何消息,不知道这笔交易是什么.

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') );
}

推荐答案

我在此过程中更改了一些代码,但这对我有用:

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') );

我已经评论了重要的部分,但是我所做的基本上是将您的事务转移到use_filter而不是add_action上.我还将过滤器移到了在Wordpress中实际使用的顺序.好像当时要做的事情.

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.

最后,请确保您的永久链接设置为使用漂亮的URL.我遇到了一个问题,其中我的设置​​为默认设置,这使Wordpress忽略了否则需要解析的所有重写条件.切换到一些漂亮的URL,您的条件就会刷新.

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.

谢谢乔

这篇关于WordPress插件中的wp_rewrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆