yanuar
yanuar

Reputation: 3

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 :)

Upvotes: 0

Views: 1046

Answers (2)

TriForce
TriForce

Reputation: 415

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);
}

Upvotes: 1

Soykot
Soykot

Reputation: 138

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

Upvotes: 0

Related Questions