通过编程将href更改为onClick=window.open

时间:2017-03-07 作者:yanuar

是否有人知道如何通过编程更改链接:

<a href="some_url">click</a>
收件人:

<a onclick="window.open(\'some_url\',\'_blank\', \'location=no\')">click</a>
这样,在wordpress visual editor中创建的所有链接都可以通过cordova应用程序中的inappbrowser打开。

这是我能得到的最接近的,但仍然不起作用\'%link%\' 变量不会更改为实际链接url:

add_filter(\'the_content\', \'changeToOnclick\');
function changeToOnclick($content) {
    return preg_replace(\'/<a [^>]*>/\', "<a onclick=\\"window.open(\'%link%\', \'_blank\', \'location=no\')\\">", $content);
}
任何帮助都将不胜感激:)

2 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

用正则表达式解析HTML并不理想。更好的选择是使用DOMDocumentDOMXpath;

此代码使用DOMDocumentDOMXpath 在不依赖正则表达式的情况下解析和修改HTML。内容中的每个链接都有onclick 属性与相应的window.open() 使用从href 属性这个href 然后从链接中删除属性。

add_filter( \'the_content\', \'wpse_cordova_links\', 10, 1 );
function wpse_cordova_links( $content ) {
    // Create an instance of DOMDocument.
    $dom = new \\DOMDocument();

    // Suppress errors due to malformed HTML.
    // See http://stackoverflow.com/a/17559716/3059883
    $libxml_previous_state = libxml_use_internal_errors( true );

    // Populate $dom with $content, making sure to handle UTF-8, otherwise
    // problems will occur with UTF-8 characters.
    $dom->loadHTML( mb_convert_encoding( $content, \'HTML-ENTITIES\', \'UTF-8\' ) );

    // Restore previous state of libxml_use_internal_errors() now that we\'re done.
    // Again, see http://stackoverflow.com/a/17559716/3059883
    libxml_use_internal_errors( $libxml_previous_state );

    // Create an instance of DOMXpath.
    $xpath = new \\DOMXpath( $dom );

    // Query all links within our content.
    $links = $xpath->query( \'//a\' );

    // Iterate over the $links.
    foreach ( $links as $link ) {
        if ( $link->hasAttributes() ) {
            // Get the value of the href attribute
            $link_href = $link->getAttribute( \'href\' );

            // Create an onlick attribute and set the value
            $link_onclick = $dom->createAttribute( \'onclick\' );
            $link_onclick->value = "window.open( \'" . $link_href . "\', \'_blank\', \'location=no\' );";
            $link->appendChild( $link_onclick );

            // Remove the href attribute
            $link->removeAttribute( \'href\' );
        }
    }

    // Save the updated HTML
    $content = $dom->saveHTML();    

    return $content;
}

Example HTML before processing

<p><a href="http://example.com/">click</a></p>

<p>Lorem ipsum dolor sit amet, ecclesiam mittam est amet constanter approximavit te. Introivit gubernum defunctam vivum eum ego esse ait mea Christianis<br>
<a class="test-class test-other-class" href="http://example.com/1/">click me too</a> aedificatur ergo accipiet duxit ad te. Ascendi in modo invenit ubi diu requievit agi coepit. Apollonii appropinquat tation ulterius quod ait mea Christianis aedificatur ergo accipiet si mihi esse deprecor cum. Equidem deceptum in fuerat eum est in, quoque sed quod ait est in fuerat.</p>

<p><a data-test="55" href="http://example.com/2/">click me as well</a></p>

<p>Lorem ipsum dolor sit amet, ecclesiam mittam est amet constanter approximavit te. Introivit gubernum defunctam vivum eum ego esse ait mea Christianis aedificatur ergo accipiet duxit ad te. Ascendi in modo invenit ubi diu requievit agi coepit. Apollonii appropinquat tation ulterius quod ait mea Christianis aedificatur ergo accipiet si mihi esse deprecor cum. Equidem deceptum<br>
<a id="test-id" class="test-class" href="http://example.com/3/">clicky</a> in fuerat eum est in, quoque sed quod ait est in <a>This link has no href attribute</a> fuerat.</p>

Example HTML after processing

<p><a onclick="window.open( \'http://example.com/\', \'_blank\', \'location=no\' );">click</a></p>

<p>Lorem ipsum dolor sit amet, ecclesiam mittam est amet constanter approximavit te. Introivit gubernum defunctam vivum eum ego esse ait mea Christianis<br>
<a class="test-class test-other-class" onclick="window.open( \'http://example.com/1/\', \'_blank\', \'location=no\' );">click me too</a> aedificatur ergo accipiet duxit ad te. Ascendi in modo invenit ubi diu requievit agi coepit. Apollonii appropinquat tation ulterius quod ait mea Christianis aedificatur ergo accipiet si mihi esse deprecor cum. Equidem deceptum in fuerat eum est in, quoque sed quod ait est in fuerat.</p>

<p><a data-test="55" onclick="window.open( \'http://example.com/2/\', \'_blank\', \'location=no\' );">click me as well</a></p>

<p>Lorem ipsum dolor sit amet, ecclesiam mittam est amet constanter approximavit te. Introivit gubernum defunctam vivum eum ego esse ait mea Christianis aedificatur ergo accipiet duxit ad te. Ascendi in modo invenit ubi diu requievit agi coepit. Apollonii appropinquat tation ulterius quod ait mea Christianis aedificatur ergo accipiet si mihi esse deprecor cum. Equidem deceptum<br>
<a id="test-id" class="test-class" onclick="window.open( \'http://example.com/3/\', \'_blank\', \'location=no\' );">clicky</a> in fuerat eum est in, quoque sed quod ait est in <a>This link has no href attribute</a> fuerat.</p>

SO网友:Rick Hellewell

有几个插件可以自动做到这一点。一个是“在新窗口中打开外部链接”(可在此处找到https://wordpress.org/plugins/open-external-links-in-a-new-window/ ). 如果您在插件中搜索“外部链接”,您可能会找到其他插件。

我在几个网站上使用这个,效果很好。

如果你想推出自己的插件,只需看看他们使用的代码,因为所有插件(通常)都是开源的。但是,为什么要重新发明轮子。。。。?

相关推荐

Wordpress URL error for links

我的数据库中有以下内容siteurl subdomain.example.com home subdomain.example.com 当我尝试转到某些页面时,页面会转到subdomain.example.com/services/subdomain.example.com这有什么原因吗?由于这个奇怪的错误,我也无法进入wp管理,我不明白为什么会发生这种情况?我尝试用更改数据库中的值http:// 也已经预先准备好了。如有任何建议,我们将不胜感激。