Customize Default Avatar
1 2 3 4 5 6 7 8 9 10 11 12 13 |
The following snippet will allow you to customize default avatar function newgravatar ($avatar_defaults) { $myavatar = get_bloginfo('template_directory') . '/images/tweaker.jpg'; $avatar_defaults[$myavatar] = "Tweaker"; return $avatar_defaults; } add_filter( 'avatar_defaults', 'newgravatar' ); |
Display Most Popular Posts from a Specific Category
The following snippet will display most popular posts from a specific category
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php $args=array( 'cat' => 3, 'orderby' => 'comment_count', 'order' => 'DESC', 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 6, 'caller_get_posts'=> 1 ); $my_query = null; $my_query = new WP_Query($args); if( $my_query->have_posts() ) { ?> <ul> <?php while ($my_query->have_posts()) : $my_query->the_post(); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php endwhile; ?> </ul> <?php } wp_reset_query(); ?> |
Source: Snipplr
WordPress Theme Options Page
Using the snippet will creates a theme options page for your WordPress theme
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
<?php /* This should be saved as theme-options.php and * you'll need to use the following in your functions.php: * require_once ( get_stylesheet_directory() . '/theme-options.php' ); */ add_action( 'admin_init', 'theme_options_init' ); add_action( 'admin_menu', 'theme_options_add_page' ); /** * Init plugin options to white list our options */ function theme_options_init(){ register_setting( 'sample_options', 'sample_theme_options', 'theme_options_validate' ); } /** * Load up the menu page */ function theme_options_add_page() { add_theme_page( __( 'Theme Options', 'sampletheme' ), __( 'Theme Options', 'sampletheme' ), 'edit_theme_options', 'theme_options', 'theme_options_do_page' ); } /** * Create arrays for our select and radio options */ $select_options = array( '0' => array( 'value' => '0', 'label' => __( 'Zero', 'sampletheme' ) ), '1' => array( 'value' => '1', 'label' => __( 'One', 'sampletheme' ) ), '2' => array( 'value' => '2', 'label' => __( 'Two', 'sampletheme' ) ), '3' => array( 'value' => '3', 'label' => __( 'Three', 'sampletheme' ) ), '4' => array( 'value' => '4', 'label' => __( 'Four', 'sampletheme' ) ), '5' => array( 'value' => '3', 'label' => __( 'Five', 'sampletheme' ) ) ); $radio_options = array( 'yes' => array( 'value' => 'yes', 'label' => __( 'Yes', 'sampletheme' ) ), 'no' => array( 'value' => 'no', 'label' => __( 'No', 'sampletheme' ) ), 'maybe' => array( 'value' => 'maybe', 'label' => __( 'Maybe', 'sampletheme' ) ) ); /** * Create the options page */ function theme_options_do_page() { global $select_options, $radio_options; if ( ! isset( $_REQUEST['settings-updated'] ) ) $_REQUEST['settings-updated'] = false; ?> <div class="wrap"> <?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme Options', 'sampletheme' ) . "</h2>"; ?> <?php if ( false !== $_REQUEST['settings-updated'] ) : ?> <div class="updated fade"><p><strong><?php _e( 'Options saved', 'sampletheme' ); ?></strong></p></div> <?php endif; ?> <form method="post" action="options.php"> <?php settings_fields( 'sample_options' ); ?> <?php $options = get_option( 'sample_theme_options' ); ?> <table class="form-table"> <?php /** * A sample checkbox option */ ?> <tr valign="top"><th scope="row"><?php _e( 'A checkbox', 'sampletheme' ); ?></th> <td> <input id="sample_theme_options[option1]" name="sample_theme_options[option1]" type="checkbox" value="1" <?php checked( '1', $options['option1'] ); ?> /> <label class="description" for="sample_theme_options[option1]"><?php _e( 'Sample checkbox', 'sampletheme' ); ?></label> </td> </tr> <?php /** * A sample text input option */ ?> <tr valign="top"><th scope="row"><?php _e( 'Some text', 'sampletheme' ); ?></th> <td> <input id="sample_theme_options[sometext]" type="text" name="sample_theme_options[sometext]" value="<?php esc_attr_e( $options['sometext'] ); ?>" /> <label class="description" for="sample_theme_options[sometext]"><?php _e( 'Sample text input', 'sampletheme' ); ?></label> </td> </tr> <?php /** * A sample select input option */ ?> <tr valign="top"><th scope="row"><?php _e( 'Select input', 'sampletheme' ); ?></th> <td> <select name="sample_theme_options[selectinput]"> <?php $selected = $options['selectinput']; $p = ''; $r = ''; foreach ( $select_options as $option ) { $label = $option['label']; if ( $selected == $option['value'] ) // Make default first in list $p = "\n\t<option style=\"padding-right: 10px;\" selected='selected' value='" . esc_attr( $option['value'] ) . "'>$label</option>"; else $r .= "\n\t<option style=\"padding-right: 10px;\" value='" . esc_attr( $option['value'] ) . "'>$label</option>"; } echo $p . $r; ?> </select> <label class="description" for="sample_theme_options[selectinput]"><?php _e( 'Sample select input', 'sampletheme' ); ?></label> </td> </tr> <?php /** * A sample of radio buttons */ ?> <tr valign="top"><th scope="row"><?php _e( 'Radio buttons', 'sampletheme' ); ?></th> <td> <fieldset><legend class="screen-reader-text"><span><?php _e( 'Radio buttons', 'sampletheme' ); ?></span></legend> <?php if ( ! isset( $checked ) ) $checked = ''; foreach ( $radio_options as $option ) { $radio_setting = $options['radioinput']; if ( '' != $radio_setting ) { if ( $options['radioinput'] == $option['value'] ) { $checked = "checked=\"checked\""; } else { $checked = ''; } } ?> <label class="description"><input type="radio" name="sample_theme_options[radioinput]" value="<?php esc_attr_e( $option['value'] ); ?>" <?php echo $checked; ?> /> <?php echo $option['label']; ?></label><br /> <?php } ?> </fieldset> </td> </tr> <?php /** * A sample textarea option */ ?> <tr valign="top"><th scope="row"><?php _e( 'A textbox', 'sampletheme' ); ?></th> <td> <textarea id="sample_theme_options[sometextarea]" cols="50" rows="10" name="sample_theme_options[sometextarea]"><?php echo esc_textarea( $options['sometextarea'] ); ?></textarea> <label class="description" for="sample_theme_options[sometextarea]"><?php _e( 'Sample text box', 'sampletheme' ); ?></label> </td> </tr> </table> <p class="submit"> <input type="submit" value="<?php _e( 'Save Options', 'sampletheme' ); ?>" /> </p> </form> </div> <?php } /** * Sanitize and validate input. Accepts an array, return a sanitized array. */ function theme_options_validate( $input ) { global $select_options, $radio_options; // Our checkbox value is either 0 or 1 if ( ! isset( $input['option1'] ) ) $input['option1'] = null; $input['option1'] = ( $input['option1'] == 1 ? 1 : 0 ); // Say our text option must be safe text with no HTML tags $input['sometext'] = wp_filter_nohtml_kses( $input['sometext'] ); // Our select option must actually be in our array of select options if ( ! array_key_exists( $input['selectinput'], $select_options ) ) $input['selectinput'] = null; // Our radio option must actually be in our array of radio options if ( ! isset( $input['radioinput'] ) ) $input['radioinput'] = null; if ( ! array_key_exists( $input['radioinput'], $radio_options ) ) $input['radioinput'] = null; // Say our textarea option must be safe text with the allowed tags for posts $input['sometextarea'] = wp_filter_post_kses( $input['sometextarea'] ); return $input; } |
Source: Themeshaper
Display the number of comments by author
The following snippet will display the number of comments by author
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
function author_comment_count(){ $oneText = '1'; $moreText = '%'; global $wpdb; $result = $wpdb->get_var(' SELECT COUNT(comment_ID) FROM '.$wpdb->comments.' WHERE comment_author_email = "'.get_comment_author_email().'"' ); if($result == 1): echo str_replace('%', $result, $oneText); elseif($result > 1): echo str_replace('%', $result, $moreText); endif; } |
Source: Snipplr
Display your latest Google+ WordPress blog
The following snippet will display your latest Google+ WordPress blog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php include_once(ABSPATH.WPINC.'/rss.php'); $googleplus = fetch_feed("http://plusfeed.appspot.com/103329092193061943712"); // Replace 103329092193061943712 by your own ID echo '<a href="'; echo $googleplus->items[0]['link']; echo '">'; echo $googleplus->items[0]['summary']; echo ''; ?> |
Get Posts By Term and Sort Them
The following snippet will get posts by term and even sort them
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
<?php $args = array( 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC' ); $posts = array(); foreach( get_posts($args) as $post ): $terms = array(); foreach(wp_get_object_terms( $post->ID, array('taxonomy1', 'taxonomy2') ) as $term) { //Works if there is only one term tagged in the post for this taxonomy. You need to figure out how best to handle multiple terms. You might try imploding() them in a comma seperated list: "Term 1, Term 2" $post[$term->taxonomy] = $term->name; } $posts[] = (object) $post; endforeach; /** * Sort array of objects by field. * * @param array $objects Array of objects to sort. * @param string $on Name of field. * @param string $order (ASC|DESC) **/ function sort_on_field(&$objects, $on, $order = 'ASC') { $comparer = ($order === 'DESC') ? "return -strcasecmp(\$a->{$on},\$b->{$on});" : "return strcasecmp(\$a->{$on},\$b->{$on});"; usort($objects, create_function('$a,$b', $comparer)); } /* Use `strcmp` for case-sensitive string comparing. Use `strcasecmp` for case-insensitive string comparing. Source: http://www.php.net/manual/de/function.usort.php#104873 */ sort_on_field($posts, 'taxonomy1', 'DESC'); foreach( $posts as $post ) { echo $post->taxonomy1; } ?> |
Source: Snipplr
Add your own Logo to WordPress Dashboard
Adding the following snippet to your theme’s function.php file will let you add your own logo to WordPress dashboard
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
add_action('admin_head', 'my_custom_logo'); function my_custom_logo() { echo ' <style type="text/css"> #header-logo { background-image: url('.get_bloginfo('template_directory').'/images/your-logo.jpg) !important; } </style> '; } |
Cached Twitter Follower Count
The following snippet will cached Twitter follower count
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
function get_follower_Count(){ $count = get_transient('follower_count'); if ($count !== false) return $count; $count = 0; $data = wp_remote_get('http://api.twitter.com/1/users/show.json?screen_name=YOURNAME'); if (!is_wp_error($data)) { $value = json_decode($data['body'],true); $count = $value['followers_count']; } set_transient('follower_count', $count, 60*60); // 1 hour cache return $count; } |
Add the first section of code to the functions.php of your wordpress theme. This code will cache your Twitter follower number for 1 hour and display the count within your template.
1 2 3 4 5 |
<? get_follower_count(); ?> |
Source: Snipplr
Photo Credit: Flickr/Travis Swicegood