Last active
October 10, 2023 10:52
-
-
Save Ciantic/56399cfe5760ca9db889d1918082cde6 to your computer and use it in GitHub Desktop.
Temporary shortcode block, when WordPress had a bug
This file contains hidden or 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 () { | |
// 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