Here is my collection of WordPress code snippets for both my benefit and yours. These code snippets for WordPress are extremely handy and can save you from adding too many plugins. for example, people usually add a plugin to allow contributors to upload images, but now you do not have to.
Allow Contributors to Upload Images
Where to put this code: This code gets pasted into the functions.php
file or the customfunctions.php
for Thesis.
What does it do: It will place an “add media” button into the contributors post edit area. There is also a video below this code.
if ( current_user_can('contributor') && !current_user_can('upload_files') ) add_action('admin_init', 'allow_contributor_uploads'); function allow_contributor_uploads() { $contributor = get_role('contributor'); $contributor->add_cap('upload_files'); }
Set Featured Image Automatically
Where to put this code: This code gets pasted into the functions.php
file. You can see this full tutorial on how to set featured images automatically.
What does it do: It will place a default featured image if none, and a featured image if there is one.
function autoset_featured() { global $post; $already_has_thumb = has_post_thumbnail($post->ID); if (!$already_has_thumb) { $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" ); if ($attached_image) { foreach ($attached_image as $attachment_id => $attachment) { set_post_thumbnail($post->ID, $attachment_id); } } else { set_post_thumbnail($post->ID, '8000'); } } } //end function add_action('the_post', 'autoset_featured'); add_action('save_post', 'autoset_featured'); add_action('draft_to_publish', 'autoset_featured'); add_action('new_to_publish', 'autoset_featured'); add_action('pending_to_publish', 'autoset_featured'); add_action('future_to_publish', 'autoset_featured');
Remove the Thesis Atribution Link
Where to put this code: This code gets pasted into the customfunctions.php
for Thesis.
What does it do: It will remove the Thesis attribution link from your theme.
remove_action(‘thesis_hook_footer’, ‘thesis_attribution’);
More Website Code Snippets
SEO ALRP Slide Box Plugin Hidden Behind Theme Background
Disable Remember Me WordPress Login Page Option
Add a Comment Policy to WordPress