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.

Don’t automatically add title attribute when inserting GB image block

  • If alt text & a title are added to an image in the media library, the default WordPress image block adds the alt text to the image on the page, and does not add the title attribute.

    When using a GB image block, both the alt text and the title attribute are added to the image on the page.

    Is there a function or code snippet that can prevent the title attribute from being added to the image when a GB image block is used?

    Deleting it manually after every image insertion is very labor intensive. And I’d love to not have to remove it from all images in my media library, which has thousands of images. And the title helps when searching for images in the media library.

  • Hi there,

    You can remove the title attributes from the GB image block via the advanced panel, you do not need to delete the title from your media library.

    To automatically do it, try this PHP code to remove title attributes from images:

    function remove_title_from_images_only( $content ) {
        // Callback function to process each img tag found
        $content = preg_replace_callback(
            '/<img[^>]+>/i', 
            function( $matches ) {
                // $matches[0] is the full <img> tag
                // Remove title="value" or title='value'
                return preg_replace( '/\s+title=["\'][^"\']*["\']/', '', $matches[0] );
            }, 
            $content 
        );
        return $content;
    }
    add_filter( 'the_content', 'remove_title_from_images_only' );
  • I was aware of how to remove the title attributes one at a time from the Advanced panel, but I was looking for a way to do it programmatically.

    That PHP snippet you provided is working perfectly. Exactly what I was looking for. Thanks Ying! 🙏

  • You are welcome   🙂

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