0

I removed the default author meta box on posts and added it back with a checkbox that I use to determine if a post should show the author's byline and author box. It's been working, but for some reason it becomes unchecked in certain situations.

It's happened after making some private posts public, and when I've changed the permalinks on other posts. It's also happened when migrating the database from localhost to server.

Here's my code:

/* Remove and then add back in the author box to include check box to display author byline and bio */

add_action( 'admin_menu', 'fb_modify_author_meta_boxes' );
function fb_modify_author_meta_boxes() {

remove_meta_box('authordiv', 'post', 'normal');
add_meta_box('fb_authordiv', __('Author'), 'fb_post_author_meta_box', 'post', 'normal', 'core');
}

function fb_post_author_meta_box( $post ) {
global $user_ID;

// get all authors
$wp_user_search = new WP_User_Search($usersearch = '', $userspage = '', 'author');
$authors = join( ', ', $wp_user_search->get_results() );?>
<label class="screen-reader-text" for="post_author_override"><?php _e('Author'); ?></label>
<?php
    wp_dropdown_users( array(
        'name' => 'post_author_override',
        'selected' => empty($post->ID) ? $user_ID : $post->post_author,
        'include_selected' => true
    ) );

global $post;
$custom = get_post_custom($post->ID);
$show_author = $custom["show_author"][0]; 
?>

<input type="checkbox" name="show_author" <?php if( $show_author == true ) { ?>checked="checked"<?php } ?> style="margin-left:15px" />  Check the box to display the author's byline and author box.
<?php }


/* Save checkbox status */
add_action('save_post', 'save_details');

function save_details($post_ID = 0) {
$post_ID = (int) $post_ID;
$post_type = get_post_type( $post_ID );
$post_status = get_post_status( $post_ID );

if ($post_type) {
update_post_meta($post_ID, "show_author", $_POST["show_author"]);
}
return $post_ID;
}

If anyone has any insight as to why the status is being lost, I would appreciate the help.

Thanks.

0

The trouble is that save_post fires in lots of places other than the "edit post" screen - when it does, your code will think the checkbox is unchecked and overwrite your saved state.

Add an additional hidden input to your meta box:

<?php

// Don't global post, use the $post parameter already passed to the function
// global $post;

// Avoid undefined index errors by passing the third argument "single" true
// The double !! will cast the value to a boolean true/false
$show_author = !! get_post_meta( $post->ID, 'show_author', true );
?>

<input type="checkbox" name="show_author" <?php checked( $show_author ) /* WordPress helper function */ ?> style="margin-left:15px" /> Check the box to display the author's byline and author box.
<input type="hidden" name="do_show_author" value="1" />

And then check that it exists before saving your checkbox state:

function save_details( $post_ID ) {
    if ( isset( $_POST['do_show_author'] ) ) {
        // Use isset - unchecked checkboxes won't send a value to $_POST, you'll get an undefined index error
        update_post_meta( $post_ID, 'show_author', isset( $_POST['show_author'] ) ? '1' : '0' );
    }
}
  • I spoke too soon. Now if I uncheck the box and update the post, it stays checked. – notGettingStabbed May 14 '15 at 18:57
  • Are you sure you implemented my code correctly? It works fine on my local machine. – TheDeadMedic May 14 '15 at 19:01
  • I replaced everything from global $post on down (closing the fb_post_author_meta_box function and keeping the save_details action, of course). – notGettingStabbed May 14 '15 at 19:07
  • Have you tried the linked gist above? – TheDeadMedic May 14 '15 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.