This code should do the trick. Put this in your functions.php
file.
function add_custom_css_meta_box()
{
add_meta_box(
'custom-css',
'Custom CSS',
'post_meta_checkbox',
'page',
'side'
);
}
add_action('add_meta_boxes', 'add_custom_css_meta_box');
function post_meta_checkbox($post)
{
$checkbox = get_post_meta($post->ID, 'custom-css', true);
$checked = false;
if($checkbox == 'true')
{
$checked = true;
}
?>
<p>
<label>Tick this for custom CSS</label><br />
<input type="checkbox" name="custom-css" id="custom-css" value="true" <?php if($checked == true) { echo 'checked'; } ?>/>
</p>
<?php
}
function save_post_meta()
{
update_post_meta(get_the_ID(), 'custom-css', $_POST['custom-css']);
}
add_action('save_post', 'save_post_meta');
When you want to check if you need to load the CSS do an if statement and use get_post_meta($post->ID, 'custom-css', true)
and check if it is true or false. Replace the $post->ID with how ever you’re getting the page id. You could do this on the page.php
file.