[英]change href to onclick
On wordpress, does anyone know how to programatically change link from: 在wordpress上,有谁知道如何以编程方式更改链接的来源:
<a href="some_url">click</a>
to: 至:
<a onclick="window.open('some_url','_blank', 'location=no')">click</a>
so that all links created in the wordpress visual editor be opened via inappbrowser in a cordova app. 以便可以在cordova应用程序中通过inappbrowser打开在wordpress可视编辑器中创建的所有链接。
After googling around, below is the closest that i can get, but still doesn't work, the '%link%'
variable doesn't change to the actual link url : 谷歌搜索后,下面是我可以获得的最接近的,但仍然无法正常工作, '%link%'
变量不会更改为实际的链接URL:
add_filter('the_content', 'changeToOnclick');
function changeToOnclick($content) {
return preg_replace('/<a [^>]*>/', "<a onclick=\"window.open('%link%', '_blank', 'location=no')\">", $content);
}
any help will be appreciated :) 任何帮助将不胜感激 :)
The correct way to do exactly what you want is this: 正确执行所需操作的正确方法是:
add_filter('the_content', 'changeToOnclick');
function changeToOnclick($content) {
return preg_replace('/<a href="(.+?)">/', '<a onclick="window.open(\'$1\', \'_blank\', \'location=no\');">',$content);
}
to do search and replace using wp filter you can do this : 要使用wp过滤器进行搜索和替换,您可以执行以下操作:
function change_submenu_class($menu) {
$menu = preg_replace('/ class="sub-menu"/','/ class="dropdown" /',$menu);
return $menu;
}
add_filter('wp_nav_menu','change_submenu_class');
So just replace the parts of javascript with this and give it a try 因此,只需将javascript的各个部分替换为此,然后尝试一下
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.