Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Please vote on the question and any answers you find useful by clicking on the UP arrow on the left hand side of the question or answer.

As with many others who are now viewing this post, I have been reading various blogs, forums, and discussion groups to learn and improve my wordpress skills. Over the past 12 months I have been on a mission to substitute my use of plugins by adding code to my functions.php file instead. While I completely agree that plugins are very useful in many situations my experience proved that in 90% of usage cases although a plugin might exist, actually utilizing it could create unnecessary complications and compatibility issues. Additionally in a great deal of cases such plugins added menus and other admin elements which I don't want or need.

More often than not I have found that by analyzing the code of plugins I was able to strip out the piece of code I wanted and hard code it into my functions.php. This provided me with the exact functionality I needed without having to include unnecessary elements.

So, the purpose of this post is my attempt to engage you, the reader/admin/developer, to share with me and other here any code bits which you find useful and have added to your theme's function.php file to extend or enhance WordPress without utilizing a plugin.

When you submit a response here please kindly give each code bit a title, let us know if with what version of wordpress you know its compatible with, include whatever description you feel best describes its function and (if applicable) include a link to the original plugin or source where you found the information.

I am looking forward to all your responses and will of course continually add my own new finds whenever I find them.

share|improve this question
9  
Considering the first 5 answers were by the OP and the question seems more geared at collecting an array of responses rather than a single, definitive answer, this should be a community wiki. –  EAMann Sep 9 '10 at 14:43
13  
All answers not related to a theme should be removed. This thread is a good example for bad coding practices. –  toscho Jun 13 '11 at 12:09
14  
I think it'd be better to encourage people to create a custom functionality plugin instead of using their theme's functions.php –  Ian Dunn Jan 16 '12 at 18:49
2  
@NetConstructor.com The pure number of page views is not an indicator for quality. We should encourage specific questions with specific answers and good coding practices. This thread is the opposite. –  toscho Feb 22 '12 at 2:44
5  
@NetConstructor.com Discuss it on Meta where people can see your arguments better. :) –  toscho Feb 24 '12 at 17:10
show 20 more comments

closed as not constructive by Rarst Feb 15 '12 at 12:44

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

108 Answers

List all constants for information and debugging

Tested on: Wordpress 3.0.1

Will only display the information if you are a logged in user

if ( is_user_logged_in() ) {
    print('<pre>');
    print_r( @get_defined_constants() );
    print('</pre>');
}

Here is version with optional filter that will partially match constant names and values:

function constants($filter = false) {

        $constants = get_defined_constants();

        if( $filter ) {

            $temp = array();

            foreach ( $constants as $key => $constant )
                if( false !== stripos( $key, $filter ) || false !== stripos( $constant, $filter ) )
                    $temp[$key] = $constant;

            $constants = $temp;
        }

        ksort( $constants );
        var_dump( $constants );
    }
share|improve this answer
add comment

Pre-populating post types

Here is one for this collection.

////////////////////////////////////////////////////////////////////////////////////
// This auto populates post types and posts.
///////////////////////////////////////////////////////////////////////////////////

add_filter( 'default_content', 'my_editor_content' );

function my_editor_content( $content ) {

    global $post_type;

    switch( $post_type ) {
        case 'your_post_type_here': //auto populate
            $content = 'The content you want to pre-populate the post type with.';
break;
     }

    return $content;
}

This can come in handy for when you need to post the same info over and over again with slight differences.

share|improve this answer
show 3 more comments

Exclude child categories of a specific category.

Pretty straight forward, but I couldn't find anyone doing exactly what I needed. Also it will display the post if the parent category is selected, except if that post has a child category of that parent selected.

   /* this code excludes all of the children of (category id = 20) posts
       on the HOME page, but allows parent (category id = 20) to be shown. */

    function exclude_category_children($query) {
        $child_cats = (array) get_term_children('20', 'category');
        if ( $query->is_home ) {
        $query->set('category__not_in', $child_cats);
        return $query;
        }
    }
    add_filter('pre_get_posts', 'exclude_category_children');
share|improve this answer
add comment

Make WordPress Editor Allow iFrames

// make TinyMCE allow iframes
add_filter('tiny_mce_before_init', create_function( '$a',
'$a["extended_valid_elements"] = "iframe[id|class|title|style|align|frameborder|height|longdesc|marginheight|marginwidth|name|scrolling|src|width]"; return $a;') );
share|improve this answer
add comment

Add "Next-page"-button in WYSIYG-editor

add_filter('mce_buttons','wysiwyg_editor');
function wysiwyg_editor($mce_buttons) {
    $pos = array_search('wp_more',$mce_buttons,true);
    if ($pos !== false) {
        $tmp_buttons = array_slice($mce_buttons, 0, $pos+1);
        $tmp_buttons[] = 'wp_page';
        $mce_buttons = array_merge($tmp_buttons, array_slice($mce_buttons, $pos+1));
    }
    return $mce_buttons;
}
share|improve this answer
add comment

