Skip to content

Instantly share code, notes, and snippets.

@krystyna93
Created June 13, 2023 07:35
Show Gist options
  • Save krystyna93/af38d93d42c3d39f6782b67b56e63ecd to your computer and use it in GitHub Desktop.
Save krystyna93/af38d93d42c3d39f6782b67b56e63ecd to your computer and use it in GitHub Desktop.
Custom WordPress Testimonials Metabox
<?php
// Add a new metabox to the testimonial post type
function add_testimonial_metabox() {
// Use the WordPress function add_meta_box to define our metabox
add_meta_box(
'testimonial_metabox', // Metabox ID
'Testimonial Details', // Metabox title
'render_testimonial_metabox', // Callback function to render the metabox contents
'testimonial', // Post type where the metabox should appear
'normal', // Metabox context (e.g. 'normal', 'side', or 'advanced')
'high' // Metabox priority (e.g. 'high', 'core', 'default', or 'low')
);
}
// Render the contents of the custom metabox
function render_testimonial_metabox($post) {
// Use get_post_meta to retrieve any existing author and company data for this testimonial
$testimonial_author = get_post_meta($post->ID, 'testimonial_author', true);
$testimonial_company = get_post_meta($post->ID, 'testimonial_company', true);
// Use wp_nonce_field to generate a nonce field for security purposes
wp_nonce_field('save_testimonial_metabox', 'testimonial_metabox_nonce');
// Output the HTML for the metabox
?>
<p>
<label for="testimonial_author">Author Name:</label><br>
<input type="text" id="testimonial_author" name="testimonial_author" value="<?php echo esc_attr($testimonial_author); ?>">
</p>
<p>
<label for="testimonial_company">Company:</label><br>
<input type="text" id="testimonial_company" name="testimonial_company" value="<?php echo esc_attr($testimonial_company); ?>">
</p>
<?php
}
// Save the data entered in the custom metabox
function save_testimonial_metabox($post_id) {
// Check that autosave is not in progress
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// Verify that the nonce is valid
if (!isset($_POST['testimonial_metabox_nonce']) || !wp_verify_nonce($_POST['testimonial_metabox_nonce'], 'save_testimonial_metabox')) {
return;
}
// Check that the current user has permission to edit this post
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Sanitize and validate the author and company data entered in the metabox
$author_name = isset($_POST['testimonial_author']) ? sanitize_text_field($_POST['testimonial_author']) : '';
$company_name = isset($_POST['testimonial_company']) ? sanitize_text_field($_POST['testimonial_company']) : '';
if (empty($author_name) || empty($company_name)) {
// Display an error message if either field is empty
add_action('admin_notices', function() {
?>
<div class="notice notice-error">
<p><?php _e('Both Author Name and Company fields are required.', 'my_textdomain'); ?></p>
</div>
<?php
});
return;
}
// Update the post meta with the sanitized author and company data entered in the metabox
update_post_meta($post_id, 'testimonial_author', $author_name);
update_post_meta($post_id, 'testimonial_company', $company_name);
}
// Hook our add_testimonial_metabox and save_testimonial_metabox functions into WordPress
add_action('add_meta_boxes_testimonial', 'add_testimonial_metabox');
add_action('save_post_testimonial', 'save_testimonial_metabox');
----
/* To add the metabox to an existing custom post type, you'll need to modify the code a bit.
Here's an example of how to add the metabox to a custom post type named "my_custom_post_type": */
function my_custom_metabox() {
add_meta_box(
'testimonial_metabox',
'Testimonial Details',
'render_testimonial_metabox',
'my_custom_post_type', // Change this to your custom post type slug
'normal',
'high'
);
}
function render_testimonial_metabox( $post ) {
// Add your metabox fields here
}
add_action( 'add_meta_boxes', 'my_custom_metabox' );
/* You'll want to replace "my_custom_post_type" with the slug of your existing custom post type, and update the render_testimonial_metabox function to include the fields you want to display in the metabox.
Once you've made these changes, simply add the modified code to your theme's functions.php file or a plugin and save the changes. The metabox should now be added to your existing custom post type. */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment