Skip to content

Instantly share code, notes, and snippets.

@diggeddy
Created August 15, 2023 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diggeddy/a784fe85e26d76311f1a13f6975bbb27 to your computer and use it in GitHub Desktop.
Save diggeddy/a784fe85e26d76311f1a13f6975bbb27 to your computer and use it in GitHub Desktop.
Meta box to add the Full Width Class to post body_class
// Add custom meta box
function add_full_width_meta_box() {
$post_types = array('post', 'page'); // Add more post types if needed
foreach ($post_types as $post_type) {
add_meta_box(
'full-width-meta-box',
'Full Width',
'full_width_meta_box_callback',
$post_type,
'side',
'default'
);
}
}
add_action('add_meta_boxes', 'add_full_width_meta_box');
// Meta box callback function
function full_width_meta_box_callback($post) {
$full_width = get_post_meta($post->ID, '_is_full_width', true);
?>
<label>
<input type="checkbox" name="is_full_width" <?php checked($full_width, 'on'); ?> />
Mark as Full Width
</label>
<?php
}
// Save meta box data
function save_full_width_meta_data($post_id) {
if (array_key_exists('is_full_width', $_POST)) {
update_post_meta($post_id, '_is_full_width', 'on');
} else {
delete_post_meta($post_id, '_is_full_width');
}
}
add_action('save_post', 'save_full_width_meta_data');
// Modify body class
function add_full_width_body_class($classes) {
if (is_singular(array('post', 'page'))) {
$full_width = get_post_meta(get_the_ID(), '_is_full_width', true);
if ($full_width === 'on') {
$classes[] = 'is-full-width';
}
}
return $classes;
}
add_filter('body_class', 'add_full_width_body_class');
@diggeddy
Copy link
Author

This PHP Snippet adds a custom meta check box to posts and pages, allowing the user to add the is-full-width class to the posts body class. This can be used in non GP themes to overcome container widths on an individual post basis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment