Skip to content

Instantly share code, notes, and snippets.

@anova
Last active September 20, 2022 20:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anova/50e7c97815332bb4301e60507f1e21b2 to your computer and use it in GitHub Desktop.
Save anova/50e7c97815332bb4301e60507f1e21b2 to your computer and use it in GitHub Desktop.
Custom media upload component for Wordpress Gutenberg.
import { MediaUpload } from '@wordpress/block-editor';
import { Button } from '@wordpress/components';
function CustomMediaUpload(props){
return <MediaUpload
onSelect={ media => { props.onMediaSelected(media.url) } }
type="image"
value={ props.mediaUrl }
render={
({open}) => {
if(props.mediaUrl) {
return <>
<img src={props.mediaUrl} />
<hr/>
<Button onClick={ () => { props.onMediaSelected(null) }}>Remove Image</Button>
</>;
}
return <Button onClick={open}>Select Image</Button>;
}
}/>;
}
export { CustomMediaUpload };
/*
Usage:
import { CustomMediaUpload } from 'custom-media-upload'
<CustomMediaUpload
mediaUrl={attributes.mediaUrl}
onMediaSelected={ ( value ) => { setAttributes({'mediaUrl': value}) } }
/>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment