Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Last active October 10, 2023 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ciantic/56399cfe5760ca9db889d1918082cde6 to your computer and use it in GitHub Desktop.
Save Ciantic/56399cfe5760ca9db889d1918082cde6 to your computer and use it in GitHub Desktop.
Temporary shortcode block, when WordPress had a bug
<?php
add_action("init", function () {
// Ob_start twice, intentionally
ob_start();
?>
<script type="module">
<?php ob_start(); ?>
wp.blocks.registerBlockType("temp/shortcode", {
title: "Shortcode redux",
icon: "shortcode",
category: "common",
attributes: {
code: {
type: "string"
}
},
edit: function(props) {
function updateContent(event) {
props.setAttributes({
code: event.target.value
});
}
return wp.element.createElement(
"div", {
style: {
"padding": "1.5em",
"border": "1px solid currentColor",
}
},
[
"Shortcode",
wp.element.createElement("input", {
type: "text",
onChange: updateContent,
value: props.attributes.code,
style: {
"width": "100%",
"display": "block",
"box-sizing": "border-box"
}
})
]
);
},
save: function(props) {
return null;
}
});
<?php $code = ob_get_clean(); ?>
</script>
<?php
ob_get_clean();
wp_register_script("temp-shortcode", false, ['wp-blocks', 'wp-element']);
wp_add_inline_script("temp-shortcode", $code);
register_block_type(
'temp/shortcode',
[
'attributes' => [
'code' => [
'type' => 'string',
],
],
'editor_script_handles' => ['temp-shortcode'],
'render_callback' => function ($attributes, $content) {
return do_shortcode($attributes['code']);
},
]
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment