【问题标题】:How to change the URL of the edit post link?如何更改编辑帖子链接的 URL?
【发布时间】:2020-08-07 01:14:53
【问题描述】:

我想更改编辑帖子链接的 URL,以便将您定向到我自己的自定义编辑页面。我一直在寻找过滤器或函数,但我只能找到edit_post_link() 函数和get_edit_post_link() 函数。从documentation 可以看到,edit_post_link 仅更改链接文本,而不更改 URL。我相信get_edit_post_link 会为您获取 URL。

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    您需要向get_edit_post_link 添加过滤器。这是未经测试的,但类似于:

    add_filter( 'get_edit_post_link', 'my_edit_post_link' );
    function my_edit_post_link( $url, $post->ID, $context) {
        $url = //However you want to generate your link
    
        return $url;
    }
    

    【讨论】:

    • 我希望我只是在等待回复,哈哈。我一直试图弄清楚,最终发现 wordpress 打印了 link-template.php 中的链接,所以我只是修改了它。但是,这不适用于编写插件,所以我将使用 get_edit_post_link 代替,谢谢。
    • 如果这是适合您情况的“正确”答案,请记得点赞,更重要的是,将答案标记为已接受,以便其他人知道解决方案是什么。
    • @JenniferO'Brien 单击答案旁边向上/向下投票箭头下方的复选按钮。截图:d.pr/i/LSmF
    • $post->ID 作为函数的参数抛出错误。我知道这是未经测试的,但有没有可行的解决方案?
    • @Derek 是的,应该是 $post_id,而不是 $post->ID。另外,对 add_filter 的调用需要指定它应该接受三个参数,否则它默认为 1 并且不起作用。即add_filter( 'get_edit_post_link', 'my_edit_post_link', 10, 3 ); 参见working example, below,如果你碰巧仍然关心这个,五年后......
    【解决方案2】:

    工作版本:

    add_filter( 'get_edit_post_link', 'my_edit_post_link', 10, 3 );
    function my_edit_post_link( $url, $post_id, $context) {
        $url = "http://somethingerother.com/custom_post_editor.php?post=".$post_id; // Modify the URL as desired
    
        return $url;
    }
    

    【讨论】:

      【解决方案3】:

      我的 wordpress 开发技能有点生疏,但您可能应该使用 wordpress hooks 挂钩 edit_post ,并编写您的插件来重写返回值或 do_action 去您自己的自定义编辑页面

      【讨论】:

      • 我知道钩子,edit_post钩子只是修改你提交文本编辑器时发生的事情,它与编辑帖子链接无关。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 1970-01-01
      • 2014-01-09
      • 2013-01-09
      • 2017-08-31
      相关资源
      最近更新 更多