Nov 8, 2011

Ten Wordpress Useful Functions And Snippets

Some useful Wordpress PHP funtions.

Just copy and paste these functions in your themes functions.php file

Change the WP Login Logo & URL Link

[php]
// CUSTOM ADMIN LOGIN HEADER LOGO
function my_custom_login_logo() {
echo '';
}
add_action('login_head', 'my_custom_login_logo');

// CUSTOM ADMIN LOGIN HEADER LINK & ALT TEXT
function change_wp_login_url() {
echo bloginfo('url'); // OR ECHO YOUR OWN URL
}
function change_wp_login_title() {
echo get_option('blogname'); // OR ECHO YOUR OWN ALT TEXT
}
add_filter('login_headerurl', 'change_wp_login_url');
add_filter('login_headertitle', 'change_wp_login_title');

[/php]

Load JQuery From Google CDN

[php]
// even more smart jquery inclusion :)
add_action( 'init', 'jquery_register' );

// register from google and for footer
function jquery_register() {

if ( !is_admin() ) {

wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', ( 'https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js' ), false, null, true );
wp_enqueue_script( 'jquery' );
}
}
[/php]

How to remove the WordPress Version Information

[php]
// remove version info from head and feeds
function complete_version_removal() {
return '';
}
add_filter('the_generator', 'complete_version_removal');

[/php]

Remove Default Wordpress Meta Boxes

[php]
// REMOVE META BOXES FROM DEFAULT POSTS SCREEN
function remove_default_post_screen_metaboxes() {
remove_meta_box( 'postcustom','post','normal' ); // Custom Fields Metabox
remove_meta_box( 'postexcerpt','post','normal' ); // Excerpt Metabox
remove_meta_box( 'commentstatusdiv','post','normal' ); // Comments Metabox
remove_meta_box( 'trackbacksdiv','post','normal' ); // Talkback Metabox
remove_meta_box( 'slugdiv','post','normal' ); // Slug Metabox
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
add_action('admin_menu','remove_default_post_screen_metaboxes');

// REMOVE META BOXES FROM DEFAULT PAGES SCREEN
function remove_default_page_screen_metaboxes() {
remove_meta_box( 'postcustom','post','normal' ); // Custom Fields Metabox
remove_meta_box( 'postexcerpt','post','normal' ); // Excerpt Metabox
remove_meta_box( 'commentstatusdiv','post','normal' ); // Comments Metabox
remove_meta_box( 'trackbacksdiv','post','normal' ); // Talkback Metabox
remove_meta_box( 'slugdiv','post','normal' ); // Slug Metabox
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
add_action('admin_menu','remove_default_page_screen_metaboxes');

[/php]

Include custom post types in the search results of WP

[php]
// MAKE CUSTOM POST TYPES SEARCHABLE
function searchAll( $query ) {
if ( $query->is_search ) { $query->set( 'post_type', array( 'site','plugin', 'theme','person' )); }
return $query;
}
add_filter( 'the_search_query', 'searchAll' );

[/php]

Add Excerpt to pages

[php]
if ( function_exists('add_post_type_support') )
{
add_action('init', 'add_page_excerpts');
function add_page_excerpts()
{
add_post_type_support( 'page', 'excerpt' );
}
}

[/php]

Change the order of Admin Menu

[php]
// CUSTOMIZE ADMIN MENU ORDER
function custom_menu_order($menu_ord) {
if (!$menu_ord) return true;
return array(
'index.php', // this represents the dashboard link
'edit.php?post_type=events', // this is a custom post type menu
'edit.php?post_type=news',
'edit.php?post_type=articles',
'edit.php?post_type=faqs',
'edit.php?post_type=mentors',
'edit.php?post_type=testimonials',
'edit.php?post_type=services',
'edit.php?post_type=page', // this is the default page menu
'edit.php', // this is the default POST admin menu
);
}
add_filter('custom_menu_order', 'custom_menu_order');
add_filter('menu_order', 'custom_menu_order');

[/php]

Change the length of excerpts

[php]
function new_excerpt_length($length) {
return 100;
}

add_filter('excerpt_length', 'new_excerpt_length');

[/php]

Enable GZIP output compression

[php]
if(extension_loaded("zlib") && (ini_get("output_handler") != "ob_gzhandler"))
add_action('wp', create_function('', '@ob_end_clean();@ini_set("zlib.output_compression", 1);'));

[/php]

Get the First Image from the Post Content

[php]
// AUTOMATICALLY EXTRACT THE FIRST IMAGE FROM THE POST
function getImage($num) {
global $more;
$more = 1;
$link = get_permalink();
$content = get_the_content();
$count = substr_count($content, ' $start = 0;
for($i=1;$i $imgBeg = strpos($content, ' $postOutput = substr($post, 0, $imgEnd+1);
$postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
$image[$i] = $postOutput;
$start=$imgEnd+1;
}
if(stristr($image[$num],' $more = 0;
}

[/php]

Credit: Users Of wordpress.stackexchange.com

1 comment :