10 Super-Useful WordPress Snippets & Hacks

<?php function debugUsr($uID) { $data = get_userdata($uID); $unsets = Array ( "ID", "user_login", "user_pass", "user_nicename", "user_email", "user_url" , "user_registered", "user_activation_key" , "user_status", "display_name" , "first_name" , "last_name" , "nickname", "description" , "rich_editing" , "comment_shortcuts", "admin_color" , "use_ssl" , "show_admin_bar_front", "show_admin_bar_admin", "aim" , "yim" , "jabber" , "wp_capabilities", "wp_user_level" , "wp_usersettings" , "wp_usersettingstime" , "wp_dashboard_quick_press_last_post_id" , "nav_menu_recently_edited" , "managenavmenuscolumnshidden" , "metaboxhidden_navmenus" , "metaboxorder_d_offer" , "screen_layout_d_offer" , "plugins_last_view", "user_level" , "user_firstname" , "user_lastname" , "user_description" ); foreach ( $unsets as $unset) { unset($data->$unset); } debug($data); } ?>

Restrict wp-admin access to a few roles

<?php function wp_hide_update() { global $current_user; get_currentuserinfo(); if ($current_user->ID != 1) { // only admin will see it remove_action( ‘admin_notices’, ‘update_nag’, 3 ); } } add_action(‘admin_menu’,’wp_hide_update’); ?>

Hide items from wp-admin

<?php function new_excerpt_more($more) { return ‘[much more to go]’; } add_filter(‘excerpt_more’, ‘new_excerpt_more’); ?>

Remove WordPress Admin Bar

<?php function get_excerpt_outside_loop($post_id) { global $wpdb; $query = ‘SELECT post_excerpt FROM ‘. $wpdb->posts .’ WHERE ID = ‘. $post_id .’ LIMIT 1′; $result = $wpdb->get_results($query, ARRAY_A); $post_excerpt=$result[0][‘post_excerpt’]; return $post_excerpt; } ?>

Replace “[…]” string with the_excerpt()

If you’re looking to add extra functionality to WordPress, take a look at this roundup of PHP snippets.
<?php function contactMail($post) { $attachments = ""; $subject = "[MyBlog] Contact"; $name = $post["name"]; $email = $post["email"]; $message = $post["message"]; ob_start(); include(TEMPLATEPATH . ‘/_mails/contact.html’); $message = ob_get_contents(); ob_end_clean(); $headers = "From: me@myblog.com rn"; $headers .= "Return-Path: me@myblog.com rn"; $headers .= "MIME-Version: 1.0rn"; $headers .= "Content-Type: text/html; charset=UTF-8rn"; $headers .= "BCC: thisIsMe@gmail.comrn"; //$headers .= "BCC: rochesterj@gmail.comrn"; wp_mail( $email, $subject, $message, $headers, $attachments ); } ?>

Know what’s going on with debug() function

<?php function mail_from() { $emailaddress = ‘contact@1stwebdesigner.com’; return $emailaddress; } function mail_from_name() { $sendername = "1stWebDesigner.com – Dainis"; return $sendername; } add_filter(‘wp_mail_from’,’mail_from’); add_filter(‘wp_mail_from_name’,’mail_from_name’); ?>

<?php function restrict_access_admin_panel(){ global $current_user; get_currentuserinfo(); if ($current_user->user_level < 4) { //if not admin, die with message wp_redirect( get_bloginfo(‘url’) ); exit; } } add_action(‘admin_init’, ‘restrict_access_admin_panel’, 1); ?>

Hide update warning for every user but admin

<?php function debug($var) { if ( ! is_string($var) ) { echo "<pre>"; print_r($var); echo "</pre>"; } else { echo "DEBUG: ".$var; } } ?>

See user custom fields with debugUsr() function

To make things even easier, I put this inside one function in my functions.php so it helps me a lot:
<?php function mail_from() { $emailaddress = ‘contact@1stwebdesigner.com’; return $emailaddress; } function mail_from_name() { $sendername = "1stWebDesigner.com – Dainis"; return $sendername; } add_filter(‘wp_mail_from’,’mail_from’); add_filter(‘wp_mail_from_name’,’mail_from_name’); ?>

HTML Emails with wp_mail()

<?php global $current_user; get_currentuserinfo(); if ($current_user->ID != 1) { add_filter( ‘show_admin_bar’, ‘__return_false’ ); } ?>

Adjust from name and email for wp_mail()

<?php if ( is_admin() ) { $url = $_SERVER[‘SCRIPT_NAME’]; $url = explode(‘/’, $url); $tam = count($url); if ( $url [ ( $tam – 1 ) ] == "index.php" ) { $url = ‘Location: ‘ . get_bloginfo(‘url’) . "/wp-admin/edit.php?post_type=page"; header( $url ); } } ?>

What do you think?

When you use WordPress Custom Post Types to store data this is a really useful function. Sometimes you want to duplicate all post meta, create something based on another. Well, just run this function, and you’re done.
<?php function duplicate($post) { $title = get_the_title($post); $post = array( ‘post_title’ => $title, ‘post_status’ => ‘publish’, ‘post_type’ => ‘post’, ‘post_author’ => 1 ); $post_id = wp_insert_post( $post ); $data = get_post_custom($post); foreach ( $data as $key => $values) { foreach ($values as $value) { add_post_meta( $post_id, $key, $value ); } } return $post_id; } ?>

Get excerpt outside The Loop

<?php function remove_dashboard_widgets() { global $menu,$submenu; global $current_user; get_currentuserinfo(); if ($current_user->ID != 1) { // only admin sees the whole thing // $menu and $submenu will return fo all menu and submenu list in admin panel . $menu[2] = ""; //Dashboard $menu[5] = ""; // Posts $menu[15] = ""; //Links $menu[25] = ""; //Comments $menu[65] = ""; //Plugins unset($submenu[‘themes.php’][5]); //themes unset($submenu[‘themes.php’][12]); //editor } } add_action(‘admin_head’, ‘remove_dashboard_widgets’); ?>

Redirect for different page instead of dashboard

If you hide dashboard you may want another page instead of an ugly error when user goes to wp-admin, right? So we can redirect them when they try to access just wp-admin:

Posted by WordPress Guru