之前我们做了WordPress自动采集并发布内容的插件,里面用到了需要重新另外打开一个窗口来采集内容,以防止出现不必要的错误而导致中断。
这里使用到了一个新的定义,比如我需要使用
https://laoji.org/jpost/?jtask=laoji.org
但其实这个页面并不存在,这里只是虚拟了一个页面和参数来处理数据。
具体代码如下:
//register activation function register_activation_hook(__FILE__, 'jpost_activate'); //register deactivation function register_deactivation_hook(__FILE__, 'jpost_deactivate'); function jpost_activate() { jpost_plugin_rules(); flush_rewrite_rules(); } function jpost_deactivate() { flush_rewrite_rules(); } //add rewrite rules in case another plugin flushes rules add_action('init', 'jpost_plugin_rules'); //add plugin query vars (product_id) to wordpress add_filter('query_vars', 'jpost_plugin_query_vars'); //register plugin custom pages display add_filter('template_redirect', 'jpost_plugin_display'); function jpost_plugin_rules() { add_rewrite_rule('jpost/?([^/]*)', 'index.php?jpost=jpost&$matches[1]', 'top'); // 调试时如需要重新应用rewrite规则并保存到数据,删除以下2行的注释 // global $wp_rewrite; // $wp_rewrite->flush_rules(); } function jpost_plugin_query_vars($query_vars) { array_push($query_vars,'jpost'); array_push($query_vars,'jtask'); array_push($query_vars,'i'); array_push($query_vars,'jpage'); array_push($query_vars,'debug'); return $query_vars; } function jpost_plugin_display( $templates = '' ) { global $wp_query,$wpdb,$options; $template = $wp_query->query_vars; if ( array_key_exists( 'jpost', $template ) && 'jpost' == $template['jpost'] ): if ( is_user_logged_in() ) { require_once plugin_dir_path( __FILE__ )."wp-jcrawl.php"; }else{ echo wp_jpost_output('Access denied.'); } exit; endif; }