Code & Me
WordPress Code Snippet Repository
  • Home
  • Featured
  • Recently Added
  • About
  • Contact

Adding Paragraph Tags Automatically In Your WordPress Blog

March 20, 2014 By Editorial Staff in Featured, WordPress No Comments Tags: Paragraph Tag, Paragraph Tags, Snippet, WordPress, WordPress Blog, WordPress Snippet

Using the wpautop() function, WordPress itself add paragraph tag to the content as well as the excerpt BUT if you wish to make an addition to this by adding paragraph tags to any text, simply add the following snippet and you will be able to add paragraph tags automatically in your WordPress blog.

1
2
3
4
$my_text = 'Lorem ipsum dolor sit amet
consectetur adipiscing elit.
Nulla pretium libero eget gravida rutrum.';
echo wpautop( $my_text );

Snippet Source/Credit: Daniel Pataki

Setting The Default Image Size In WordPress Galleries

March 19, 2014 By Editorial Staff in WordPress No Comments Tags: Shortcode, Snippet, WordPress Shortcode, WordPress Snippets

The WordPress built-in galleries by default use the “full” as a size for the images but in some cases condition arise that you want to set up a different size. Well, this can be done by simply using the following snippet which you have to paste in your theme’s functions.php file with which you can set the default image size in your built-in WordPress galleries.

1
2
3
4
5
6
remove_shortcode('gallery');
add_shortcode('gallery', 'custom_size_gallery');
function custom_size_gallery($attr) {
     $attr['size'] = 'medium';
     return gallery_shortcode($attr);
}

Snippet Source/Credit: emoticode

How To Add A Breadcrumb To Your Blog

By Editorial Staff in Featured, WordPress No Comments Tags: Shortcode, Snippet, WordPress Shortcode, WordPress Snippets

Thanks to the one-to-many plugins which are available over the web for adding a breadcrumb to your blog, BUT, what’s more if you can achieve the task by simply adding the following snippet. Simply paste the following code to your functions.php file and you are done.

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
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, call the following function by opening header.php:

1
<?php the_breadcrumb(); ?>

Snippet Source/Credit: emoticode

Changing Contents Of A Dashboard Tab

By Editorial Staff in WordPress No Comments Tags: Shortcode, Snippet, WordPress Shortcode, WordPress Snippets

If you noticed clearly that in your WordPress dashboard, there is a little tab in the top-right corner which when clicked, drop down and show contextual help. Well, you will be delighted to know that you can hook different help text for different pages. Just, simply paste the snippet below into your functions.php file and you will be able to change the contents of your dashboard tab.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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>";
}

Snippet Source/Credit: WP Tuts

How To Show A Short Intro Of The Author Bio

March 18, 2014 By Editorial Staff in WordPress No Comments Tags: Shortcode, Snippet, WordPress Shortcode, WordPress Snippets

There are one-to-many times when you wish to show a short introduction of the author bio instead of the complete author byline. Well, in order to achieve this, all you have to do is to simply copy and paste the following snippet into your theme’s functions.php file.

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
function author_excerpt (){                     
$word_limit = 20; // Limit the number of words
$more_txt = 'read more about:'; // The read more text
$txt_end = '...'; // Display text end
$authorName = get_the_author();
$authorUrl = get_author_posts_url( get_the_author_meta('ID'));
$authorDescriptionShort = wp_trim_words(strip_tags(get_the_author_meta('description')), $word_limit, $txt_end.'<br /> '.$more_txt.' <a href="'.$authorUrl.'">'.$authorName.'</a>');
return $authorDescriptionShort;
}
?>
Remember a thing more, once added the above snippet, add the following line of code at the place where you want to display a short introduction of the author byline:
<?php  if (function_exists('author_excerpt')){echo author_excerpt();} ?>

Snippet Source/Credit: Tim Marcher

Bringing Back Single-Column Dashboard In WordPress 3.8

By Editorial Staff in Featured, WordPress No Comments Tags: Shortcode, Snippet, WordPress Shortcode, WordPress Snippets

With recent WordPress 3.8 version, a whole new way of dashboard make its way BUT if you, just like many others, don’t like it, then not to worry as with the following snippet you will be able to bring back the single-column dashboard. Simply add the code to your theme’s functions.php file and you will be able to bring back single-column dashboard in WordPress 3.8.

1
2
3
4
5
6
7
8
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');

Snippet Source/Credit: Jeff Starr

Adding Custom Text To Your Website’s Login Page

By Editorial Staff in WordPress No Comments Tags: Shortcode, Snippet, WordPress Shortcode, WordPress Snippets

For one-to-many reasons, you wish to display a custom message on your WordPress website’s login page. Well, doing this is not a difficult task at all as all you have to do is simply page the following snippet in your theme’s functions.php file while to customize the message, you can do on line 3.

1
2
3
4
5
6
7
8
function wps_login_message( $message ) {
    if ( empty($message) ){
        return "<p class='message'>Welcome to this site. Please log in to continue</p>";
    } else {
        return $message;
    }
}
add_filter( 'login_message', 'wps_login_message' );

Snippet Source/Credit: WP Snippy

