The standard Lorem Ipsum passage, used since the 1500s

“Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.”

Section 1.10.32 of “de Finibus Bonorum et Malorum”, written by Cicero in 45 BC

“Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?”

EN NL

Add Sticky tickbox to attachment posts

For my project to use normal WordPress posts as “custom post type” I wanted to add the the sticky posts functionality to attachment posts. The process is handled via the standard function stick_post() in post.php. I found out that there is no check for the post_type so if you add the tickbox with the ID and name “sticky” to the attachment post the function stick_post() will process the $post_data[‘sticky’] when updating the attachment post.

Add metabox to attachment sidebar

Copy and paste this code in your theme’s function.php.

/**
Add metabox to attachment page
*/
function add_sticky_to_attachment_meta(){
add_meta_box( 'sticky-attachment-meta-box', // ID
'Sticky', // NAME
'sticky_attachment_meta_box_callback', // Callback function name
'attachment', // Post type
'side', // Location (sidebar)
'low'); // Priority
}
add_action( 'admin_init', 'add_sticky_to_attachment_meta' );

Add a sticky posts metabox callback function

The callback function display the checkbox and makes sure it display the correct value.

/**
Add callback for metabox on attachment page
*/
function sticky_attachment_meta_box_callback(){
global $post;
// Set variable for checkbox
$checked = '';
$stickies = get_option('sticky_posts');
if (!is_array($stickies)){
// do nothing show 'empty' in code
$checked = 'empty';
}else{
// Make intval to cherck numbers strict
$stickies = array_map( 'intval', $stickies );
if (in_array(intval($post->ID),$stickies,true)){
$checked = 'checked';
}
}
echo '<div>';
echo '<input type="checkbox" id="sticky" name="sticky" '.$checked.'>';
echo ' Stick to the top of the gallery';
echo '</div>';
}

There is no need for further code to handle the saving in the database. The default edit_post function is called when saving an attachment and because we named the checkbox ‘sticky’ the post_data contains $post_data[‘sticky’] which is processed by the function stick_post().

Leave a Reply