Skip to content

Instantly share code, notes, and snippets.

@EdwardBock
Last active October 17, 2021 11:34
Show Gist options
  • Save EdwardBock/820ac9eaa9eeb1437540984fbca4e746 to your computer and use it in GitHub Desktop.
Save EdwardBock/820ac9eaa9eeb1437540984fbca4e746 to your computer and use it in GitHub Desktop.
Add reading time to post content with a custom Gutenberg block.
import {registerBlockType} from '@wordpress/blocks';
import ServerSideRender from '@wordpress/server-side-render';
registerBlockType( 'public-function-org/reading-time', {
title: "Reading time",
icon: 'clock',
category: 'common',
edit: () => {
return <ServerSideRender
block="public-function-org/reading-times"
/>
},
} );
<?php
add_action('init', function(){
register_block_type(
'public-function-org/reading-time',
[
"render_callback" => function( $attributes, $editorContent){
$postId = get_the_ID();
if(empty($postId)) return "";
// https://gist.github.com/EdwardBock/fa659b67bcf4f568289d0c49c175f42b
return reading_time_get_content($postId);
}
]
)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment