简体   繁体   中英

change href to onclick

On wordpress, does anyone know how to programatically change link from:

<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.

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 :

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM