Skip to content

Instantly share code, notes, and snippets.

@spencejs
spencejs / Add Custom Field and Value To All Pages
Created March 22, 2013 03:46
Add Custom Field and Value To All Pages On Publish In Wordpress
//Add Custom Field and Value to all pages
add_action('publish_page', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
global $wpdb;
if(!wp_is_post_revision($post_ID)) {
add_post_meta($post_ID, 'ngg_gal_Images', '42', true);
}
}
@spencejs
spencejs / Add Single Post Templating By Category
Created March 22, 2013 03:44
Add Single Post Templating By Category In Wordpress
//Add Single Post Templating By Category
add_filter('single_template', create_function('$t', 'foreach( (array) get_the_category() as $cat ) { if ( file_exists(TEMPLATEPATH . "/single-{$cat->term_id}.php") ) return TEMPLATEPATH . "/single-{$cat->term_id}.php"; } return $t;' ));
@spencejs
spencejs / Add Post Type To Category Archives
Created March 22, 2013 03:43
Add Post Type To Category Archives In Wordpress
// Add Post Type To Category Archives
add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
if(is_category() || is_tag()) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array('nav_menu_item','post','private_post'); // replace private_post to your custom post type, Leave nav_menu_item where it is!
$query->set('post_type',$post_type);
@spencejs
spencejs / Add Post Thumbnail to Feed
Created March 22, 2013 03:42
Add Post Thumbnail to Feed in Wordpress
//Add Post Thumbnail to Feed
function feedFilter($query) {
if ($query->is_feed) {
add_filter('the_content', 'feedContentFilter');
}
return $query;
}
add_filter('pre_get_posts','feedFilter');
function feedContentFilter($content) {
@spencejs
spencejs / Add Post Thumbnails
Created March 22, 2013 03:39
Add Post Thumbnails To Wordpress
add_theme_support('post-thumbnails');
set_post_thumbnail_size( 150, 120, true ); // Normal post thumbnails
add_image_size( 'magazine-post-thumbnail', 260, 174, true ); // Permalink thumbnail size
add_image_size( 'home-post-thumbnail', 168, 89, true ); // Permalink thumbnail size
add_image_size( 'single-post-thumbnail', 527, 317, true ); // Permalink thumbnail size
@spencejs
spencejs / Meta Boxes Without A Plugin
Created March 22, 2013 03:38
Add Wordpress Meta Boxes Without A Plugin
/**
Made with the help of a tutorial at WPShout.com => http://wpshout.com.
Courtesy of the Hybrid theme - themehybrid.com
* Adds the Hybrid Settings meta box on the Write Post/Page screeens
*
* @package Hybrid
* @subpackage Admin
*/
@spencejs
spencejs / Featured Image Instructions
Created March 22, 2013 03:35
Add Instruction Text To Featured Image Box In Wordpress Admin
@spencejs
spencejs / Add Custom Post type To Feed
Created March 22, 2013 03:34
Add Custom Post Type To Main Wordpress Feed
//Add Custom Post Type To Feed
function mysite_feed_request($vars) {
if (isset($vars['feed']) && !isset($vars['post_type']))
$vars['post_type'] = array('post', 'projects');
return $vars;
}
add_filter('request', 'mysite_feed_request');
@spencejs
spencejs / Custom Meta Value Column
Last active December 15, 2015 06:49
Add Column With A Custom Field To Wordpress Admin Post Listing
///////////////////////////////////////////////////////////
//Custom Backend Column from Custom Field. Be sure to edit post-type- "manage_edit-POSTTYPE_columns"- and Custom Field Name.
///////////////////////////////////////////////////////////
add_filter('manage_edit-staff_columns', 'my_columns');
function my_columns($columns) {
unset($columns['author']);
unset($columns['date']);
$columns['Name'] = 'Name';
$columns['author'] = 'Author';
$columns['date'] = 'Date';
@spencejs
spencejs / Categories As Body Class Names
Created March 22, 2013 03:29
Add Categories For a Post As Class Names On The Body Tag - Wordpress
// add category nicenames in body and post class
function category_id_class($classes) {
global $post;
foreach((get_the_category($post->ID)) as $category)
$classes[] = $category->category_nicename;
return $classes;
}
add_filter('post_class', 'category_id_class');
add_filter('body_class', 'category_id_class');