Skip to content

Featured Image Block: Prevent default action on image click to fix linking to post #69716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

Mayank-Tripathi32
Copy link
Contributor

@Mayank-Tripathi32 Mayank-Tripathi32 commented Mar 26, 2025

Fixes #69714

What?

Fixes an issue where the link inside the Featured Image block remains functional even when no image is set.

Why?

Currently, if you click the button from the image placeholder without setting a featured image, the link still works. This is unexpected behavior and may lead to confusion.

A related PR: #68775

How?

  • Add PreventDefault to button from further opening the link inside editor.
  <Button
  __next40pxDefaultSize
  icon={ upload }
  variant="primary"
  label={ label }
  showTooltip
  tooltipPosition="top center"
  onClick={ ( e ) => {
	   e.preventDefault();
	   open();
   } }
   />

Testing Instructions

  1. Open a post or page.
  2. Insert a Featured Image block.
  3. Setting a link without setting a featured image
  4. When I try to add an image the link works.

Screencast

Test.FI.mp4

@Mayank-Tripathi32 Mayank-Tripathi32 marked this pull request as ready for review March 26, 2025 12:19
Copy link

github-actions bot commented Mar 26, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Mayank-Tripathi32 <mayanktripathi32@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: shimotmk <shimotomoki@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@Mamaduka Mamaduka added [Type] Bug An existing feature does not function as intended [Block] Post Featured Image Affects the Post Featured Image Block labels Mar 26, 2025
Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! This PR works well for me.

However, note that this PR is a temporary fix for 6.8. I think the fundamental problem is that the a element includes interactive element (button) even though it is not permitted by the HTML specification:

<figure class="wp-block-post-featured-image">
  <a>
    <div class="components-placeholder block-editor-media-placeholder is-large has-illustration">
      <div class="components-placeholder__fieldset">
        <div class="components-form-file-upload">
          <button type="button" class="components-button">Upload</button>
        </div>
        <button type="button" class="components-button" aria-label="Add a featured image"></button>
      </div>
    </div>
  </a>
</figure>

I believe the following approach is the ideal solution:

  • If the block is not in the query context:
    • The placeholder has an upload button, so we can't wrap the placeholder with a element.
    • If the featured image is not set, don't show the link-related settings (Lint to Post, Open in new tab, Link Rel)
    • When the featured image is reset, initialize/disable all link-related attributes.
  • If the block is in the query context:
    • The placeholder doesn't have an upload button, and the featured image is set depending on the current context.
    • Link related settings are always visible. When the block is linked, wrap the placeholder in an a element. This is safe because there are no interactive buttons inside the placeholder.

Doing these things in 6.8 may be risky, so it would be a good idea to explore the ideal solution after shipping this PR.

@t-hamano t-hamano added the Backport to WP 6.8 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Mar 27, 2025
@t-hamano
Copy link
Contributor

This PR fixes a regression that is new to 6.8, so I think we need to backport this PR.

I'd be happy to get additional review/approval as we are in the RC phase.

Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense going with the hotfix for WP 6.8 ✅

@Mayank-Tripathi32
Copy link
Contributor Author

I would be happy to work on finding ideal solution in background after hot-fix is merged 🙌🏻

@Mamaduka
Copy link
Member

@t-hamano, do you mind creating a new issue based on your comment?

@Mamaduka Mamaduka merged commit bdb1679 into WordPress:trunk Mar 27, 2025
71 of 74 checks passed
@github-project-automation github-project-automation bot moved this from 🔎 Needs Review to ✅ Done in WordPress 6.8 Editor Tasks Mar 27, 2025
@github-actions github-actions bot added this to the Gutenberg 20.6 milestone Mar 27, 2025
@github-actions github-actions bot removed the Backport to WP 6.8 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Mar 27, 2025
gutenbergplugin pushed a commit that referenced this pull request Mar 27, 2025
…to post (#69716)

Co-authored-by: Mayank-Tripathi32 <mayanktripathi32@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: shimotmk <shimotomoki@git.wordpress.org>
@github-actions github-actions bot added the Backported to WP Core Pull request that has been successfully merged into WP Core label Mar 27, 2025
Copy link

I just cherry-picked this PR to the wp/6.8 branch to get it included in the next release: 3e457a2

Mayank-Tripathi32 added a commit to Mayank-Tripathi32/gutenberg that referenced this pull request Mar 27, 2025
… added checks for link to be shown only if featured image is set.
t-hamano added a commit that referenced this pull request Apr 7, 2025
…ment (#69730)

* Feat: removed the code from  #69716  and fixed html structure, added checks for link to be shown only if featured image is set.

* Fix: Update conditions for displaying link options in Post Featured Image edit panel

* Refactor: Add conditional rendering to inspector controls

* feat: add templates for single page templates

Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com>

* feat: add sizeSlug prop to reset handler

---------

Co-authored-by: Mayank-Tripathi32 <mayanktripathi32@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
chriszarate pushed a commit to chriszarate/gutenberg that referenced this pull request Jul 1, 2025
…to post (WordPress#69716)

Co-authored-by: Mayank-Tripathi32 <mayanktripathi32@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: shimotmk <shimotomoki@git.wordpress.org>
chriszarate pushed a commit to chriszarate/gutenberg that referenced this pull request Jul 1, 2025
…ment (WordPress#69730)

* Feat: removed the code from  WordPress#69716  and fixed html structure, added checks for link to be shown only if featured image is set.

* Fix: Update conditions for displaying link options in Post Featured Image edit panel

* Refactor: Add conditional rendering to inspector controls

* feat: add templates for single page templates

Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com>

* feat: add sizeSlug prop to reset handler

---------

Co-authored-by: Mayank-Tripathi32 <mayanktripathi32@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported to WP Core Pull request that has been successfully merged into WP Core [Block] Post Featured Image Affects the Post Featured Image Block [Type] Bug An existing feature does not function as intended
Projects
Development

Successfully merging this pull request may close these issues.

Featured Image block: If you enable link without setting an image, the link will work.
3 participants