Last active
October 17, 2021 11:34
-
-
Save EdwardBock/820ac9eaa9eeb1437540984fbca4e746 to your computer and use it in GitHub Desktop.
Add reading time to post content with a custom Gutenberg block.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
/> | |
}, | |
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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