I want to add a new checkbox field inside Publish block in add/edit post page. Does anyone have idea how to do that ?
Simply create a new post or edit an existing one. Go to the custom fields meta box and select your custom field from the drop-down menu and enter its value. Click on the 'Add Custom Field' button to save your changes and then publish or update your post.
At the top of the screen, click on the Screen Options option. This will expand and allow you to change some Boxes, Layout, and Additional settings on your post and page content. Under the Boxes section, you will see Custom Fields. Make sure that the checkbox is checked to enable them.
I have finally found the solution. I hope it will be of good use for somebody.
add_action( 'post_submitbox_misc_actions', 'publish_in_frontpage' );
function publish_in_frontpage($post)
{
$value = get_post_meta($post->ID, '_publish_in_frontpage', true);
echo '<div class="misc-pub-section misc-pub-section-last">
<span id="timestamp">'
. '<label><input type="checkbox"' . (!empty($value) ? ' checked="checked" ' : null) . 'value="1" name="publish_in_frontpage" /> Publish to frontpage</label>'
.'</span></div>';
}
function save_postdata($postid)
{
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
if ( !current_user_can( 'edit_page', $postid ) ) return false;
if(empty($postid) || $_POST['post_type'] != 'article' ) return false;
if($_POST['action'] == 'editpost'){
delete_post_meta($postid, 'publish_in_frontpage');
}
add_post_meta($postid, 'publish_in_frontpage', $_POST['publish_in_frontpage']);
}
rbncha's code didn't work out of the box and needed a lot of tweaking, the code below is what I came up with. I've added some comments which explains everything thoroughly.
The following code adds a checkbox in the publish block of posts (you can easily change the post type), and stores/retrieves the value in/from the database. With some minor tweaking you could easily add a text field or anything you like.
It should be noted that you have to change my_
to a unique key for your theme or plugin!
add_action( 'post_submitbox_misc_actions', 'my_featured_post_field' );
function my_featured_post_field()
{
global $post;
/* check if this is a post, if not then we won't add the custom field */
/* change this post type to any type you want to add the custom field to */
if (get_post_type($post) != 'post') return false;
/* get the value corrent value of the custom field */
$value = get_post_meta($post->ID, 'my_featured_post_field', true);
?>
<div class="misc-pub-section">
<?php //if there is a value (1), check the checkbox ?>
<label><input type="checkbox"<?php echo (!empty($value) ? ' checked="checked"' : null) ?> value="1" name="my_featured_post_field" /> Featured on frontpage</label>
</div>
<?php
}
add_action( 'save_post', 'my_save_postdata');
function my_save_postdata($postid)
{
/* check if this is an autosave */
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
/* check if the user can edit this page */
if ( !current_user_can( 'edit_page', $postid ) ) return false;
/* check if there's a post id and check if this is a post */
/* make sure this is the same post type as above */
if(empty($postid) || $_POST['post_type'] != 'post' ) return false;
/* if you are going to use text fields, then you should change the part below */
/* use add_post_meta, update_post_meta and delete_post_meta, to control the stored value */
/* check if the custom field is submitted (checkboxes that aren't marked, aren't submitted) */
if(isset($_POST['my_featured_post_field'])){
/* store the value in the database */
add_post_meta($postid, 'my_featured_post_field', 1, true );
}
else{
/* not marked? delete the value in the database */
delete_post_meta($postid, 'my_featured_post_field');
}
}
If you want to read more about custom fields see here: http://codex.wordpress.org/Custom_Fields
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With