Skip to content

Instantly share code, notes, and snippets.

@5ally

5ally/README.md Secret

Last active January 11, 2021 20:53
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 5ally/d13a0d3e46aa33bc0f3f39f15d6194b7 to your computer and use it in GitHub Desktop.
Save 5ally/d13a0d3e46aa33bc0f3f39f15d6194b7 to your computer and use it in GitHub Desktop.

ABOUT THE FILES

  1. featured-video-toggle.src.js is the custom component (named FeaturedVideoToggle) created similar to the original PostFeaturedImage component.

  2. index.src.js is the file which "imports" the custom FeaturedVideoToggle component.

  3. index.build.js is the build version of the files #1 and #2 above. It was generated by wp-scripts after running the npm run build command.

  4. index.asset.php is also generated by the wp-scripts (version 5+ only).

And note that, in my plugin or actual code, I don't have the .src or .build in the file name. My plugin structure:

my-plugin/
	build/
		index.js
		index.asset.php
	src/
		index.js
		featured-video-toggle.js
	index.php

And in index.php, here's how I enqueue the my-plugin/build/index.js file:

add_action( 'enqueue_block_assets', function () {
	$asset_file = include( plugin_dir_path( __FILE__ ) . 'build/index.asset.php' );
	wp_enqueue_script( 'my-guten', plugin_dir_url( __FILE__ ) . 'build/index.js',
		$asset_file['dependencies'], $asset_file['version'] );
} );

And that's how it should be done — don't enqueue the source files — they may be small-sized, but browsers don't understand the JSX syntax without a compiler, and the thing is, compilers are "heavy" and can make your site slow.

Anyway, I hope the code/source helps. Cheers!

<?php return array('dependencies' => array('wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-i18n', 'wp-polyfill'), 'version' => '3dd0e6d1d6eeb5718f155452d70ac7e3');
!function(e){var t={};function n(o){if(t[o])return t[o].exports;var r=t[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,n),r.l=!0,r.exports}n.m=e,n.c=t,n.d=function(e,t,o){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)n.d(o,r,function(t){return e[t]}.bind(null,r));return o},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=5)}([function(e,t){!function(){e.exports=this.wp.element}()},function(e,t){!function(){e.exports=this.wp.components}()},function(e,t){!function(){e.exports=this.wp.data}()},function(e,t){!function(){e.exports=this.wp.i18n}()},function(e,t){!function(){e.exports=this.wp.compose}()},function(e,t,n){"use strict";n.r(t);var o=n(0),r=n(3),i=n(1),u=n(4),c=n(2);var a=Object(c.withSelect)((function(e){var t=(0,e("core/editor").getEditedPostAttribute)("meta");return{isVideo:t._featured_image_is_video,videoUrl:t._featured_image_video_url}})),l=Object(c.withDispatch)((function(e){var t=e("core/editor").editPost;return{onSetIsVideo:function(e){t({meta:{_featured_image_is_video:e}})},onSetVideoUrl:function(e){t({meta:{_featured_image_video_url:e}})}}})),d=Object(u.compose)(a,l)((function(e){var t=e.isVideo,n=e.videoUrl,r=e.onSetIsVideo,u=e.onSetVideoUrl;return Object(o.createElement)(o.Fragment,null,Object(o.createElement)(i.CheckboxControl,{label:"Image is a video",checked:t,onChange:r}),t&&Object(o.createElement)(i.TextControl,{value:n,onChange:u}))}));wp.hooks.addFilter("editor.PostFeaturedImage","dsplugin/featured-image-as-video",(function(e){return function(t){return Object(o.createElement)(o.Fragment,null,Object(o.createElement)("h4",null,Object(r.__)("Image Options","dsplugin")),Object(o.createElement)(e,t),Object(o.createElement)(d,null))}}))}]);
// WordPress dependencies.
import { createElement as el, Fragment } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
// Internal dependencies.
import FeaturedVideoToggle from './featured-video-toggle';
function wrapPostFeaturedImage( OriginalComponent ) {
return function( props ) {
return (
el(
Fragment, null,
// Show a custom heading
el( 'h4', null, __( 'Image Options', 'dsplugin' ) ),
// ..then the original featured image element
el( OriginalComponent, props ),
// ..then our custom checkbox and text box.
<FeaturedVideoToggle />
)
);
};
}
wp.hooks.addFilter(
'editor.PostFeaturedImage',
'dsplugin/featured-image-as-video',
wrapPostFeaturedImage
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment