Support » Fixing WordPress » How to have images in posts open in new window

  • Moderator kmessinger

    (@kmessinger)


    Is there a way (perhaps in functions.php) to have any inmages added to post with the image uploader have target=”_blank” added to the path.

    I can do it after adding the image by clicking on “link” but I seem to forget to do that most of the time. It would be better (for me) if it was auto as part of the a href=

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi,

    Try with this function:

    $(document).ready(function() {
    $(“a[@href^=http]”).each(
    function(){
    if(this.href.indexOf(location.hostname) == -1) {
    $(this).attr(‘target’, ‘_blank’);
    }})
    });

    Thanks,

    Shane G.

    Moderator kmessinger

    (@kmessinger)

    Hi Shane,

    I added this,
    /**
    * Add target new to images in posts.
    */
    function $(document).ready(function() {
    $(“a[@href^=http]”).each(
    function(){
    if(this.href.indexOf(location.hostname) == -1) {
    $(this).attr(‘target’, ‘_blank’);
    }})
    });

    to function.php and my site would not open.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to have images in posts open in new window’ is closed to new replies.