Change default Author Slug

Put this in your functions to change the default Author Slug to whatever you want,
just change the "sellers" to the slug you want.

// Change URL Slug from Author to Sellers
function new_author_base() {
    global $wp_rewrite;
    $author_slug = 'sellers';
    $wp_rewrite->author_base = $author_slug;
}
add_action('init', 'new_author_base');
share|improve this answer
4  
You'd not want to run that code on every page load however. –  t31os Dec 21 '10 at 20:27
show 2 more comments

Loading scripts conditionally

Here is a way to load scripts only if a particular shortcode or widget is present. source: Loading scripts only if a particular shortcode or widget is present

function has_my_shortcode($posts) {
    if ( empty($posts) )
        return $posts;
    $found = false;
    foreach ($posts as $post) {
        if ( stripos($post->post_content, '[my_shortcode') )
            $found = true;
            break;
        }
    if ($found){
        $urljs = get_bloginfo( 'template_directory' ).IMP_JS;
    wp_register_script('my_script', $urljs.'myscript.js' );
    wp_print_scripts('my_script');
}
    return $posts;
}
add_action('the_posts', 'has_my_shortcode');

And here is how to load scripts only if a certain widget is present

To load the script in the page where the widget is loaded only, you will have to add the is_active_widget() code, in you widget class. E.g., see the default recent comments widget (wp-includes/default-widgets.php, line 602):

class WP_Widget_Recent_Comments extends WP_Widget {
    function WP_Widget_Recent_Comments() {
        $widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'The most recent comments' ) );
        $this->WP_Widget('recent-comments', __('Recent Comments'), $widget_ops);
        $this->alt_option_name = 'widget_recent_comments';
        if ( is_active_widget(false, false, $this->id_base) )
        add_action( 'wp_head', array(&$this, 'recent_comments_style') );
        add_action( 'comment_post', array(&$this, 'flush_widget_cache') );
        add_action( 'transition_comment_status', array(&$this, 'flush_widget_cache') );
    }
share|improve this answer
add comment

Enable Numeric Pagination

Tested on: Wordpress 3.0.1

/* Numeric Pagination ********************************************/

