Skip to content

Instantly share code, notes, and snippets.

@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 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 / Include Tags In Search
Created March 22, 2013 03:53
Include Tags In Wordpress Search
@spencejs
spencejs / Add Alert Notice To Options Pages
Created March 22, 2013 03:57
Add Alert Notice To Options Pages In Wordpress
//Add Alert Notice To Options Pages
add_action( 'admin_notices', 'my_admin_notice' );
function my_admin_notice(){
global $current_screen;</div>
if ( $current_screen->parent_base == 'options-general' )
echo '<div><p>Warning - changing settings on these pages may cause problems with your website’s design!</p></div>';
}
@spencejs
spencejs / Caption For Featured Image
Created March 22, 2013 04:04
Caption For Featured Image In Wordpress
@spencejs
spencejs / Change Excerpt Length
Created March 22, 2013 04:34
Change Excerpt Length In Wordpress
//Change Excerpt Length
function new_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');
@spencejs
spencejs / Custom Title Field Text
Created March 22, 2013 04:44
Custom Title Field Text By Post Type In Wordpress
//Change Title By Post Type
function custom_title_text( $title ){
$screen = get_current_screen();
if ( 'issues' == $screen->post_type ) {
$title = 'Issue Months and Year e.g. "Jul/Aug 2011"';
}
return $title;
}
add_filter( 'enter_title_here', 'custom_title_text' );
@spencejs
spencejs / Customizable Additional Excerpt
Created March 22, 2013 04:46
Customizable Additional Excerpt In Wordpress ( USAGE: limit_content( $length, $allow-tags, '$tags-to-allow' ) )
//Custom Additional Excerpt Length
function custom_excerpt($content_length = 250, $allowtags = true, $allowedtags = '') {
global $post;
$exc = $post->post_excerpt;
if ($exc == '') {
$content = $post->post_content;
} else {
$content = $post->post_excerpt;
}
$content = apply_filters('the_content', $content);
@spencejs
spencejs / Custom Taxonomy Column.php
Last active December 15, 2015 06:49
Add Custom Taxonomy Admin Column In Wordpress
////////////////////////////////////////////////
//Add Project Type Column to Projects Backend
///////////////////////////////////////////////
add_filter('manage_edit-project_columns', 'project_columns');
function project_columns($columns) {
unset($columns['date']);
$columns['Type'] = 'Type';
$columns['date'] = 'Date';
return $columns;
}
@spencejs
spencejs / Custom Dashboard Widget - Side
Created March 22, 2013 04:51
Add Custom Dashboard Widget - Side - In Wordpress
//Add Custom Dashboard Widget - Side
add_action( 'wp_dashboard_setup', 'my_dashboard_setup_function' );
function my_dashboard_setup_function() {
add_meta_box( 'my_dashboard_widget', 'Electrico Useful Information', 'my_dashboard_widget_function', 'dashboard', 'side', 'high' );
}
function my_dashboard_widget_function() {
// widget content goes here
echo '<p>Some Content</p>';
}