动态创建wp页面

动态创建wp页面

问题描述:

I'm using a custom page template that is intended to create a dynamic content using the wpdb class and some external tables within the database. I'm trying to keep the url's structure as clean as possible, hence im using the 'post name' option in the permalinks..

I'll just give the whole flow so it won't be confusing.

  1. An user is trying to reach www.domain.com/dynamic/347
  2. The template is searching for '347' in the database and creates a special dedicated page
  3. All the rendering is being executed within the template itself (the one that the /dynamic page is using)

The problem is with the mod_rewrite - Since I'm using the post_name option in wp's permalinks - the wordpress itself is trying to access an actual page called '347' under the parent of 'dynamic', instead of rendering dynamic's template and use 347 as a parameter, by 'exploding' the url and retrieve the last value as the dynamic parameter, like this -

$str = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$dynamic_para = explode("/", $str, 4); // in this case - 347
//... retrieve dynamic_para from db and stuff with it.

How can I bypass wp's current mod_rewrite specifically for this page? Is it even possible? I am trying to avoid sending GET variables with the url, e.g. www.domain.com/dynamic/?para=347

我正在使用自定义页面模板,该模板旨在使用wpdb类和一些外部表创建动态内容 在数据库中。 我试图保持url的结构尽可能干净,因此我使用永久链接中的'post name'选项.. p>

我只是给它 整个流程,所以不会让人感到困惑。 p>

  1. 用户正试图访问www.domain.com/dynamic/347 li>
  2. 模板在数据库中搜索'347'并创建一个特殊的专用页面 li>
  3. 所有渲染都在模板本身内执行(/动态页面正在使用的那个) li > ol>

    问题在于mod_rewrite - 因为我在wp的永久链接中使用了post_name选项 - wordpress本身正试图访问父级下面名为'347'的实际页面 '动态',而不是渲染动态模板并使用347作为参数,通过'爆炸'网址并检索最后一个值 作为动态参数,像这样 - p>

      $ str =“$ _SERVER [HTTP_HOST] $ _ SERVER [REQUEST_URI]”; 
     $ dynamic_para = explode(“/”,$  str,4);  //在这种情况下 -  347 
     // ...从db检索dynamic_para并用它来填充它。
      code>  pre> 
     
     

    如何专门为此绕过wp的当前mod_rewrite 页? 它甚至可能吗? 我试图避免使用url发送GET变量,例如 www.domain.com/dynamic/?para=347

    div>

want is /dynamic/xxx will be rewritten to /dynamic?para=xxx

in WP you should rewrite to index.php with querystring (user still sees "/dynamic/xxx" in their browser address bar)

where exactly did you place the tc_query_vars_filter and the tc_rewrite_rules($rules) functions

You can place these in the (child) theme's functions.php or better create add them to your own simple plugin so they will work with any theme:

e.g. create, upload, and activate:

<?php
/*
Plugin Name: Theme independent functions
Description: NOT TESTED
*/

//  allow WP to store querystring attribs for use in our pages
function your_query_vars_filter($vars) {
  $vars[] = 'para';
  return $vars;
}
add_filter( 'query_vars', 'your_query_vars_filter' );

//  "rewrite" /dynamic/xxx to index.php?pagename=dynamic&para=xxx
function your_rewrite_rules($rules) {
   global $wp_rewrite;
   $your_rule = array( // (not tested)
     'dynamic/(.+)/?' => 'index.php?pagename=dynamic&para=$matches[1]'
   );
   return array_merge($your_rule, $rules);
}
add_filter('page_rewrite_rules', 'your_rewrite_rules');

// any other site customisation stuff you want to add to this plugin
?>

N.B. above filter is for Pages; for POSTS use post_rewrite_rules filter instead

Then in your custom page template:

$my_search_var = get_query_var('para'); // sanitize as reqd

Wordpress "caches" its re-write rules so finally we need to flush them so your new ones will be recognised:

The usual advice is you can flush by simply clicking "Save Changes" button on admin dashboard Permalink page (Settings->Permalinks) However, (either due to a quirk on my site or a change in the latest version of WP?) last time I did this I actually had to edit the permalink, save the change then change it back to what it should be, and save again for my rewrite functions rules to be applied.