0
votes

I need to select an image in wordpress visual editor to edit its url in a shortcode dialog window. It work fine and the img url is replaced as well:

var content = editor.selection.getContent();
content = content.replace(original_url, new_url);
editor.selection.setContent(content);

But my problem is to replace also the parent selected image <a href> attribute with the new_url value.

The editor.selection.getContent() function return only the img tag:

<img class="wp-image-1007 size-medium" src="**original_url**" width="373" height="250" />

and not:

<a href="**original_url**" target="_blank">
<img class="wp-image-1007 size-medium" src="**original_url**" width="373" height="250" />
</a>

With jQuery i could do:

var imgsel = editor.selection.getNode();
jQuery(imgsel).parent("a").attr("href", new_url);

but inside the editor doesn't works. How can i replace the image link url using editor.selection.setContent() function? Thanks

1

1 Answers

0
votes

Try this code , use selection.getNode();

var img = editor.selection.getNode();
img.setAttribute('src' , newURL);
var parent  = editor.dom.getParent(img,'a');
parent.setAttribute('href' ,newURL);