嗯,我不太确定我是否理解这一点,但我知道你想使用插件“模拟”页面,我的最佳选择是使用WordPress查询和重写规则动态创建自己的帖子。让我们试试这个:
创建一个变量以响应您的视图。
add_action( \'query_vars\', \'add_query_vars\' );
function add_query_vars( $vars ) {
array_push( $vars, \'form_id\' );
return $vars;
}
创建重写规则以填充此变量:
add_action( \'rewrite_rules_array\', \'rewrite_rules\' );
function rewrite_rules( $rules ) {
$new_rules = array(
\'forms/([^/]+)/?$\' => \'index.php?form_id=$matches[1]\'
);
return $new_rules + $rules;
}
现在,访问您网站的
options-permalink.php
页面刷新规则并使上述规则有效(
http://yourdevsite.com/wp-admin/options-permalink.php).
您可以访问自定义URL,如http://yourdevsite.com/forms/some-form 或同等产品http://yourdevsite.com/?form_id=some-form.
现在,就像在WordPress中一样,我们不能抑制主查询,让我们在匹配form_id
发生时间:
add_action( \'wp\', \'custom_wp_query\' );
function custom_wp_query( $wp ) {
// Don\'t do anything for other queries
if ( ! $form_id = get_query_var(\'form_id\') )
return false;
global $wp_query;
// Let\'s respond this request with this function
$func = \'form_\' . str_replace( \'-\', \'_\', $form_id );
// Throw a 404 if there\'s no function to deal with this request
if ( ! function_exists( $func ) ) {
$wp_query->is_404 = true;
return false;
}
// Set as a valid query for this case
$wp_query->is_404 = false;
$wp_query->is_single = true;
$wp_query->found_posts = 1;
// Call the function
$post = call_user_func( $func );
// Stick this post into the query
$wp_query->posts = array( $post );
$wp_query->post = $post;
}
最后创建您的帖子:
function form_some_form() {
return (object) array(
// Put a negative ID as they don\'t exist
\'ID\' => rand() * -1,
// What matters for us
\'post_title\' => \'Form title\',
\'post_content\' => \'Some post content (the form itself, presumably)\',
// It is important to maintain the same URL structure of \'add_rewrite_rules\',
// otherwise wrong links will be displayed in the template
\'post_name\' => \'forms/\' . get_query_var( \'form_id\' ),
\'post_guid\' => home_url( \'?form_id=\' . get_query_var( \'form_id\' ) ),
// Admin
\'post_author\' => 1,
// Straighforward stuff
\'post_date\' => date( \'mysql\' ),
\'post_date_gmt\' => date( \'mysql\' ),
\'post_status\' => \'publish\',
\'post_type\' => \'page\',
\'comment_status\' => \'closed\',
\'ping_status\' => \'closed\'
);
}
所以,如果您现在想为一个具有
some-other-form
URL,只需创建一个名为
form_some_other_form
就像
form_some_form
.
显然,编辑链接将无法工作,因为它会将您发送到admin中不存在的帖子。
对于菜单,如您所问,我建议将这些页面作为自定义链接插入。