function numeric_pagination ($pageCount = 9, $query = null) {

 if ($query == null) {
  global $wp_query;
  $query = $wp_query;
 }

 if ($query->max_num_pages <= 1) {
  return;
 }

 $pageStart = 1;
 $paged = $query->query_vars['paged'];

 // set current page if on the first page
 if ($paged == null) {
  $paged = 1;
 }

 // work out if page start is halfway through the current visible pages and if so move it accordingly
 if ($paged > floor($pageCount / 2)) {
  $pageStart = $paged - floor($pageCount / 2);
 }

 if ($pageStart < 1) {
  $pageStart = 1;
 }

 // make sure page start is
 if ($pageStart + $pageCount > $query->max_num_pages) {
  $pageCount = $query->max_num_pages - $pageStart;
 }

?>
 <div id="archive_pagination">
<?php
 if ($paged != 1) {
?>
 <a href="<?php echo get_pagenum_link(1); ?>" class="numbered page-number-first"><span>&lsaquo; <?php _e('<< First', 'global'); ?></span></a>
<?php
 }
 // first page is not visible...
 if ($pageStart > 1) {
  //echo 'previous';
 }
 for ($p = $pageStart; $p <= $pageStart + $pageCount; $p ++) {
  if ($p == $paged) {
?>
  <span class="numbered page-number-<?php echo $p; ?> current-numeric-page"><?php echo $p; ?></span>
<?php } else { ?>
  <a href="<?php echo get_pagenum_link($p); ?>" class="numbered page-number-<?php echo $p; ?>"><span><?php echo $p; ?></span></a>

<?php
  }
 }
 // last page is not visible
 if ($pageStart + $pageCount < $query->max_num_pages) {
  //echo "last";
 }
 if ($paged != $query->max_num_pages) {
?>
  <a href="<?php echo get_pagenum_link($query->max_num_pages); ?>" class="numbered page-number-last"><span><?php _e('>> Last', 'global'); ?> &rsaquo;</span></a>
<?php } ?>

 </div>
share|improve this answer
10  
There's a function in WordPress for doing numeric pagination already, see codex.wordpress.org/Function_Reference/paginate_links –  t31os Nov 11 '10 at 17:09
add comment

Display Specific Content only for logged in users

Tested on: Wordpress 3.0.1

function content_only4logged_in($content) {

    // ALL LOGGED IN USERS
    if ( is_user_logged_in() &&
            !is_null($content) &&
            !is_feed()
         ) {
        return $content;
    } else {
        $content  = wp_html_excerpt( $content, 80 );
        $content .= ' …';
        $content .= __( 'Sorry, more of this content is only available for logged users.', FB_TEXTDOMAIN );
        return $content;
    }
}
add_action( 'the_content', 'content_only4logged_in' );

more possibilities and informations on the post http://wpengineer.com/2046/control-the-wordpress-content-via-userrights-or-time/

share|improve this answer
show 1 more comment

Auto Include Google Analytics Code

Tested on: Wordpress 3.1 RC3

I've been using this script on all of my sites since WordPress 2.3.0 ... it just adds the standard Google tracking scripts to the footer.

// Add Google Analytics Tracking Code
function add_google_analytics() {
?>
<script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
    try {
        var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
        pageTracker._trackPageview();
    } catch(err) {}</script>
<?php
}

add_action('wp_footer', 'add_google_analytics');

Just be sure to replace UA-XXXXXXX-X with your own Google tracking code...

share|improve this answer
1  
Not sure about adding campaign tracking variables only because I haven't had a need for that yet. But to add the same scripts to admin pages, you'd just use add_action('admin_footer', 'add_google_analytics'); to hook on to the footer of admin pages. –  EAMann Sep 17 '10 at 13:08
show 1 more comment

Resize large image on upload

Image will be resize according to the large size in your media settings.

/**resize on upload to the largest size in media setting */


function replace_uploaded_image($image_data) {
// if there is no large image : return
if (!isset($image_data['sizes']['large'])) return $image_data;

// path to the uploaded image and the large image
$upload_dir = wp_upload_dir();
$uploaded_image_location = $upload_dir['basedir'] . '/' .$image_data['file'];
$large_image_location = $upload_dir['path'] . '/'.$image_data['sizes']['large']['file'];

// delete the uploaded image
unlink($uploaded_image_location);

// rename the large image
rename($large_image_location,$uploaded_image_location);

// update image metadata and return them
$image_data['width'] = $image_data['sizes']['large']['width'];
$image_data['height'] = $image_data['sizes']['large']['height'];
unset($image_data['sizes']['large']);

return $image_data;
}
add_filter('wp_generate_attachment_metadata','replace_uploaded_image');
share|improve this answer
show 1 more comment

Replace Default Gravatar with Custom Image

All you'll need to customize is the path to your default image.

function custom_gravatar($avatar_defaults) {
    $logo = get_bloginfo('template_directory') . '/images/icons/gravatar_logo.jpg'; //Change to whatever path you like.
    $avatar_defaults[$logo] = get_bloginfo('name');
    return $avatar_defaults;
}//END FUNCTION    
add_filter( 'avatar_defaults', 'custom_gravatar' );
share|improve this answer
add comment

is_tree() conditional Function

/* Adapted from csstricks with addition of
ancestors .... use = if(is_tree($id)) { // do stuff } ... Returns true if the
page is  = $id OR any of it's children OR descendants */

function is_tree($pid) {      // $pid = The ID of the page we're looking for pages underneath
  global $post;         // load details about this page
  $ancestors = get_post_ancestors($post);
  if(is_page()&&($post->post_parent==$pid||is_page($pid)||(in_array($pid,$ancestors))))
    return true;   // we're at the page or at a sub page
  else
    return false;  // we're elsewhere 
  };
share|improve this answer
add comment

Create a conditional tag for custom taxonomies

In this example, 'student' was a custom post type, and 'stud_cat' was the custom taxonomy. Use has_student(null) for the conditional

    function has_student( $student, $_post = null ) {
    if ( !empty( $student ) )
        return false;
    if ( $_post )
        $_post = get_post( $_post );
    else
        $_post =& $GLOBALS['post'];
    if ( !$_post )
        return false;
    $r = is_object_in_term( $_post->ID, 'studcat', $student );
    if ( is_wp_error( $r ) )
        return false;
    return $r;
}
share|improve this answer
1  
I think this gets replaced with has_term() now that 3.1 is released? –  Rarst Feb 25 '11 at 23:32
add comment

Add custom styles to TinyMCE editor

Sometimes users and clients get confused about how their content is displayed in the editor versus how it is displayed on the front-end. Copying just a few relevant lines from your main stylesheet into your new tinymce.css can help a lot:

function custom_mce_css($wp) {
    return $wp .= ',' . get_bloginfo('stylesheet_directory') . '/css/tinymce.css';
}
add_filter( 'mce_css', 'custom_mce_css' );
share|improve this answer
show 3 more comments

Auto Clean SEO Slugs without removing functionality

Tested on: Wordpress 3.0.1

By adding this code to your functions.php file wordpress will automatically clean up the slug URL title by removing all unnecessary words. I have also extended the capabilities with additional customizations which hide the screen option for slug as well as the metabox. By including the code below any new post you create will automatically get shortened and you will still have the ability to manually edit the slug by clicking on the url under the post title and saving the post.

// AUTOMATICALLY SANITIZE PAGE/POST SEO SLUG FROM SHORT WORDS
   add_filter('name_save_pre', 'seo_slugs', 0);
   function seo_slugs($slug) {
    // We don't want to change an existing slug
 if ($slug) return $slug;
 global $wpdb;
 $seo_slug = strtolower(stripslashes($_POST['post_title']));
 $seo_slug = preg_replace('/&.+?;/', '', $seo_slug); // kill HTML entities
    // kill anything that is not a letter, digit, space or apostrophe
 $seo_slug = preg_replace ("/[^a-zA-Z0-9 \']/", "", $seo_slug);
    // Turn it to an array and strip common words by comparing against c.w. array
 $seo_slug_array = array_diff (split(" ", $seo_slug), seo_slugs_stop_words());
    // Turn the sanitized array into a string
 $seo_slug = join("-", $seo_slug_array);
 return $seo_slug;
   }
   function seo_slugs_stop_words () {
   return array ("a", "able", "about", "above", "abroad", "according", "accordingly", "across", "actually", "adj", "after", "afterwards", "again", "against", "ago", "ahead", "ain't", "all", "allow", "allows", "almost", "alone", "along", "alongside", "already", "also", "although", "always", "am", "amid", "amidst", "among", "amongst", "an", "and", "another", "any", "anybody", "anyhow", "anyone", "anything", "anyway", "anyways", "anywhere", "apart", "appear", "appreciate", "appropriate", "are", "aren't", "around", "as", "a's", "aside", "ask", "asking", "associated", "at", "available", "away", "awfully", "b", "back", "backward", "backwards", "be", "became", "because", "become", "becomes", "becoming", "been", "before", "beforehand", "begin", "behind", "being", "believe", "below", "beside", "besides", "best", "better", "between", "beyond", "both", "brief", "but", "by", "c", "came", "can", "cannot", "cant", "can't", "caption", "cause", "causes", "certain", "certainly", "changes", "clearly", "c'mon", "co", "co.", "com", "come", "comes", "concerning", "consequently", "consider", "considering", "contain", "containing", "contains", "corresponding", "could", "couldn't", "course", "c's", "currently", "d", "dare", "daren't", "definitely", "described", "despite", "did", "didn't", "different", "directly", "do", "does", "doesn't", "doing", "done", "don't", "down", "downwards", "during", "e", "each", "edu", "eg", "eight", "eighty", "either", "else", "elsewhere", "end", "ending", "enough", "entirely", "especially", "et", "etc", "even", "ever", "evermore", "every", "everybody", "everyone", "everything", "everywhere", "ex", "exactly", "example", "except", "f", "fairly", "far", "farther", "few", "fewer", "fifth", "first", "five", "followed", "following", "follows", "for", "forever", "former", "formerly", "forth", "forward", "found", "four", "from", "further", "furthermore", "g", "get", "gets", "getting", "given", "gives", "go", "goes", "going", "gone", "got", "gotten", "greetings", "h", "had", "hadn't", "half", "happens", "hardly", "has", "hasn't", "have", "haven't", "having", "he", "he'd", "he'll", "hello", "help", "hence", "her", "here", "hereafter", "hereby", "herein", "here's", "hereupon", "hers", "herself", "he's", "hi", "him", "himself", "his", "hither", "hopefully", "how", "howbeit", "however", "hundred", "i", "i'd", "ie", "if", "ignored", "i'll", "i'm", "immediate", "in", "inasmuch", "inc", "inc.", "indeed", "indicate", "indicated", "indicates", "inner", "inside", "insofar", "instead", "into", "inward", "is", "isn't", "it", "it'd", "it'll", "its", "it's", "itself", "i've", "j", "just", "k", "keep", "keeps", "kept", "know", "known", "knows", "l", "last", "lately", "later", "latter", "latterly", "least", "less", "lest", "let", "let's", "like", "liked", "likely", "likewise", "little", "look", "looking", "looks", "low", "lower", "ltd", "m", "made", "mainly", "make", "makes", "many", "may", "maybe", "mayn't", "me", "mean", "meantime", "meanwhile", "merely", "might", "mightn't", "mine", "minus", "miss", "more", "moreover", "most", "mostly", "mr", "mrs", "much", "must", "mustn't", "my", "myself", "n", "name", "namely", "nd", "near", "nearly", "necessary", "need", "needn't", "needs", "neither", "never", "neverf", "neverless", "nevertheless", "new", "next", "nine", "ninety", "no", "nobody", "non", "none", "nonetheless", "noone", "no-one", "nor", "normally", "not", "nothing", "notwithstanding", "novel", "now", "nowhere", "o", "obviously", "of", "off", "often", "oh", "ok", "okay", "old", "on", "once", "one", "ones", "one's", "only", "onto", "opposite", "or", "other", "others", "otherwise", "ought", "oughtn't", "our", "ours", "ourselves", "out", "outside", "over", "overall", "own", "p", "particular", "particularly", "past", "per", "perhaps", "placed", "please", "plus", "possible", "presumably", "probably", "provided", "provides", "q", "que", "quite", "qv", "r", "rather", "rd", "re", "really", "reasonably", "recent", "recently", "regarding", "regardless", "regards", "relatively", "respectively", "right", "round", "s", "said", "same", "saw", "say", "saying", "says", "second", "secondly", "see", "seeing", "seem", "seemed", "seeming", "seems", "seen", "self", "selves", "sensible", "sent", "serious", "seriously", "seven", "several", "shall", "shan't", "she", "she'd", "she'll", "she's", "should", "shouldn't", "since", "six", "so", "some", "somebody", "someday", "somehow", "someone", "something", "sometime", "sometimes", "somewhat", "somewhere", "soon", "sorry", "specified", "specify", "specifying", "still", "sub", "such", "sup", "sure", "t", "take", "taken", "taking", "tell", "tends", "th", "than", "thank", "thanks", "thanx", "that", "that'll", "thats", "that's", "that've", "the", "their", "theirs", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "there'd", "therefore", "therein", "there'll", "there're", "theres", "there's", "thereupon", "there've", "these", "they", "they'd", "they'll", "they're", "they've", "thing", "things", "think", "third", "thirty", "this", "thorough", "thoroughly", "those", "though", "three", "through", "throughout", "thru", "thus", "till", "to", "together", "too", "took", "toward", "towards", "tried", "tries", "truly", "try", "trying", "t's", "twice", "two", "u", "un", "under", "underneath", "undoing", "unfortunately", "unless", "unlike", "unlikely", "until", "unto", "up", "upon", "upwards", "us", "use", "used", "useful", "uses", "using", "usually", "v", "value", "various", "versus", "very", "via", "viz", "vs", "w", "want", "wants", "was", "wasn't", "way", "we", "we'd", "welcome", "well", "we'll", "went", "were", "we're", "weren't", "we've", "what", "whatever", "what'll", "what's", "what've", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "where's", "whereupon", "wherever", "whether", "which", "whichever", "while", "whilst", "whither", "who", "who'd", "whoever", "whole", "who'll", "whom", "whomever", "who's", "whose", "why", "will", "willing", "wish", "with", "within", "without", "wonder", "won't", "would", "wouldn't", "x", "y", "yes", "yet", "you", "you'd", "you'll", "your", "you're", "yours", "yourself", "yourselves", "you've", "z", "zero");
   }

When adding the additional code below to your functions.php file it will remove/hide the slug option for the screen options dropdown as well as the metabox.

// HIDE THE SLUG METABOX AND SLUG SCREEN OPTIONS
   function hide_slug_options() {
 global $post;
 global $pagenow;
 $hide_slugs = "<style type=\"text/css\">#slugdiv, #edit-slug-box, [for=\"slugdiv-hide\"] { display: none; }</style>\n";
 if (is_admin() && $pagenow=='post-new.php' OR $pagenow=='post.php') print($hide_slugs);
   }
   add_action( 'admin_head', 'hide_slug_options'  );
share|improve this answer
add comment

Remove Admin (User #1) from User list

function your_pre_user_query($user_search) {
  $user = wp_get_current_user();
  if ($user->ID!=1) {
    global $wpdb;
    $user_search->query_where = str_replace('WHERE 1=1',
      "WHERE 1=1 AND {$wpdb->users}.ID<>1",$user_search->query_where);
  }
}
add_action('pre_user_query','your_pre_user_query');
share|improve this answer
show 5 more comments

Remove Admin Backend Menus for all users, except User #1 (usually the first Admin)

/*-----------------------------------------------------------------------------------*/
/*  Restrict access
/*-----------------------------------------------------------------------------------*/
function remove_menus () {
global $menu;
$user = wp_get_current_user();
    if ($user->ID!=1) { // Is not administrator,

        $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');
share|improve this answer
1  
If you look at the linked plugin, then it allows you to set access groups per "whatever a user offers (login name, role, ID)". Relying on IDs generally is a "not best practice" concept. That's all i wanted to point out. –  kaiser Jun 13 '11 at 14:32
show 9 more comments

Easy WordPress Security Fixes

Security through obscurity is the name of the game here. These functions do three different things.

  1. Remove the version string from the code. No point in telling folks what version we're running.
  2. Removes any error messages (Wrong Password, No Such User, etc.) from admin login screens
  3. When the admin posts a comment, a CSS class is added. This removes the admin name in comments.

    //REMOVE VERSION STRING FROM HEADER
    remove_action('wp_head', 'wp_generator');
    
    //HIDE LOGIN ERROR MESSAGES (Wrong Password, No Such User etc.)
    add_filter('login_errors',create_function('$a', "return null;"));
    
    // Remove admin name in comments class
    // Source: http://www.wprecipes.com/wordpress-hack-remove-admin-name-in-comments-class
    function remove_comment_author_class( $classes ) {
        foreach( $classes as $key => $class ) {
            if(strstr($class, "comment-author-")) {
                unset( $classes[$key] );
            }
        }
        return $classes;
    }
    add_filter( 'comment_class' , 'remove_comment_author_class' );
    
share|improve this answer
show 1 more comment

Display the users that have submitted the most comments without a plugin

function top_comment_authors($amount = 5) {
global $wpdb;
$results = $wpdb->get_results('
    SELECT
    COUNT(comment_author_email) AS comments_count, comment_author_email, comment_author, comment_author_url
    FROM '.$wpdb->comments.'
    WHERE comment_author_email != "" AND comment_type = "" AND comment_approved = 1
    GROUP BY comment_author_email
    ORDER BY comments_count DESC, comment_author ASC
    LIMIT '.$amount
);
$output = "<ul>";
foreach($results as $result) {
    $output .= "<li>".$result->comment_author."</li>";
}
$output .= "</ul>";
echo $output;
}

Other options you can call: $result->comment_author_email $result->comments_count $result->comment_author_url

share|improve this answer
add comment

Automatically create a new page upon activating a theme

if (isset($_GET['activated']) && is_admin()){
    $new_page_title = 'This is the page title';
    $new_page_content = 'This is the page content';
    $new_page_template = ''; //ex. template-custom.php. Leave blank if you don't want a custom page template.

//don't edit under this line
$page_check = get_page_by_title($new_page_title);
$new_page = array(
    'post_type' => 'page',
    'post_title' => $new_page_title,
    'post_content' => $new_page_content,
    'post_status' => 'publish',
    'post_author' => 1,
);
if(!isset($page_check->ID)){
    $new_page_id = wp_insert_post($new_page);
    if(!empty($new_page_template)){
        update_post_meta($new_page_id,'_wp_page_template', $new_page_template);
    }
}
share|improve this answer
add comment

Here are some nice shortcodes for you to use:


1. Easy to add twitter & facebook share button Shortcode

function shreplz() {
   return '
    <div class="sharebox">
    <div class="twittme"><a href="https://twitter.com/share" class="twitter-share-button" data-count="horizontal">Tweet</a><script type="text/javascript" src="//platform.twitter.com/widgets.js"></script></div>
    <div class="shareface"><a name="fb_share"></a> <script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script></div>
    <br style="clear: left;" />
    </div>
   ';
}
add_shortcode('sharethis', 'shreplz');
// how to use: [sharethis]

2. Easy Remote site snapshot using Wordpress API shortcode

function wpr_snap($atts, $content = null) {
        extract(shortcode_atts(array(
            "snap" => 'http://s.wordpress.com/mshots/v1/',
            "url" => 'http://www.sagive.co.il',
            "alt" => 'My image',
            "w" => '400', // width
            "h" => '300' // height
        ), $atts));

    $img = '<img src="' . $snap . '' . urlencode($url) . '?w=' . $w . '&h=' . $h . '" alt="' . $alt . '"/>';
        return $img;
}

add_shortcode("snap", "wpr_snap");
//how to use: [snap url="http://www.example.com" alt="Cool Site!" w="300px" h="200px"]

3. Easy to use & embed iFrame Shortcode

function GenerateIframe( $atts ) {
    extract( shortcode_atts( array(
        'href' => 'http://the-url',
        'height' => '550px',
        'width' => '600px',     
    ), $atts ) );

    return '<iframe src="'.$href.'" width="'.$width.'" height="'.$height.'"> <p>Your Browser does not support Iframes.</p></iframe>';
}
add_shortcode('iframe', 'GenerateIframe');
// how to use: [iframe href="http://www.exmaple.com" height="480" width="640"]

4. Easy to include remote file / doc with Shortcode

function getfile_content( $atts ) {
  extract( shortcode_atts( array(
    'fileurl' => ''
  ), $atts ) );

  if ($fileurl!='')
    return @file_get_contents($fileurl);
}

add_shortcode( 'getfile', 'getfile_content' );
// how to use: [getfile fileurl="http://www.exmaple.com/somepage.html"]

.
Here are some Comments Related Snippets:

1. close the ability to comment globaly

function closeCommentsGlobaly($data) { return false; }
add_filter('comments_number', 'closeCommentsGlobaly');
add_filter('comments_open', 'closeCommentsGlobaly');

2. Give admin a different Css Class for he's comments

if (1 == $comment->user_id)
echo 'siteadmin'; // pick your class here

3. a really cool rich with data list of comments - gr8 for custom locked page

$comments = get_comments( array(
    'number'    => 10, // how many comments
    'status'    => 'approve' // type of comments
) );

foreach($comments as $eachComment){

// collect the data and assign it
$commentID = comment_ID;
$commentAuthorEmail = $eachComment->comment_author_email;
$commentPostId = $eachComment->comment_post_ID;
$commentPostTitle = get_the_title( $commentPostId );
$commentPostUrl = get_permalink( $commentPostId );
$comment_sidebarnumber = get_comments_number( $commentPostId );

global $wpdb;
$userCommentCount = $wpdb->get_var('SELECT COUNT('.$commentID.') FROM ' . $wpdb->comments. ' WHERE comment_author_email = "' . $commentAuthorEmail . '"');

    echo    '<div style="border: 1px solid #ccc; padding: 10px;">';
    echo    '<ul style="margin: 0px;">';
    echo    '<li>Name: '. $eachComment->comment_author .'</li>';
    echo    '<li>Commented about: <a href="'.$commentPostUrl.'">'. $commentPostTitle .'</a></li>';
    echo    '<li>Commented On: '. $eachComment->comment_date .'</li>';
    echo    '<li>Commneter Site: '. $eachComment->comment_author_email .'</</li>';
    echo    '<li>Commenter Email: '. $eachComment->comment_author_email .'</</li>';
    echo    '<li>This Commenter'. $eachComment->comment_author .' Commented '. $userCommentCount .' on your site</</li>';
    echo    '</ul>';
    echo    '<p style="padding: 10px;"><strong>'. $eachComment->comment_author .' wrote</strong>: '. $eachComment->comment_content .'</p>';
    echo    '</div>';

}
share|improve this answer
add comment

Displaying information for logged in users

if ( is_user_logged_in() ) {

}

is not working in functions.php file. You can use this code:

if ( !function_exists('is_user_logged_in') ) :

 function is_user_logged_in() {
$user = wp_get_current_user();

if ( $user->id == 0 ){
// This section if user is not logged in
} else {
// This section if user is logged in
}
}
endif;
share|improve this answer
2  
I just tested it and is_user_logged_in() seems to work just fine in functions.php?.. Any background info on why it's not supposed to? –  Rarst Jan 26 '11 at 13:48
show 1 more comment

Automatically adding header images from directory location

Within the default them which comes with wordpress you will notice an additional theme menu which gets activated that lets you select a header image to be utilized. Within the default theme code these images are hardcoded into the functions.php file. The code below allows wordpress to automatically pick up new images based upon a specific header image directly you can create on your server (or within your themes folder).

It will automatically include any .jpg or .jpeg files. Every image must have a associating thumbnail file but this can just be a copy of the original with a different name with a file name that has to end in "-thumbnail". The associating name is used as the description in the headers appearance settings and underscores are automatically replaced with spaces. (e.g. My_Header_Image_A.jpg, My_Header_Image_A=thumbnail.jpg will have a description automatically presented a “My Header Image A”)

if ($handle = opendir( TEMPLATEPATH . '/images/headers/') ) {
    $headers = array();
    while (false !== ($file = readdir($handle))) {
        $pos = strrpos( $file, '.' );
        if( $pos !== false && $pos > 0 ) {
            $file_name = substr( $file, 0, $pos );
            if( strpos( $file_name, "-thumbnail" ) === false ) {
                $file_ext = substr( $file, $pos+1 );
                $file_ext_low = strtolower( $file_ext );
                if( $file_ext_low == "jpg" || $file_ext_low == "jpeg" ) {
                    $headers[$file_name] = array (
                    'url' => '%s/images/headers/' . $file,
                    'thumbnail_url' => '%s/images/headers/' . $file_name ."-thumbnail." . $file_ext,
                    'description' => __( str_replace( "_", " ", $file_name ), 'twentyten' )
                    );
                }
            }
        }
    }
closedir($handle);
register_default_headers( $headers );
}
share|improve this answer
add comment

Get the Users Real IP Address whenever possible

Tested on: Wordpress 3.0.1

If you're using a proxy or load balancer, adding this to your wp-config.php file or functions.php

// Gets the user's real IP address

$_SERVER['REMOTE_ADDR'] = getRealIpAddress();
function getRealIpAddress( $validate = true ) {
    if ( isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ) {
        $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $ip = trim($ips[count($ips) - 1]);
    } elseif ( isset($_SERVER['HTTP_X_REAL_IP']) && !empty($_SERVER['HTTP_X_REAL_IP']) ) {
        $ip = $_SERVER['HTTP_X_REAL_IP'];
    } elseif ( isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP']) ) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }

    if ( $validate && function_exists('filter_var') &&  filter_var($ip, FILTER_VALIDATE_IP, array('flags' => FILTER_FLAG_IPV4, FILTER_FLAG_NO_PRIV_RANGE, FILTER_FLAG_NO_RES_RANGE)) )
        return $ip;
    elseif ( $validate )
        return long2ip(ip2long($ip));

    return $ip;
}
share|improve this answer
1  
what does this do exactly? –  NetConstructor.com Sep 14 '10 at 2:21
show 2 more comments

List all SubCategories

Tested on: Wordpress 3.0.1

$echo = '<ul>' . "\n";
$childcats = get_categories('child_of=' . $cat . '&hide_empty=1');
foreach ($childcats as $childcat) {
    if (1 == $childcat->category_parent) {
        $echo .= "\t" . '<li><a href="' . get_category_link($childcat->cat_ID).'" title="' . $childcat->category_description . '">';
        $echo .= $childcat->cat_name . '</a>';
        $echo .= '</li>' . "\n";
    }
}
$echo .= '</ul>' . "\n";
echo $echo;

also here, more informations and functins in the post http://wpengineer.com/2025/list-all-subcategories/

share|improve this answer
1  
Why would you do this when there's a function available specifically for displaying a list of categories..See: codex.wordpress.org/Template_Tags/… –  t31os Nov 11 '10 at 17:06
add comment

Disable browser upgrade warning in wordpress 3.2

enter image description here

//Disable browser upgrade warning in wordpress 3.2
function disable_browser_upgrade_warning() {
    remove_meta_box( 'dashboard_browser_nag', 'dashboard', 'normal' );
}
add_action( 'wp_dashboard_setup', 'disable_browser_upgrade_warning' );
share|improve this answer
3  
Not after switching themes. Themes are for frontends. –  toscho Aug 19 '11 at 17:29
show 2 more comments

Get Attributes of Given Thumbnail

Use this function with the loop to determine width, height and URL of a thumbnailed image. Very handy for assigning a thumbnailed image as a background element via inline CSS.

/**
* GET THUMBNAIL ATTRIBUTES
*
* Fetches width, heigth and URI of a thumbnail.
*
* @author Philip Downer <philip@manifestbozeman.com>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version v1.0
*
* @param string $return Accepts 'path', 'width', or 'height'.
* @param string $size The thumbnail size corresponding to {@link add_image_size() WP core function}.
* @return mixed Returns the requested info, or if no 'Featured Image' assigned, returns 'false'.
*/
function get_thumb_attr($return,$size='thumbnail') {
    global $post;

    if (has_post_thumbnail($post->ID)) {
      $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'intro');
      if ( $return == 'path' ) { return $thumb[0]; }
      if ( $return == 'width' ) { return $thumb[1]; }
      if ( $return == 'height' ) { return $thumb[2]; }
    } else {
        return false;
    }
}//end function
share|improve this answer
add comment

Show template files being included

Display inline comment with Template File and get_template_part files being included when rendering pages. Handy for troubleshooting multipart templates.

add_action('all','template_snoop');
function template_snoop(){
    $args = func_get_args();
    if( !is_admin() and $args[0] ){
        if( $args[0] == 'template_include' ) {
            echo "<!-- Base Template: {$args[1]} -->\n";
        } elseif( strpos($args[0],'get_template_part_') === 0 ) {
            global $last_template_snoop;
            if( $last_template_snoop )
                echo "\n\n<!-- End Template Part: {$last_template_snoop} -->";
            $tpl = rtrim(join('-',  array_slice($args,1)),'-').'.php';
            echo "\n<!-- Template Part: {$tpl} -->\n\n";
            $last_template_snoop = $tpl;
        }
    }
}
share|improve this answer
add comment

Output the contents of a widget outside the context of a sidebar using it's ID. The wrapping before/after HTML is not included. You need to know the specific ID of the widget you're angling for (ie 'text-5').

function widget_contents($id) {
    list($type,$number) = explode('-',$id);
    global $wp_registered_widgets;
    $wp_registered_widgets[$id]['callback'][0]->display_callback(array('widget_id'=>$id),$number);
}

You can peek at the output of wp_get_sidebars_widgets() if you aren't sure of the precise ID you need.

A more complete example lifted from /wp-includes/widgets.php under the dynamic_sidebar() function:

function render_widget($id) {
    global $wp_registered_widgets;
    $params = array_merge(
            array( array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ),
            (array) $wp_registered_widgets[$id]['params']
    );
    $classname_ = '';
    foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
            if ( is_string($cn) )
                    $classname_ .= '_' . $cn;
            elseif ( is_object($cn) )
                    $classname_ .= '_' . get_class($cn);
    }
    $classname_ = ltrim($classname_, '_');
    $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);

    if ( is_callable($wp_registered_widgets[$id]['callback']) )
            call_user_func_array($wp_registered_widgets[$id]['callback'], $params);
}
share|improve this answer
show 1 more comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.