• Resolved Holkan

    (@cubeinthebox)


    Hello!

    I’ve been struggling with this all day long looking into the docs, googling, etc. So, please help me with this:
    How do I get the alt attribute of the current post’s featured image?
    I know that I can access the editor’s data through wp.data.select(‘core/editor’), hence I can get the post Id and post content, etc. I just can’t figure out how to get the featured image.
    I’m working on a plugin that helps editors to know if they are doing a good job while writing posts. This plugin is written in React/Javascript and it needs to grab the alt attribute of the current post’s featured image in order to tell the editors if they have already set the alt attribute of its post.

    Thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • @cubeinthebox.,

    Try this code

    $thumb_id = get_post_thumbnail_id();
    $alt = get_post_meta( $thumb_id, '_wp_attachment_image_alt', true );

    Thanks

    Using the withSelect HOC you can do something like this to pass the current featured image to your block:

    
    // Create a higher-order component that updates automatically when featured image changes.
    const applyWithSelect = withSelect( ( select ) => {
    	const { getMedia } = select( 'core' );
    	const { getEditedPostAttribute } = select( 'core/editor' );
    	const featuredImageId = getEditedPostAttribute( 'featured_media' );
    
    	return {
    		media: featuredImageId ? getMedia( featuredImageId ) : null,
    		featuredImageId,
    	};
    } );
    
    export default compose( [
    	applyWithSelect,
    ] )( MyCustomBlockEdit );
    
    Thread Starter Holkan

    (@cubeinthebox)

    @swisspidy
    Thanks for your reply. I’m using withSelect and pretty much what you told me and now I get the current featured image alt attribute, but, I’m not building a bock, but a plugin with registerPlugin();.
    In this plugin I’m updating some post meta values with wp.apiRequest() inside getDerivedStateFromProps() . The problem is that I can’t setState inside getDerivedStateFromProps() since this is not accessible. Hence, I’m trying to setState outside that method. I thought that it can be done inside componentDidUpdate() but even if i wrap setState inside a condition, it fires an infinite loop.
    Where can I setState in this case?

    	componentDidUpdate(prevProps){
    		console.log('did update');
    		console.log(prevProps);
    
    		if(prevProps.media !== undefined && prevProps.content_count !== ''){
    			let color_code = '';
    			let fti_color_code = '';
    			let alt_text = prevProps.media.alt_text;
    			let content_count = prevProps.content_count;
    
    			if(content_count < 300){
    				color_code = 'red';
    			}else if(content_count >= 300 && content_count < 400){
    				color_code = 'orange';
    			}else{
    				color_code = 'green';
    			}
    
    			let alt_text_arr = alt_text.split(' ');
    			if(alt_text_arr.length !== 0){
    				if(alt_text_arr.length < 4 && alt_text_arr.length > 6){
    					fti_color_code = 'red';
    					console.log(<code>fti _cc: ${fti_color_code}</code>);
    				}else if(alt_text_arr.length >= 4 && alt_text_arr.length <= 6){
    					fti_color_code = 'green';
    					console.log(<code>fti cc: ${fti_color_code}</code>);
    				}
    			}
    
    			if(this.state.body_content.color_code !== color_code || this.state.alt_attribute.color_code !== fti_color_code){
    				console.log(<code>content count: ${content_count}</code>);
    				console.log(<code>alt text: ${alt_text}</code>);
    
    				this.setState({
    					body_content: {
    						color_code: {key: 'body_content_cc', value: color_code}
    					},
    					alt_attribute:{
    						color_code: {key: 'alt_attribute_cc', value: fti_color_code}
    					}
    				});
    			}
    		}
    
    	}//End component did update

    Thanks for your help

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Getting current post’s featured image and its alt attribute’ is closed to new replies.