WordPress.org

Ready to get started?Download WordPress

Forums

How to have images in posts open in new window (3 posts)

  1. kmessinger
    Volunteer Moderator
    Posted 5 years ago #

    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=

  2. Shane G
    Member
    Posted 5 years ago #

    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.

  3. kmessinger
    Volunteer Moderator
    Posted 5 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.