admin管理员组文章数量:1122832
I’m on WordPress 5 beta 4, the problem is that meta boxes can not hide them when selecting a specific template chosen by page attributes. To make them appear or hide, you have to update the browser page or exit the created page and re-enter it. This is not very nice for users. This is my code:
<?php
function willer_custom_meta_portfolio_standar() {
global $post;
if(!empty($post))
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'template-pages/page-portfolio-standar.php' )
{
add_meta_box(
'page-portfolio-standar-meta-box', // $id
esc_html__('Customize Portfolio Standar','willer'), // $title
'willer_portfolio_standar_meta_callback', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
}
}
}
add_action( 'add_meta_boxes', 'willer_custom_meta_portfolio_standar' );
?>
The problem also occurs with WordPress 4.9.8 using Gutenberg, using the classic editor the problem is not there. I think it’s because Gutenberg uses Ajax to save the options. Do you have any suggestions to solve the problem? Thank yo
I’m on WordPress 5 beta 4, the problem is that meta boxes can not hide them when selecting a specific template chosen by page attributes. To make them appear or hide, you have to update the browser page or exit the created page and re-enter it. This is not very nice for users. This is my code:
<?php
function willer_custom_meta_portfolio_standar() {
global $post;
if(!empty($post))
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'template-pages/page-portfolio-standar.php' )
{
add_meta_box(
'page-portfolio-standar-meta-box', // $id
esc_html__('Customize Portfolio Standar','willer'), // $title
'willer_portfolio_standar_meta_callback', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
}
}
}
add_action( 'add_meta_boxes', 'willer_custom_meta_portfolio_standar' );
?>
The problem also occurs with WordPress 4.9.8 using Gutenberg, using the classic editor the problem is not there. I think it’s because Gutenberg uses Ajax to save the options. Do you have any suggestions to solve the problem? Thank yo
Share Improve this question asked Nov 20, 2018 at 9:27 DenisDenis 494 bronze badges 2- Gutenberg is running with React. This won't work properly anymore with PHP. – André Kelling Commented Nov 20, 2018 at 11:14
- Yes I know that Gutenberg uses React, I was looking for a solution to improve the code of my meta-boxes. Thanks anyway for the help. – Denis Commented Nov 20, 2018 at 11:51
1 Answer
Reset to default 0Here is the answer for Gutenberg editor by Christine Cooper.
Create JS file /js/block-script.js
and use the JS function removeEditorPanel()
which will completely remove the panel with all its controls:
// remove excerpt panel
wp.data.dispatch( 'core/edit-post').removeEditorPanel( 'post-excerpt' );
Add this to fucntion.php
function cc_gutenberg_register_files() {
// script file
wp_register_script(
'cc-block-script',
get_stylesheet_directory_uri() .'/js/block-script.js', // adjust the path to the JS file
array( 'wp-blocks', 'wp-edit-post' )
);
// register block editor script
register_block_type( 'cc/ma-block-files', array(
'editor_script' => 'cc-block-script'
) );
}
add_action( 'init', 'cc_gutenberg_register_files' );
You can find the original answer here: https://wordpress.stackexchange.com/a/339437
本文标签: metaboxShow or hide custom meta box not work with Gutenberg
版权声明:本文标题:metabox - Show or hide custom meta box not work with Gutenberg 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736289459a1928308.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论