WordPress 钩子/过滤器来处理帖子内的链接 [英] WordPress hook / filter to process link inside a post

查看:24
本文介绍了WordPress 钩子/过滤器来处理帖子内的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有挂钩/过滤器来处理添加到 WordPress 帖子中的链接?

我的目标是使用以下按钮预处理插入帖子中的链接,并使用第三方 API(如 bit.ly)缩短它

我想为内部/外部链接执行此操作.

我想到的一个解决方案是在我的编辑器中添加一个额外的按钮来做到这一点,但我更喜欢一个钩子/过滤器来完成这项工作,这样它会更干净和我会将其转换为我网站的自定义插件(通过允许我的 WordPress 升级).

我浏览了 WordPress 文档并浏览了以下对我没有用的钩子/过滤器

  1. 要过滤通过此对话框插入的 URL,我们必须覆盖 wpLink.getAttrs 方法.例如,将 so39115564 字符串添加到每个 URL:

    jQuery(document).ready(function($) {wpLink.getAttrs = function() {wpLink.correctURL();返回 {href: $.trim( $("#wp-link-url").val() + "so39115564" ),目标:$("#wp-link-target").prop("checked") ?_空白的" : ""};}});

    您应该查看 wp-includes/js/wplink.js 以获取更多信息.在这里详细解释太长了.

    假设上面的脚本是mylink.js,我们应该如何将它排入队列:

    add_filter('admin_enqueue_scripts', function(){//{ 此处可能是有效屏幕的一些条件.}wp_enqueue_script('mylink', 'link/to/the/mylink.js', ['link'], '1.0', true);}, 0, 0);

    1. wp-includes/js/tinymce/plugins/wplink/plugin.js 处理这个对话框:

    这一次,我们还要重写tinymce.ui.WPLinkPreviewsetURL方法.但是,这几乎不可能,除非您取消注册此脚本并注册修改后的版本.然后使用来自 WordPress 的不可预测的更改自行管理该脚本.

    现在,明智地选择它!在将它们粘贴到您的帖子中之前缩短任何外部 URL,或者弄乱 WordPress TinyMCE 插件或使用 wp-includes/js/wplink.js 插件的对话框.

    <小时>

    是的!WordPress 通过 wp_link_ajax 操作插入内联链接,该操作由 wp_ajax_wp_link_ajax()<执行/a> 函数.

    正如您在该函数的源代码中所见,$results_WP_Editors::wp_link_query 检索.看看这个方法,你会遇到 wp_link_query 过滤器.此过滤器接受两个参数:$results$query.$results 将是我们需要过滤的内容.

    例如,我们需要在查询的链接后附加so39115564:

    add_filter('wp_link_query', function(array $results, array $query){$results[0]['permalink'] = $results[0]['permalink'] .'so39115564';返回 $results;}, PHP_INT_MAX, 2);

    现在,你应该知道怎么做了.请务必查看 _WP_Editors::wp_link_query 以过滤 $results 更有效率.

    Is there a hook / filter to process the link being added into a WordPress post ?

    My aim is to pre-process the link inserted in a post using the following button and shorten it using a third party API like bit.ly

    I want to do this for both internal / external links.

    One solution that I an think of is to add an extra button to my editor that does this but I would prefer a hook / filter that does the job, that way it would be more clean and I will convert that into a custom plugin for my website (there by allowing my WordPress to be up-gradable).

    I went through the WordPress docs and skimmed through the following hooks / filters which were of no use to me

    1. https://developer.wordpress.org/reference/hooks/add_link/

    2. https://developer.wordpress.org/reference/hooks/post_link/

    3. And most of the ones listed here https://developer.wordpress.org/?s=link

    解决方案

    Update 1: As far as I know, external URLs are inserted into post content by TinyMCE editor link plugin, PHP does nothing.

    In WordPress, there're two plugins which located at wp-includes/js/wplink.js and wp-includes/js/tinymce/plugins/wplink/plugin.js. Note that if you're not in SCRIPT_DEBUG mode, they have .min suffix.

    1. wp-includes/js/wplink.js handles this dialog:

    To filter URLs inserted via this dialog box, we must override wpLink.getAttrs method. For example, to add so39115564 string to every URLs:

    jQuery(document).ready(function($) {
      wpLink.getAttrs = function() {
        wpLink.correctURL();
        return {
          href: $.trim( $("#wp-link-url").val() + "so39115564" ),
          target: $("#wp-link-target").prop("checked") ? "_blank" : ""
        };
      }
    });
    

    You should take a look at the wp-includes/js/wplink.js for more info. It's too long to explain in detail here.

    And let say the above script is mylink.js, here is how we should enqueue it:

    add_filter('admin_enqueue_scripts', function()
    {
      // { Maybe some conditions for a valid screen here. }
    
      wp_enqueue_script('mylink', 'link/to/the/mylink.js', ['link'], '1.0', true);
    }, 0, 0);
    

    1. wp-includes/js/tinymce/plugins/wplink/plugin.js handles this dialog:

    This time, we also have to override setURL method of tinymce.ui.WPLinkPreview. But, it's almost impossible, unless you deregister this script and register a modified version. Then manage that script yourself with unpredictable changes from WordPress.

    Now, choose it wisely! Shorten any external URLs before pasting them into your posts or mess up with WordPress TinyMCE plugins or use dialog box of wp-includes/js/wplink.js plugin.


    Yes! WordPress inserts inline links via wp_link_ajax action which is executed by wp_ajax_wp_link_ajax() function.

    As you can see in source code of that function, $results is retrieved by _WP_Editors::wp_link_query. Check out this method, you will meet wp_link_query filter. This filter accept two arguments: $results and $query. $results will be what we need to filter.

    For example, we need to append so39115564 to the queried link:

    add_filter('wp_link_query', function(array $results, array $query)
    {
      $results[0]['permalink'] = $results[0]['permalink'] . 'so39115564';
    
      return $results;
    }, PHP_INT_MAX, 2);
    

    Now, you should know how to do it. Make sure to take a look at _WP_Editors::wp_link_query to filter the $results more efficient.

    这篇关于WordPress 钩子/过滤器来处理帖子内的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