Loading All Posts From The Current Week
The following snippet will allow you to load all the posts from the current week.
1 2 3 4 5 6 7 8 9 |
$query_args = array( 'w' => date( 'W' ), 'year' => date( 'Y' ), ); $posts = get_posts( $query_args ); |
Source: MarketPress – Christian Brückner
Breadcrumb Without Plugin
While there are numbers of plugins available to breadcrumb your WordPress blog or website BUT what if you can do the same with simply adding the following snippet at your theme’s functions.php file.
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 |
function the_breadcrumb() { echo '<ul id="crumbs">'; if (!is_home()) { echo '<li><a href="'; echo get_option('home'); echo '">'; echo 'Home'; echo "</a></li>"; if (is_category() || is_single()) { echo '<li>'; the_category(' </li><li> '); if (is_single()) { echo "</li><li>"; the_title(); echo '</li>'; } } elseif (is_page()) { echo '<li>'; echo the_title(); echo '</li>'; } } elseif (is_tag()) {single_tag_title();} elseif (is_day()) {echo"<li>Archive for "; the_time('F jS, Y'); echo'</li>';} elseif (is_month()) {echo"<li>Archive for "; the_time('F, Y'); echo'</li>';} elseif (is_year()) {echo"<li>Archive for "; the_time('Y'); echo'</li>';} elseif (is_author()) {echo"<li>Author Archive"; echo'</li>';} elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "<li>Blog Archives"; echo'</li>';} elseif (is_search()) {echo"<li>Search Results"; echo'</li>';} echo '</ul>'; } |
Once done, open header.php and call the function:
1 |
<?php the_breadcrumb(); ?> |
Source: emoticode.net
Changing Dashboard Help Tab Contents
Using the following snippet, you will be able to change the contents of your dashboard help tab.
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 |
//hook loading of new page and edit page screens add_action('load-page-new.php','add_custom_help_page'); add_action('load-page.php','add_custom_help_page'); function add_custom_help_page() { //the contextual help filter add_filter('contextual_help','custom_page_help'); } function custom_page_help($help) { //keep the existing help copy echo $help; //add some new copy echo "<h5>Custom Features</h5>"; echo "<p>Content placed above the more divider will appear in column 1. Content placed below the divider will appear in column 2.</p>"; } |
Source: WP Tuts
Automatically Adding Paragraph Tags
While using the wpautop(), WordPress itself add paragraph tags to the content and the excerpt BUT if in case you requires to add paragraph tags to any text automatically, then use the following snippet for the purpose.
1 2 3 4 5 6 7 |
$my_text = 'Lorem ipsum dolor sit amet consectetur adipiscing elit. Nulla pretium libero eget gravida rutrum.'; echo wpautop( $my_text ); |
Source: Daniel Pataki
How To Login In A WordPress User Programmatically
The following snippet will allow you to login in a WordPress user programmatically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function auto_login( $user ) { $username = $user; if ( !is_user_logged_in() ) { $user = get_userdatabylogin( $username ); $user_id = $user->ID; wp_set_current_user( $user_id, $user_login ); wp_set_auth_cookie( $user_id ); do_action( 'wp_login', $user_login ); } } |
Followed by, to log in a user, do the following:
1 |
auto_login( 'admin' ) |
Source: evilsocket
Bringing Single-Column Dashboard Back To WordPress 3.8
With recently introduced WordPress 3.8, a whole new way to display the dashboard BUT if in case, you don’t like the way, then adding the following snippet to your theme’s functions.php will let you bring back single-column dashboard.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// force one-column dashboard function shapeSpace_screen_layout_columns($columns) { $columns['dashboard'] = 1; return $columns; } add_filter('screen_layout_columns', 'shapeSpace_screen_layout_columns'); function shapeSpace_screen_layout_dashboard() { return 1; } add_filter('get_user_option_screen_layout_dashboard', 'shapeSpace_screen_layout_dashboard'); |
Source: Jeff Starr
Ordering Posts By Two Meta Values
By default, WordPress allows you to sort results of a query by one meta value, BUT if you want to order posts by two meta values, then adding the following snippet will help.
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 |
<?php $query = "SELECT wposts.*, wpostmeta1.*, wpostmeta2.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta1, $wpdb->postmeta wpostmeta2 WHERE wposts.ID = wpostmeta1.post_id AND wposts.ID = wpostmeta2.post_id AND wpostmeta1.meta_key = 'date' AND wpostmeta2.meta_key = 'time' ORDER BY wpostmeta1.meta_value ASC, wpostmeta2.meta_value ASC"; $results = $wpdb->get_results($query); foreach ( $results as $result ) { //output results as desired } ?> |
Source: WP Recipes
Displaying Recently Registered Users
The following snippet will display recently registered users.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<ul> <?php $usernames = $wpdb->get_results("SELECT user_nicename, user_url FROM $wpdb->users ORDER BY ID DESC LIMIT 15"); foreach ($usernames as $username) { echo '<li>' .get_avatar($username->comment_author_email, 45).'<a href="'.$username->user_url.'">'.$username->user_nicename."</a></li>"; } ?> </ul> |
Source: emoticode.net