admin管理员组文章数量:1291124
In a pre-Gutenberg site, I have a shortcode with roughly the following structure:
function generate_new_member_form() {
ob_start(); ?>
// a form
// to generate this form there is some code that uses objects or functions
// only available in the front end
<?php return ob_get_clean();
}
add_shortcode('new-member-form', 'generate_new_member_form');
For whatever it's worth, the code has to do with Woocommerce checkout fields, and I get fatal errors when I try to edit a page that has the shortcode [new_member_form]
such as "Call to a member function get() on null". But I don't think the problem has to do with Woocommerce
Is there a way to prevent Gutenberg from trying to run any of this code in the backend? I would like to be able to edit the page and see just [new_member_form]
with no attempt to display the shortcode.
In a pre-Gutenberg site, I have a shortcode with roughly the following structure:
function generate_new_member_form() {
ob_start(); ?>
// a form
// to generate this form there is some code that uses objects or functions
// only available in the front end
<?php return ob_get_clean();
}
add_shortcode('new-member-form', 'generate_new_member_form');
For whatever it's worth, the code has to do with Woocommerce checkout fields, and I get fatal errors when I try to edit a page that has the shortcode [new_member_form]
such as "Call to a member function get() on null". But I don't think the problem has to do with Woocommerce
Is there a way to prevent Gutenberg from trying to run any of this code in the backend? I would like to be able to edit the page and see just [new_member_form]
with no attempt to display the shortcode.
- Your shortcode should include checks to make sure of any dependencies are available. Then the shortcode loading in the backend wouldn’t be a problem. – Jacob Peattie Commented Nov 30, 2019 at 5:59
- I was hoping I could avoid doing that. I'm sure there are plenty of cases where you want to run the shortcode in the backend, but this is not one of them (I can see the part of the form before the errors in the editor, but I don't really care about seeing it, since I cannot modify it in the editor) – adelval Commented Nov 30, 2019 at 6:12
- It doesn't matter where the shortcode is intended to be used. You should always code that way. It's surely a couple of lines of code that would solve this problem and make your code considerably less prone to bugs. – Jacob Peattie Commented Nov 30, 2019 at 8:25
1 Answer
Reset to default 1Use it like this, this defined('REST_REQUEST') will help you to disable running on the backend gutenberg block editor
function generate_new_member_form() {
if(defined('REST_REQUEST')) return;
ob_start(); ?>
// a form
// to generate this form there is some code that uses objects or functions
// only available in the front end
<?php return ob_get_clean();
}
add_shortcode('new-member-form', 'generate_new_member_form');
本文标签: block editorGutenberg running code only available in front end within shortcode
版权声明:本文标题:block editor - Gutenberg running code only available in front end within shortcode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741511637a2382641.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论