Implementing metaboxes with CMB2 - which is free, lightweight and not frontend-dependent. Basic sample.
<?php
// Sample code to display fields on frontend - put in a hookbox in Dynamik, for example, or in your custom template
$text = esc_html (get_post_meta( get_the_ID(), '_siga_text', true ));
$email = is_email(get_post_meta( get_the_ID(), '_siga_email', true ));
$url = esc_url(get_post_meta( get_the_ID(), '_siga_url', true ));
$output = '';
// Only display the fields if we have all the data. Define your required fields here.
if ( $text && $email && $url ) {
echo '<p>' . $text . '<br>' . $email . '<br>' . $url . '</p>';
}
// Else display nothing to avoid empty HTML tags being added
else {
return $output;
}
?>
// Goes to functions
add_action( 'cmb2_admin_init', 'cmb2_sample_metaboxes' );
/**
* Define the metabox and field configurations.
*/
function cmb2_sample_metaboxes() {
// Start with an underscore to hide fields from custom fields list
$prefix = '_siga_';
/**
* Initiate the metabox
*/
$cmb = new_cmb2_box( array(
'id' => 'test_metabox',
'title' => __( 'Test Metabox', 'cmb2' ),
'object_types' => array( 'page', ), // These fields will only show up on pages
//'show_on' => array( 'key' => 'page-template', 'value' => 'my-templates/teammembers.php' ), // will show up only on this template
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
// 'cmb_styles' => false, // false to disable the CMB stylesheet
// 'closed' => true, // Keep the metabox closed by default
) );
// Regular text field
$cmb->add_field( array(
'name' => __( 'Test Text', 'cmb2' ),
'desc' => __( 'Put your text in here)', 'cmb2' ),
'id' => $prefix . 'text',
'type' => 'text',
'show_on_cb' => 'cmb2_hide_if_no_cats', // function should return a bool value
'before_row' => '<h4 style="color:red;">ALL fields must be populated!</h4>',
// 'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter
// 'escape_cb' => 'my_custom_escaping', // custom escaping callback parameter
// 'on_front' => false, // Optionally designate a field to wp-admin only
// 'repeatable' => true,
) );
// URL text field
$cmb->add_field( array(
'name' => __( 'Website URL', 'cmb2' ),
'desc' => __( 'Add your URL, please', 'cmb2' ),
'id' => $prefix . 'url',
'type' => 'text_url',
// 'protocols' => array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet'), // Array of allowed protocols
// 'repeatable' => true,
) );
// Email text field
$cmb->add_field( array(
'name' => __( 'Test Text Email', 'cmb2' ),
'desc' => __( 'Add your email address, please', 'cmb2' ),
'id' => $prefix . 'email',
'type' => 'text_email',
// 'repeatable' => true,
) );
// Add other metaboxes as needed
}