【问题标题】:How to map an arbitrary URL to a function in a Wordpress plugin如何将任意 URL 映射到 Wordpress 插件中的函数
【发布时间】:2016-09-02 04:28:23
【问题描述】:

我正在尝试创建一个 Wordpress 插件,它可以根据 XXX 的值将 example.com/redirect/XXX 的访问者重定向到不同的页面。我想我知道如何执行重定向逻辑,但我不知道如何确保当访问者访问 example.com/redirect 时会调用我的 Wordpress 插件函数。现在我只得到一个 404。有 other solutions 涉及更改 .htaccess 文件,但我希望它可以作为独立插件运行。谢谢!

【问题讨论】:

    标签: wordpress plugins


    【解决方案1】:

    当我需要这种东西时,我只需创建一个带有模板作为“插件”的通用页面,一个包含我需要的所有功能的页面。

    例如,如果我需要一个购物车,我只需创建一个 cart.php 为:

    <?php
    /*
    Template Name: Cart
    */
    // functions here
    ?>
    

    然后我去我的 wp-admin 并创建一个以购物车作为模板的页面。

    【讨论】:

      【解决方案2】:

      取决于你到底想做什么,这很模糊(当你说页面时,你实际上是指页面?发布?cpt?当你说插件功能时 - 它们是什么?你使用永久链接吗?) .. 但在某些情况下,您可以使用 wp conditionals

      示例(来自法典)

          is_singular( 'foo' ) 
         // Returns true if the post_type is "foo". execute plugin hook
          is_singular( array( 'foo', 'bar', 'baz' ) ) 
         // Returns true if the post_type is "foo", "bar", or "baz".
         // See also the Custom Post Types book.
      

      或者,如果您的目标是过滤,您可以随时使用is_main_query() 或任何其他条件连接到pre_get_post //

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-29
        • 1970-01-01
        • 2017-01-18
        • 1970-01-01
        • 2012-03-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多