Remove Custom metabox from particular page template is used

I have created custom metafields like the following and it was working fine. I want to show the metafield only when an particular page template is used. but it was not working.

class contactdetailsMetabox {
private $screen = array(
‘page’,
);
private $meta_fields = array(
array(
‘label’ => ‘Business Title’,
‘id’ => ‘business-title’,
‘default’ => ‘Business Title’,
‘type’ => ‘text’,
),
array(
‘label’ => ‘Business Logo’,
‘id’ => ‘business-logo’,
‘type’ => ‘media’,
),
array(
‘label’ => ‘Street Address’,
‘id’ => ‘street-address’,
‘type’ => ‘wysiwyg’,
),
array(
‘label’ => ‘Postal code’,
‘id’ => ‘postal-code’,
‘type’ => ‘text’,
),
array(
‘label’ => ‘Address Locality’,
‘id’ => ‘address-locality’,
‘type’ => ‘text’,
),
array(
‘label’ => ‘Business Phone No’,
‘id’ => ‘business-phone’,
‘type’ => ‘text’,
),
array(
‘label’ => ‘Business Mobile’,
‘id’ => ‘business-mobile’,
‘type’ => ‘tel’,
),
array(
‘label’ => ‘Business Email’,
‘id’ => ‘business-email’,
‘type’ => ’email’,
),
array(
‘label’ => ‘Business Website’,
‘id’ => ‘business-website’,
‘type’ => ‘url’,
),
array(
‘label’ => ‘Latitude’,
‘id’ => ‘map-latitude’,
‘type’ => ‘text’,
),
array(
‘label’ => ‘Longitude’,
‘id’ => ‘map-longitude’,
‘type’ => ‘text’,
),
array(
‘label’ => ‘Zoom Level’,
‘id’ => ‘zoom-level’,
‘type’ => ‘text’,
),
);

public function __construct() {
add_action( ‘add_meta_boxes’, array( $this, ‘add_meta_boxes’ ) );
add_action( ‘admin_footer’, array( $this, ‘media_fields’ ) );
add_action( ‘save_post’, array( $this, ‘save_fields’ ) );
}
public function add_meta_boxes() {
foreach ( $this->screen as $single_screen ) {
add_meta_box(
‘contactdetails’,
__( ‘Contact details’, ‘rjslwp’ ),
array( $this, ‘meta_box_callback’ ),
$single_screen,
‘advanced’,
‘high’
);
}
}
public function meta_box_callback( $post ) {
wp_nonce_field( ‘contactdetails_data’, ‘contactdetails_nonce’ );
echo ‘Here you can add your business contact details.’;
$this->field_generator( $post );
}
public function media_fields() {
?><script>
jQuery(document).ready(function($){
if ( typeof wp.media !== ‘undefined’ ) {
var _custom_media = true,
_orig_send_attachment = wp.media.editor.send.attachment;
$(‘.contactdetails-media’).click(function(e) {
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = $(this);
var id = button.attr(‘id’).replace(‘_button’, ”);
_custom_media = true;
wp.media.editor.send.attachment = function(props, attachment){
if ( _custom_media ) {
$(‘input#’+id).val(attachment.url);
} else {
return _orig_send_attachment.apply( this, [props, attachment] );
};
}
wp.media.editor.open(button);
return false;
});
$(‘.add_media’).on(‘click’, function(){
_custom_media = false;
});
}
});
</script><?php
}
public function field_generator( $post ) {
$output = ”;
foreach ( $this->meta_fields as $meta_field ) {
$label = ‘<label for=”‘ . $meta_field[‘id’] . ‘”>’ . $meta_field[‘label’] . ‘</label>’;
$meta_value = get_post_meta( $post->ID, $meta_field[‘id’], true );
if ( empty( $meta_value ) ) {
$meta_value = $meta_field[‘default’]; }
switch ( $meta_field[‘type’] ) {
case ‘media’:
$input = sprintf(
‘<input style=”width: 80%%” id=”%s” name=”%s” type=”text” value=”%s”> <input style=”width: 19%%” class=”button contactdetails-media” id=”%s_button” name=”%s_button” type=”button” value=”Upload” />’,
$meta_field[‘id’],
$meta_field[‘id’],
$meta_value,
$meta_field[‘id’],
$meta_field[‘id’]
);
break;
case ‘wysiwyg’:
ob_start();
wp_editor($meta_value, $meta_field[‘id’]);
$input = ob_get_contents();
ob_end_clean();
break;
default:
$input = sprintf(
‘<input %s id=”%s” name=”%s” type=”%s” value=”%s”>’,
$meta_field[‘type’] !== ‘color’ ? ‘style=”width: 100%”‘ : ”,
$meta_field[‘id’],
$meta_field[‘id’],
$meta_field[‘type’],
$meta_value
);
}
$output .= $this->format_rows( $label, $input );
}
echo ‘<table class=”form-table”><tbody>’ . $output . ‘</tbody></table>’;
}
public function format_rows( $label, $input ) {
return ‘<tr><th>’.$label.'</th><td>’.$input.'</td></tr>’;
}
public function save_fields( $post_id ) {
if ( ! isset( $_POST[‘contactdetails_nonce’] ) )
return $post_id;
$nonce = $_POST[‘contactdetails_nonce’];
if ( !wp_verify_nonce( $nonce, ‘contactdetails_data’ ) )
return $post_id;
if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE )
return $post_id;
foreach ( $this->meta_fields as $meta_field ) {
if ( isset( $_POST[ $meta_field[‘id’] ] ) ) {
switch ( $meta_field[‘type’] ) {
case ’email’:
$_POST[ $meta_field[‘id’] ] = sanitize_email( $_POST[ $meta_field[‘id’] ] );
break;
case ‘text’:
$_POST[ $meta_field[‘id’] ] = sanitize_text_field( $_POST[ $meta_field[‘id’] ] );
break;
}
update_post_meta( $post_id, $meta_field[‘id’], $_POST[ $meta_field[‘id’] ] );
} else if ( $meta_field[‘type’] === ‘checkbox’ ) {
update_post_meta( $post_id, $meta_field[‘id’], ‘0’ );
}
}
}
}
if (class_exists(‘contactdetailsMetabox’)) {
new contactdetailsMetabox;
};

The above code is for adding custom fields

What i have tried is

function remove_custom_meta_box() {
global $post;

if(!empty($post))
{
$pageTemplate = get_post_meta($post->ID, ‘_wp_page_template’, true);

if($pageTemplate == ‘templates/contact-us.php’ )
{
remove_meta_box( ‘contactdetails’ , ‘page’ , ‘advanced’ );
}
}
}
}
add_action( ‘admin_menu’, ‘remove_custom_meta_box’ );

For testing my code i tried to remove it from all page its also not working

function remove_custom_meta_box() {
remove_meta_box( ‘contactdetails’ , ‘page’ , ‘advanced’ );
}
add_action( ‘admin_menu’, ‘remove_custom_meta_box’ );

Read more here:: Remove Custom metabox from particular page template is used

Leave a Reply

Your email address will not be published. Required fields are marked *