Php WordPress钩子/过滤器处理帖子内的链接
是否有一个钩子/过滤器来处理添加到WordPress帖子中的链接 我的目标是使用以下按钮对插入帖子的链接进行预处理并使用第三方API(如bit.ly)缩短链接 我想对内部/外部链接都这样做。 我想到的一个解决方案是在我的编辑器中添加一个额外的按钮来实现这一点,但我更喜欢一个钩子/过滤器来完成这项工作,这样它会更干净,我会将其转换为我网站的自定义插件(通过允许我的WordPress升级) 我浏览了WordPress文档,浏览了以下对我没有用的钩子/过滤器Php WordPress钩子/过滤器处理帖子内的链接,php,wordpress,Php,Wordpress,是否有一个钩子/过滤器来处理添加到WordPress帖子中的链接 我的目标是使用以下按钮对插入帖子的链接进行预处理并使用第三方API(如bit.ly)缩短链接 我想对内部/外部链接都这样做。 我想到的一个解决方案是在我的编辑器中添加一个额外的按钮来实现这一点,但我更喜欢一个钩子/过滤器来完成这项工作,这样它会更干净,我会将其转换为我网站的自定义插件(通过允许我的WordPress升级) 我浏览了WordPress文档,浏览了以下对我没有用的钩子/过滤器 这里列出的大多数 您是否考虑过在保
您是否考虑过在保存帖子之前不处理链接?wordpress插件api中有一个名为“save_post”的操作在保存时触发 使用“save_post”,您可以解析帖子的内容,然后使用url缩短器替换链接
更新1:据我所知,外部URL是通过TinyMCE编辑器链接插件插入到帖子内容中的,PHP什么都不做 在WordPress中,有两个插件位于
wp includes/js/wplink.js
和wp includes/js/tinymce/plugins/wplink/plugin.js
。请注意,如果您未处于SCRIPT\u DEBUG
模式,则它们具有.min
后缀
wpLink.getAttrs
方法。例如,要向每个URL添加so39115564
字符串:
jQuery(document).ready(function($) {
wpLink.getAttrs = function() {
wpLink.correctURL();
return {
href: $.trim( $("#wp-link-url").val() + "so39115564" ),
target: $("#wp-link-target").prop("checked") ? "_blank" : ""
};
}
});
更多信息,请查看wp-includes/js/wplink.js。这里太长了,无法详细解释
假设上面的脚本是mylink.js
,下面是我们应该如何将其排队:
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);
tinymce.ui.WPLinkPreview的setURL
方法。但是,这几乎是不可能的,除非您取消注册此脚本并注册修改后的版本。然后使用WordPress中不可预知的更改自行管理该脚本
现在,明智地选择它!在将外部URL粘贴到帖子中之前,请先缩短它们,或者使用WordPress TinyMCE插件,或者使用wp includes/js/wplink.js
plugin的对话框
对!!WordPress通过函数执行的wp\u link\u ajax
操作插入内联链接
正如您在该函数的源代码中所看到的,$results
由\u WP\u Editors::WP\u link\u query
检索。检查此方法,您将遇到筛选器。此筛选器接受两个参数:$results
和$query
<代码>$results
将是我们需要筛选的内容
例如,我们需要将so39115564
附加到查询的链接:
add_filter('wp_link_query', function(array $results, array $query)
{
$results[0]['permalink'] = $results[0]['permalink'] . 'so39115564';
return $results;
}, PHP_INT_MAX, 2);
现在,你应该知道怎么做了。请确保查看以更高效地过滤
$results
。可能有用于此的插件,但下面是默认逻辑
第1种方式:将缩短的URL也保存在DB中
add_action('save_post','save_book_meta',10,3);
函数save\u book\u meta($post\u id,$post,$update){
全球$wpdb;
$slug='event';
//如果这不是一个“书”帖子,不要更新它。
如果($slug!=$post->post\u类型){
返回;
}
预赛|
我在TinyMCE方面并不完美,因为我在几个项目中没有使用TinyMCE,但我有javascript方面的经验,所以我找到了一个TinyMCE事件api来使用,您可以创建一个简短的url。我查看了WP最新版本和TinyMCE 4.x版本。您还可以找到合适的api来触发自定义回调
在wordpress编辑器中进行tinymce初始化之前,使用wordpress hooktiny\u mce\u init注册设置函数
add_filter( 'tiny_mce_before_init', 'my_plugin_tiny_mce_before_init' );
function my_plugin_tiny_mce_before_init( $in ){
$in['setup'] = 'myplugin_tinymce_setup'; // javascript callback
return $in;
}
add_action( 'admin_footer', 'add_admin_footer' );
在管理员页脚中添加javascript回调后,我有两种方法来解决这个问题
#1方法
function add_admin_footer(){
?>
<script type="text/javascript">
var counter = 1, insert_url, shorten_url;
function myplugin_tinymce_setup( editor ){
editor.onNodeChange.add(function(ed, cm, e) {
// Activates the link button when the caret is placed in a anchor element
if (e.nodeName.toLowerCase() == 'a'){
var element = jQuery(e).not('a[href="_wp_link_placeholder"],a[data-wplink-edit="true"]');
if( element.length ){
if( is_shorten = getShortenUrl( element.attr( 'href') ) ){
element.attr( 'href', is_shorten );
element.attr( 'data-mce-href', is_shorten );
}
}
}
});
var hostname = new RegExp(location.host);
var shorten_host = new RegExp('http://www.example.com'); // enter your short url generate host name
function getShortenUrl( url ){
// add more condition here...
// Add here ajax or shorten javascript api etc.
// filter and return short url
if( hostname.test( url ) ){
// internal link
// Return internal url with shorten
return 'http://www.example.com/Internal/'+counter++;
}else if( shorten_host.test( url ) ){
// not update already update with shorten url
return false;
}else{
// Return external url with shorten
return 'http://www.example.com/External/'+counter++;
}
}
</script>
<?php
function add_admin_footer(){
?>
<script type="text/javascript">
var counter = 1, insert_url, shorten_url;
function myplugin_tinymce_setup( editor ){
editor.on('ExecCommand', function(e) {
// Eexcute command when click on button apply on insert/edit link
if( e.command == 'wp_link_apply' ){
if( editor.selection.getContent() ){
element = tinymce.$(editor.selection.getContent()); // Create new link
}else{
element = tinymce.$(editor.selection.getNode()); // Edit link option
}
if( element.prop('tagName').toLowerCase() == 'a' ){
current_link_url = element.attr('href'); // Real url added in editor.
// and also check current_link_url is not shorten url
// add more logic here. Get short url using ajax or bitly javascript api.
if( is_shorten_url = getShortenUrl( element.attr('href')) ){
editor.execCommand('mceInsertLink',false,{ href: is_shorten_url });
}
}
}
});
}
var hostname = new RegExp(location.host);
var shorten_host = new RegExp('http://www.example.com'); // enter your short url generate host name
function getShortenUrl( url ){
// add more condition here...
// Add here ajax or shorten javascript api etc.
// filter and return short url
if( hostname.test( url ) ){
// internal link
// Return internal url with shorten
return 'http://www.example.com/Internal/'+counter++;
}else if( shorten_host.test( url ) ){
// not update already update with shorten url
return false;
}else{
// Return external url with shorten
return 'http://www.example.com/External/'+counter++;
}
}
</script>
<?php
}
函数添加\管理\页脚(){
?>
var counter=1,插入\u url,缩短\u url;
函数myplugin\u tinymce\u设置(编辑器){
editor.onNodeChange.add(函数(ed、cm、e){
//当插入符号放置在锚元素中时,激活链接按钮
if(e.nodeName.toLowerCase()=='a'){
var element=jQuery(e).not('a[href=“\u wp\u link\u placeholder”]、a[data wplink edit=“true”]);
if(element.length){
如果(is_shorten=getShortenUrl(element.attr('href')){
attr('href',是_shorten);
attr('data mce href',is_shorten);
}
}
}
});
var hostname=newregexp(location.host);
var shorten_host=new RegExp('http://www.example.com“);//输入您的短url生成主机名
函数getShortenUrl(url){
//在这里添加更多条件。。。
//在此添加ajax或缩短javascript api等。
//筛选并返回短url
if(hostname.test(url)){
//内部链接
//返回带有缩写的内部url
返回'http://www.example.com/Internal/“+计数器++;
}else if(缩短主机测试(url)){
//未更新已使用缩短url更新
返回false;
}否则{
//返回带有缩写的外部url
返回'http://www.example.com/External/“+计数器++;
}
}
var counter=1,插入\u url,缩短\u url;
函数myplugin\u tinymce\u设置(编辑器){
编辑器.on('ExecCommand',函数(e){
//单击按钮ap时执行Eexcute命令
function add_admin_footer(){
?>
<script type="text/javascript">
var counter = 1, insert_url, shorten_url;
function myplugin_tinymce_setup( editor ){
editor.on('ExecCommand', function(e) {
// Eexcute command when click on button apply on insert/edit link
if( e.command == 'wp_link_apply' ){
if( editor.selection.getContent() ){
element = tinymce.$(editor.selection.getContent()); // Create new link
}else{
element = tinymce.$(editor.selection.getNode()); // Edit link option
}
if( element.prop('tagName').toLowerCase() == 'a' ){
current_link_url = element.attr('href'); // Real url added in editor.
// and also check current_link_url is not shorten url
// add more logic here. Get short url using ajax or bitly javascript api.
if( is_shorten_url = getShortenUrl( element.attr('href')) ){
editor.execCommand('mceInsertLink',false,{ href: is_shorten_url });
}
}
}
});
}
var hostname = new RegExp(location.host);
var shorten_host = new RegExp('http://www.example.com'); // enter your short url generate host name
function getShortenUrl( url ){
// add more condition here...
// Add here ajax or shorten javascript api etc.
// filter and return short url
if( hostname.test( url ) ){
// internal link
// Return internal url with shorten
return 'http://www.example.com/Internal/'+counter++;
}else if( shorten_host.test( url ) ){
// not update already update with shorten url
return false;
}else{
// Return external url with shorten
return 'http://www.example.com/External/'+counter++;
}
}
</script>
<?php
}