php – wp_rewrite在WordPress插件

前端之家收集整理的这篇文章主要介绍了php – wp_rewrite在WordPress插件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好的,我有这个我一直在使用的代码,向我的应用程序吐出消息.直到今天才起作用.我已经删除了以下代码中的所有逻辑,使其成为simpiler.但是它应该“工作”有人可以帮助我修复这个代码到它的工作,并做好了吗?我知道它一起被黑客攻击,直到今天才似乎没有任何问题.我没有更新任何东西,不知道交易是什么.
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','init') );
    add_action( 'generate_rewrite_rules','createRewriteRules') );

    add_action( 'template_redirect','template_redirect_intercept') );
    // add_filter( 'query_vars','add_query_vars') );
}
我在这个过程中改变了一点代码,但这对我有用:
<?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__,'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','create_rewrite_rules'));
add_filter('query_vars','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','flush_rewrite_rules'));
add_action( 'template_redirect','template_redirect_intercept') );

我已经评论过重要的部分,但是我所做的是基本上将钩子移到use_filter而不是add_action.我也将过滤器移动到wordpress中实际使用的顺序.看来当时要做的事情

最后,确保您的永久链接设置为使用漂亮的URL.我有一个问题,我的设置为默认,这使wordpress忽略任何重写条件,否则需要解析.更改为一些漂亮的URL,您的条件将刷新.

让我知道,如果这适用于你.希望有帮助.

谢谢,乔

原文链接:/php/131096.html

猜你在找的PHP相关文章