Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Open Image Link in New Tab

  • Hi,

    I have a set of product images displayed in my right sidebar. The associated URLs are stored in an ACF custom field associated with post. I select these URLs as links via Link Source = Post Meta, Post Meta Field = the ACF field that stores the link. Everything works great, but I don’t see any option to open the links in a new tab.

    How do I address this?

    Wayne

  • Hi there,

    Can I see the page by any chance?

  • Sure. Main page, right side: https://rjsilverauthor.com/

  • Select the Image block in the Query Loop, then under Advanced > Additional CSS Class(es) add this:
    img-open-new-tab

    After that add this PHP snippet:

    add_filter( 'render_block', function( $block_content, $block ) {
    	if ( ! is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'img-open-new-tab' ) !== false ) {
    		$my_search = 'href="';
    		$my_replace = 'target="_blank" href="';
    		$new_content = str_replace( $my_search, $my_replace, $block_content );
    		return $new_content;
    	}
    	return $block_content;
    }, 10, 2 );

    Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

  • Hi Alvind,

    That worked. Thank you for your excellent support. That has been evident since Day 1.

    Wayne

  • You’re welcome!

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.