wordpress使用插件创建前台页面

最近项目中需要使用插件创建前台页面,试了很多种办法,最好的办法是rewrite url,再load template。

 

1,定义插件

创建插件目录: 项目根目录/wp-content/plugins/iggogrid

创建入口文件: iggogrid.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/**
* WordPress plugin "IggoGrid" main file, responsible for initiating the plugin
*
* @package IggoGrid
* @author Iggo
* @version 0.1
*/

/*
Plugin Name: IggoGrid
Description: IggoGrid enables you to create and manage tables in your posts and pages, without having to write HTML code, and it adds valuable functions for your visitors.
Version: 0.1
Author: Iggo
Author email: dev@fenzsoft.com
Text Domain: iggogrid
Domain Path: /i18n
License: GPL 2
*/

// Prohibit direct script loading
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );

// Define certain plugin variables as constants
define( 'IGGOGRID_ABSPATH', plugin_dir_path( __FILE__ ) );

define( 'IGGOGRID__FILE__', __FILE__ );
define( 'IGGOGRID_BASENAME', plugin_basename( IGGOGRID__FILE__ ) );
// // Load IggoGrid class, which holds common functions and variables
require_once IGGOGRID_ABSPATH . 'classes/class-iggogrid.php';

// // Start up IggoGrid on WordPress's "init" hook
add_action( 'init', array( 'IggoGrid', 'run') );
register_activation_hook( __file__, array('IggoGrid', 'activate') );

//核心内容(4个) rewrite url 以及获取参数
// 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('IggoGrid', 'create_rewrite_rules'));
add_filter('query_vars',array('IggoGrid', '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('IggoGrid', 'flush_rewrite_rules'));
add_action( 'template_redirect', array('IggoGrid', 'template_redirect_intercept') );

 

2,创建核心类(用于rewrite url以及query vars的函数)

classes/class-iggogrid.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
*IggoGrid Class
*
* @package IggoGrid
* @author Iggo
* @since 1.0.0
*/

// Prohibit direct script loading
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );

/**
* IggoGrid class
* @package IggoGrid
* @author Iggo
* @since 1.0.0
*/
abstract class IggoGrid {
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('iggoGrid/(.+)' => 'index.php?iggoGrid='.$wp_rewrite->preg_index(1));
$newRules = $newRule + $rules;
return $newRules;
}

function add_query_vars($qvars) {
$qvars[] = 'iggoGrid';
return $qvars;
}

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

function template_redirect_intercept() {
global $wp_query;
if ($wp_query->get('iggoGrid')) {
$iggoGrid = base64_decode($wp_query->get('iggoGrid'));
$iggoGrid = explode("&&", $iggoGrid);
if(count($iggoGrid) == 2){
self::pushoutput($iggoGrid[0],$iggoGrid[1]);
}
exit;
}
}

function pushoutput($table,$id) {
if(is_numeric($id) && is_string($table)){
global $wpdb;
$GLOBALS['iggoGridInfoData'] = '';

$id = $id - 1;
$table_name = $wpdb->prefix .$table;
$sql = "SELECT * FROM {$table_name} limit {$id},1";
$GLOBALS['iggoGridInfoData'] = (array)$wpdb->get_row($sql);
if(!empty($GLOBALS['iggoGridInfoData'])){
echo self::locate_plugin_template('page.php',true);
}
}
exit;
}

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

function locate_plugin_template($template_name, $load = false, $require_once = true )
{
// if ( !is_array($template_names) )
// return '';

$located = '';

// $this_plugin_dir = WP_PLUGIN_DIR.'/'.str_replace( basename( __FILE__), "", plugin_basename(__FILE__) );
$this_theme_dir = IGGOGRID_ABSPATH.'/themes/default/';
// foreach ( $template_names as $template_name ) {
// if ( !$template_name )
// continue;
// if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
// $located = STYLESHEETPATH . '/' . $template_name;
// break;
// } else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
// $located = TEMPLATEPATH . '/' . $template_name;
// break;
// } else if ( file_exists( $this_plugin_dir . $template_name) ) {
// $located = $this_plugin_dir . $template_name;
// break;
// }
// }
if ( file_exists( $this_theme_dir . $template_name) ) {
$located = $this_theme_dir . $template_name;
}else{
return '';
}
if ( $load && '' != $located ){
load_template( $located, $require_once );
}

return $located;
}
} // class IggoGrid

 

 

3,创建前台模板文件

themes/default/page.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
get_header(); ?>

<div id="main-content" class="main-content">

<?php
if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
// Include the featured content template.
get_template_part( 'featured-content' );
}
?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<div class="entry-content" role="grid">
<table border="1">
<?php foreach ($GLOBALS['iggoGridInfoData'] as $key=>$value):?>
<tr>
<td><?php echo $key?></td>
<td><?php echo $value?></td>
</tr>
<?php endforeach;?>
</table>
</div>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar( 'content' ); ?>
</div><!-- #main-content -->

<?php
get_sidebar();
get_footer();

 

 

4。测试

浏览器访问:http://wordpress.com/?iggoGrid=aWdnb19ncmlkJiYx

iggoGrid的值被加密了,原参数是:

$params = base64_encode($this->table[‘name’].”&&”.$row_idx);
$url = get_site_url().’/?iggoGrid=’.$params;

 

坚持原创技术分享,您的支持将鼓励我继续创作!