How To Get Email Alerts For 404 Pages In Your Website

March 17, 2014 By Editorial Staff in Featured, WordPress No Comments Tags: Shortcode, Snippet, WordPress Shortcode, WordPress Snippets

You do wish to keep a track of 404 pages in your WordPress website. BUT what if instead of keeping a manual track of it by looking onto the log files, you will be getting an email alerts for 404 pages. Well this can be possible by simply including the following snippet at the top of your theme’s 404.php file and you can achieve this.

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
<?php // WP 404 ALERTS @ http://wp-mix.com/wordpress-404-email-alerts/
 
// set status
header("HTTP/1.1 404 Not Found");
header("Status: 404 Not Found");
 
// site info
$blog  = get_bloginfo('name');
$site  = get_bloginfo('url') . '/';
$email = get_bloginfo('admin_email');
 
// theme info
if (!empty($_COOKIE["nkthemeswitch" . COOKIEHASH])) {
$theme = clean($_COOKIE["nkthemeswitch" . COOKIEHASH]);
} else {
$theme_data = wp_get_theme();
$theme = clean($theme_data->Name);
}
 
// referrer
if (isset($_SERVER['HTTP_REFERER'])) {
$referer = clean($_SERVER['HTTP_REFERER']);
} else {
$referer = "undefined";
}
// request URI
if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER["HTTP_HOST"])) {
$request = clean('http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
} else {
$request = "undefined";
}
// query string
if (isset($_SERVER['QUERY_STRING'])) {
$string = clean($_SERVER['QUERY_STRING']);
} else {
$string = "undefined";
}
// IP address
if (isset($_SERVER['REMOTE_ADDR'])) {
$address = clean($_SERVER['REMOTE_ADDR']);
} else {
$address = "undefined";
}
// user agent
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$agent = clean($_SERVER['HTTP_USER_AGENT']);
} else {
$agent = "undefined";
}
// identity
if (isset($_SERVER['REMOTE_IDENT'])) {
$remote = clean($_SERVER['REMOTE_IDENT']);
} else {
$remote = "undefined";
}
// log time
$time = clean(date("F jS Y, h:ia", time()));
 
// sanitize
function clean($string) {
$string = rtrim($string);
$string = ltrim($string);
$string = htmlentities($string, ENT_QUOTES);
$string = str_replace("\n", "<br>", $string);
 
if (get_magic_quotes_gpc()) {
$string = stripslashes($string);
}
return $string;
}
 
$message =
"TIME: "            . $time    . "\n" .
"*404: "            . $request . "\n" .
"SITE: "            . $site    . "\n" .
"THEME: "           . $theme   . "\n" .
"REFERRER: "        . $referer . "\n" .
"QUERY STRING: "    . $string  . "\n" .
"REMOTE ADDRESS: "  . $address . "\n" .
"REMOTE IDENTITY: " . $remote  . "\n" .
"USER AGENT: "      . $agent   . "\n\n\n";
 
mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email");
 
?>

Snippet Source/Credit: WP Mix

How To Get The First Link In Post?

By Editorial Staff in WordPress No Comments Tags: Shortcode, Snippet, WordPress Shortcode, WordPress Snippet

Using the following snippet and pasting it in your theme’s functions.php file will let you get the first link in post.

1
2
3
4
5
6
function get_link_url() {
    $content = get_the_content();
    $has_url = get_url_in_content( $content );
 
    return ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
}

Snippet Source/Credit: Filip Stefansson

How To Display Recently Registered Users?

By Editorial Staff in WordPress No Comments Tags: Snippet, WordPress Shortcode, WordPress Snippets

Adding lucrative things in your website will add value to it for a longer time, then why not opt for it, if it is an easy task to achieve. Say, suppose you are running a huge website where you allow user registration, than why not display the last five or ten registered users. To display recently registered users, simply paste the following code below at the place where you want to display recent registered users.

1
2
3
4
5
6
7
<ul class="recently-user">
    <?php $usernames = $wpdb->get_results("SELECT user_nicename, user_url FROM $wpdb->users ORDER BY ID DESC LIMIT 5");
        foreach ($usernames as $username) {
                echo '<li>' .get_avatar($username->comment_author_email, 45).'<a href="'.$username->user_url.'">'.$username->user_nicename."</a></li>";
        }
    ?>
</ul>

Snippet Source/Credit: emoticode

«< 64 65 66 67 68

Recently Added Snippets

  • How To Set Role And Capabilities In WordPress
  • How To Set WordPress Options As Value
  • List Author Comments On Author Page
  • WordPress Display Page
  • Removing WordPress Version From Head And Feeds

Search The Snippet

Featured Snippets

  • How To Set WordPress Options As Value
  • WordPress Display Page
  • Removing WordPress Version From Head And Feeds
  • Delete Link In Comments In Your WordPress Website
  • Creating Database For Your WordPress Website
Code & Me
  • About
  • Recently Added
  • Advertise
  • Terms
  • Privacy Policy
  • Contact
© 2014-19 CodeAndMe.
Proud Member Of G2One Network Family.

↑ Back to top