【问题标题】:Output content from wordpress plugin and rewrite rules从 wordpress 插件输出内容并重写规则
【发布时间】:2018-02-10 23:27:06
【问题描述】:

我创建了一个 wordpress 插件,它在the_content 上有一个过滤器,查找特定标签,然后输出插件内容代替该标签。

我现在想使用重写规则来调用插件并在模板中输出数据,但我没有找到太多帮助。

有人可以提供一个示例,或者一些关于如何使用内置 wp 方法添加重写规则并在输出一些内容的插件中调用我的方法的指导。

理想情况下,我希望匹配shop/,然后将购物后的所有内容传递给我插件上的调度方法,以便我可以拥有shop/category/shirtsshop/product/the-cool-shirt。我的调度方法将处理分解其余 url 并相应地调用方法。

【问题讨论】:

    标签: php wordpress plugins url-rewriting


    【解决方案1】:

    这会变得相当有趣。我不得不为一个插件做这件事,我面前没有它,所以它内存不足,但总体思路应该是正确的。

    <?php
    
    add_action('init', 'rewrite_rules');        
    
    
    function rewrite_rules() {
        global $wp, $wp_rewrite;
        $wp_rewrite->add_rule('(widget1|widget2|widget3)/([a-zA-Z0-9_-]{3,50})$', 'index.php?pagename=listing&category=$matches[1]&subcategory=$matches[2]', 'top' );
        $wp->add_query_var( 'category' );
        $wp->add_query_var( 'subcategory' );
        $wp_rewrite->flush_rules();
    }
    
    ?>
    

    使用正则表达式本身就是一项艰巨的任务,我相信我使用过这个网站:http://tools.netshiftmedia.com/regexlibrary/ 寻求帮助。

    我还使用 FakePage 插件来实际显示我的自定义“动态”页面,正如我所说的那样,但我认为 WP 中的所有内容在技术上都是动态的。

    http://scott.sherrillmix.com/blog/blogger/creating-a-better-fake-post-with-a-wordpress-plugin/

    如果您需要更多帮助,请告诉我。

    【讨论】:

      【解决方案2】:

      不久前我做了一件非常相似的事情,我是通过作弊来做到的。

      如果您发现内置的重写规则过于复杂或无法完成这项工作,您可能会发现更容易捕获请求并过滤结果。简化版:

      add_action('parse_request', 'my_parse_request');
      
      function my_parse_request (&$wp) {
        $path = $wp->request;
      
        $groups = array();
        if (preg_match("%shop/product/([a-zA-Z0-9-]+)%", $path, $groups)) {
          $code = $groups[1];
          $product = get_product($code); // your own code here
          if (isset($product)) {
            add_filter('the_posts', 'my_product_filter_posts');
          }
        }
      }
      
      function my_product_filter_posts ($posts) {
        ob_start();
        echo "stuff goes here";  //  your body here
        $content = ob_get_contents();
        ob_end_clean();
      
        return array(new DummyResult(0, "Product name", $content));
      }
      

      解释一下:

      1. 在数据库查找之前调用parse_request 上的操作。它会根据 URL 安装其他操作和过滤器。

      2. 帖子过滤器将数据库查找的结果替换为虚假结果。

      DummyResult 是一个简单的类,它具有与帖子相同的字段,或者它们的数量足以摆脱它:

      class DummyResult {
        public $ID;
        public $post_title;
        public $post_content;
      
        public $post_author;
        public $comment_status = "closed";
        public $post_status = "publish";
        public $ping_status = "closed";
        public $post_type = "page";
        public $post_date = "";
      
        function __construct ($ID, $title, $content) {
          $this->ID = $ID;
          $this->post_title = $title;
          $this->post_content = $content;
      
          $this->post_author = get_default_author(); // implement this function
        }
      }
      

      上面有很多作业留给读者,但这是一种丑陋的工作方法。您可能需要为template_redirect 添加一个过滤器,以将普通页面模板替换为特定于产品的页面模板。如果你想要漂亮的永久链接,你可能需要调整 URL 正则表达式。

      【讨论】:

        猜你喜欢
        • 2012-11-21
        • 2023-03-27
        • 2016-03-13
        • 2012-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-27
        相关资源
        最近更新 更多