Skip to content
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

Added toggle control to set any image as feature image if no feature image is set for post #65896

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ Insert an image to make a visual statement. ([Source](https://github.com/WordPre
- **Name:** core/image
- **Category:** media
- **Supports:** align (center, full, left, right, wide), anchor, color (~~background~~, ~~text~~), filter (duotone), interactivity, shadow (), spacing (margin)
- **Attributes:** alt, aspectRatio, blob, caption, height, href, id, lightbox, linkClass, linkDestination, linkTarget, rel, scale, sizeSlug, title, url, width
- **Attributes:** alt, aspectRatio, blob, caption, height, href, id, isFeatureImage, lightbox, linkClass, linkDestination, linkTarget, rel, scale, sizeSlug, title, url, width

## Latest Comments

Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@
"source": "attribute",
"selector": "figure > a",
"attribute": "target"
},
"isFeatureImage": {
"type": "boolean",
"default": false
}
},
"supports": {
Expand Down
14 changes: 14 additions & 0 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
__experimentalToolsPanelItem as ToolsPanelItem,
__experimentalUseCustomUnits as useCustomUnits,
Placeholder,
ToggleControl,
} from '@wordpress/components';
import { useViewportMatch } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';
Expand Down Expand Up @@ -128,6 +129,7 @@ export default function Image( {
sizeSlug,
lightbox,
metadata,
isFeatureImage,
} = attributes;

// The only supported unit is px, so we can parseInt to strip the px here.
Expand Down Expand Up @@ -793,6 +795,18 @@ export default function Image( {
/>
) }
</ToolsPanel>
<ToolsPanel style={ { display: 'flex' } }>
<ToggleControl
label={ __( 'Set this image as feature image' ) }
checked={ isFeatureImage }
onChange={ () =>
setAttributes( {
isFeatureImage: ! isFeatureImage,
} )
}
__nextHasNoMarginBottom
/>
</ToolsPanel>
</InspectorControls>
<InspectorControls group="advanced">
<TextControl
Expand Down
11 changes: 11 additions & 0 deletions packages/block-library/src/image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ function render_block_core_image( $attributes, $content, $block ) {
return '';
}

// If feature image is not set for post and isFeatureImage is true then set this image as feature image.
if ( isset( $attributes['isFeatureImage'] ) && $attributes['isFeatureImage'] ) {
$post_id = get_the_ID();
if ( $post_id ) {
$has_thumbnail = has_post_thumbnail( $post_id );
if ( ! $has_thumbnail ) {
set_post_thumbnail( $post_id, $attributes['id'] );
}
}
}

$has_id_binding = isset( $attributes['metadata']['bindings']['id'] ) && isset( $attributes['id'] );

// Ensure the `wp-image-id` classname on the image block supports block bindings.
Expand Down
Loading