WordPress functions collection

dinsdag 04 mei, 2010

Here is a collection of collected WordPress functions to expand your website.

Limit number of post revisions

[code lang="php"]
define('WP_POST_REVISIONS', 5);
[/code]

Custom Excerpt

[code lang="php"]
function new_excerpt_more($more) {
return '... <a href="'.get_permalink().'" rel="bookmark">Lees verder &raquo;</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');
[/code]

Limit excerpt lenght

[code lang="php"]
function new_excerpt_length($length) {
return 15;
}
add_filter('excerpt_length', 'new_excerpt_length');
[/code]

Add thumb support for posts

[code lang="php"]
add_theme_support( 'post-thumbnails', array( 'post') );
set_post_thumbnail_size(220, 220, true);
[/code]

Get first image

[code lang="php"]
function get_first_image($post_ID, $fullsize=false, $max_dims=false){
$thumbargs = array(
'post_type' => 'attachment',
'post_status' => null,
'post_parent' => $post_ID
);
$thumbs = get_posts($thumbargs);
if ($thumbs) {
$num = count($thumbs)-1;
return get_attachment_innerHTML($thumbs[$num]->ID, $fullsize, $max_dims);
}
}
[/code]

Remove admin menu

[code lang="php"]
function remove_menus () {
global $menu;
$restricted = array(__('Dashboard'), __('Posts'), __('Media'), __('Links'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins'));
end ($menu);
while (prev($menu)){
$value = explode(' ',$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
}
}
add_action('admin_menu', 'remove_menus');
[/code]

Remove default gallery css

[code lang="php"]
add_filter('gallery_style', create_function('$a', 'return "<div class=\'gallery\'>";'));
[/code]

Get the first category id

[code lang="php"]
function get_first_category_ID() {
$category = get_the_category();
return $category[0]->cat_ID;
}
[/code]

Custom admin icon

[code lang="php"]

function my_custom_logo() {
echo '
<style type="text/css">
#header-logo { background-image: url('.get_bloginfo('template_directory').'/32x32/rotate.php) !important; }
</style>
';
}

add_action('admin_head', 'my_custom_logo');
[/code]

Custom login Logo

[code lang="php"]
function custom_login() {
echo '
<style type="text/css">
h1 a {
background:url('.get_bloginfo('template_directory').'/images/login_logo.png) 0 0 no-repeat;
width:325px;
height:100px;
}
</style>';
}
add_action('login_head', 'custom_login');
[/code]

Dashboard setup / Custom help pane

[code lang="php"]
function my_custom_dashboard_widgets() {
global $wp_meta_boxes;
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
wp_add_dashboard_widget('custom_help_widget', 'Help and Support', 'custom_dashboard_help');
}
function custom_dashboard_help() {
echo '<p>Welcome to '.get_bloginfo('url').'<br />Need help? Contact Jonas Vorwerk <a href="http://www.jonasvorwerk.com">here</a>.<br /><br /><a href="http://www.jonasvorwerk.com" target="_blank"><img src="http://www.jonasvorwerk.com/wp-content/themes/JonasVorwerk/32x32/rotate.php" alt="Jonas Vorwerk" title="http://www.jonasvorwerk.com" width="25"></a></p>';
}
add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');
[/code]

Get custom field value

[code lang="php"]
function get_custom_field_value($szKey, $bPrint = false) {
global $post;
$szValue = get_post_meta($post->ID, $szKey, true);
if ( $bPrint == false ) return $szValue; else echo $szValue;
}
[/code]

Add a favicon to your blog

[code lang="php"]
function blog_favicon() {
echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('template_directory').'/images/favicon.ico" />';
}
add_action('wp_head', 'blog_favicon');
[/code]

Add a favicon for your admin

[code lang="php"]
function admin_favicon() {
echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('template_directory').'/images/favicon.ico" />';
}
add_action('admin_head', 'admin_favicon');
[/code]

No more jumping for read more link

[code lang="php"]
function no_more_jumping($post) {
return '<a href="'.get_permalink($post->ID).'">'.'Continue Reading'.'</a>';
}
add_filter('excerpt_more', 'no_more_jumping');
[/code]

Filter categories in rss feed

[code lang="php"]
function FeedFilter($query) {
if ($query->is_feed) {
$query->set('cat','-60');
}
return $query;
}
add_filter('pre_get_posts','FeedFilter');
[/code]

Remove rss feeds

[code lang="php"]
function fb_disable_feed() {
wp_die( __('No feed available,please visit our <a href="'. get_bloginfo('url') .'">homepage</a>!') );
}

add_action('do_feed', 'fb_disable_feed', 1);
add_action('do_feed_rdf', 'fb_disable_feed', 1);
add_action('do_feed_rss', 'fb_disable_feed', 1);
add_action('do_feed_rss2', 'fb_disable_feed', 1);
add_action('do_feed_atom', 'fb_disable_feed', 1);
[/code]

WordPress.com stats smiley remover (css)

[code lang="css"]
img#wpstats{display:none}
[/code]

Get current page number

[code lang="php"]
$pageNumber = (get_query_var('paged')) ? get_query_var('paged') : 1;
[/code]

Menu support WP3.0

[code lang="php"]
add_theme_support('menus');
[/code]

Remove wp version number

[code lang="php"]
remove_action('wp_head', 'wp_generator');
[/code]