The problem here is a slight error in the origianl code.
Near the bottom of the save_sticky_metabox()
callback, change the line –
if(isset($_POST['was_checked'])) :
To this –
if($_POST['was_checked'] != 0) :
The was_checked
value is derived from a hidden field, not a checkbox, so regardless of the value it will always be set. Therefore we need to check if that value is not equal to 0
, as opposed to just checking if it was set.
A few tips for you also –
- You should alwasy escape quote and apostraphe characters in strings –
- Correct – ‘There\’s no highlight.’
- Incorrect – ‘There’s no highlight.’ (this will cause a PHP error)
- As you are creating 2x postmeta fields, consider starting the metakey with an
_
('_post_source'
and'_post_review'
). Doing this means that the postmeta will be “hidden” and will not appear in the Custom Fields metabox on the edit posts page. - If you wish, you can add all 3 metaboxes via one callback, rather than one for each (but that’s personal preference, nothing wrong with doing it your way) –
Just in case you want to do that –
function add_post_metaboxs(){
add_meta_box('sticky_post_metabox', 'Sticky Post', 'output_sticky_metabox', 'post');
add_meta_box('source_post_metabox', 'Bron', 'output_source_metabox', 'post');
add_meta_box('review_post_metabox', 'Review', 'output_review_metabox', 'post');
}