假设您有一个带有slug“教学门户”的页面,该插件会为该页面添加一个重写规则,允许在URL中的该页面之后添加一个部分,即您所称的“mygetdata”。我把它叫做“教训”。
<?php
/**
* Plugin Name: Custom PHP app on teaching-portal page
* Plugin URI: http://wordpress.stackexchange.com/questions/75991
*/
// Flush rewrite rules on plugin activation
register_activation_hook(__FILE__, \'wpse75991_activate\');
function wpse75991_activate()
{
wpse75991_add_rewrite_rule();
flush_rewrite_rules();
}
// Flush rewrite rules on plugin deactivation
register_deactivation_hook(__FILE__, \'wpse75991_deactivate\');
function wpse75991_deactivate()
{
flush_rewrite_rules();
}
// Add a custom rewrite rule for the teaching portal section
add_action(\'init\', \'wpse75991_add_rewrite_rule\');
function wpse75991_add_rewrite_rule()
{
$pagename = \'teaching-portal\'; // Slug
add_rewrite_rule(
$pagename.\'/?([^/]*+)\',
\'index.php?pagename=\'.$pagename.\'&lesson=$matches[1]\',
\'top\'
);
}
// Register the custom query var so WP recognizes it
add_filter(\'query_vars\', \'wpse75991_add_query_var\');
function wpse75991_add_query_var($vars)
{
$vars[] = \'lesson\';
return $vars;
}
激活此插件后,您应该能够访问页面模板中的课程变量。下面是一个简单的例子:
$lesson = get_query_var(\'lesson\');
if (empty($lesson))
{
// Show all lessons
}
else
{
// Show a single lesson
var_dump($lesson);
}
另请参见
Rewrite API 在法典中。