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

How To Stop WordPress From Asking FTP Credentials

April 11, 2014 By Editorial Staff in WordPress No Comments Tags: Code Snippet, Credential, FTP, FTP Credential, FTP Credentials, Snippet, WordPress, WordPress Blog, WordPress Snippet

You might get noticed a thing that when you install, delete or even upgrade plugins or themes, WordPress might asks you the FTP credentials all the time, which nothing but makes you but. While, honestly speaking it does irritate me a lot, but thanks to the snippet below, you can change the file system method in your website, result of which WordPress won’t ask for your FTP login and password again. Copy and paste the following line of code in your wp-config.php file and you are done.

1
define('FS_METHOD', 'direct');

Snippet Source/Credit: WP Tuts+

Using Your Gravatar As A Favicon On Your WordPress Site

By Editorial Staff in WordPress No Comments Tags: Code Snippet, Favicon, Gravatar, Snippet, Website, WordPress, WordPress Blog, WordPress Site, WordPress Snippet

Some from us wish to use their gravatar as a favicon on their WordPress site. Although, the idea sounds good but in order to achieve this, a little work is required to accomplish at first stage. To do so, first edit the following function by adding your email address on line 3 and then paste it on your theme’s functions.php file.

1
2
3
4
5
function GravatarAsFavicon() {
//We need to establish the hashed value of your email address
$GetTheHash = md5(strtolower(trim('you@yourdomain.com')));
echo 'http://www.gravatar.com/avatar/' . $GetTheHash . '?s=16';
}

Now once done, paste the following files in your theme’s header.php file:

1
2
<link rel="shortcut icon" href="<?php GravatarAsFavicon(); ?>" />
<link rel="apple-touch-icon" href="<?php GravatarAsFavicon(); ?>">

Snippet Source/Credit: WP-Snippets.com

How To Search Within Post Type Only

April 9, 2014 By Editorial Staff in WordPress No Comments Tags: Blog, Code Snippet, Post, Post Type, Search, Snippet, WordPress, WordPress Blog, WordPress Snippet

If you want to force the search engine to search only within a specific post type, then all you have to do is simply copy and paste the following code into your theme’s functions.php file. Remember to change the post type name on line 4 and you are done.

1
2
3
4
5
6
7
8
9
10
function SearchFilter($query) {
  if ($query->is_search) {
    // Insert the specific post type you want to search
    $query->set('post_type', 'feeds');
  }
  return $query;
}
 
// This filter will jump into the loop and arrange our results before they're returned
add_filter('pre_get_posts','SearchFilter');

Snippet Source/Credit: Hongkiat

How To Protect Your Blog Against Image Hotlinking

By Editorial Staff in Featured, WordPress No Comments Tags: Blog, Code Snippet, Hotlinking, Image, Image Hotlinking, Snippet, WordPress, WordPress Blog, WordPress Snippet

You did notice that most people simply use your RSS feed and display it on their website, means they will be using your original images on their website while consuming your server bandwidth for their own purpose. It’s nothing but a kind of steal of your data. Well, using the following snippet you will be able to protect your blog against image hotlinking.

1
2
3
4
5
6
RewriteEngine On
#Replace ?mysite\.com/ with your blog url
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
#Replace /images/nohotlink.jpg with your "don't hotlink" image url
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]

Snippet Source/Credit: WPRecipies

How To Limit Search To Specific Categories

April 8, 2014 By Editorial Staff in WordPress No Comments Tags: Category, Code Snippet, Limit Search, Search, Snippet, WordPress, WordPress Search, WordPress Snippet

You do think of doing such kind of act by limiting the search to one or more specific categories. If your answer is yes and you really wish to do that, just simply replace the categories IDs below on line 3 and paste the following snippet on your theme’s search.php file and you will be able to limit search to specific categories.

1
2
3
4
<?php if( is_search() )  :
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("s=$s&paged=$paged&cat=1,2,3");
endif; ?>

Snippet Source/Credit: WPRecipies

How To Add A Google+ Button To Your Posts

By Editorial Staff in Featured, WordPress No Comments Tags: Code Snippet, Google, Google Plus, Google+ Button, Snippet, WordPress, WordPress Snippet

Simply paste the following snippet to your theme’s functions.php file and you will be able to add Google+ button to your posts.

1
2
3
4
5
6
7
8
9
add_filter('the_content', 'wpr_google_plusone');
function wpr_google_plusone($content) {
$content = $content.'<div class="plusone"><g:plusone size="tall" href="'.get_permalink().'"></g:plusone></div>';
return $content;
}
add_action ('wp_enqueue_scripts','wpr_google_plusone_script');
function wpr_google_plusone_script() {
wp_enqueue_script('google-plusone', 'https://apis.google.com/js/plusone.js', array(), null);
}

Snippet Source/Credit: SpyResStudios

How To Make Your WordPress Website SEO Friendly Without A Plugin

April 7, 2014 By Editorial Staff in Featured, WordPress No Comments Tags: Code Snippet, Plugin, Search Engine Optimization, SEO, SEO Friendly, Snippet, Website, WordPress, WordPress Snippet, WordPress Website

No doubt, with the plugins like WordPress SEO by Yoast and Ultimate SEO, you can make your website SEO friendly. BUT, what if there is a possibility that you can make your WordPress website SEO friendly without the use of any plugin. This can be achieve by simply pasting the code in your theme’s 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
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
function basic_wp_seo() {
global $page, $paged, $post;
$default_keywords = 'wordpress, plugins, themes, design, dev, development, security, htaccess, apache, php, sql, html, css, jquery, javascript, tutorials'; // customize
$output = '';
 
// description
$seo_desc = get_post_meta($post->ID, 'mm_seo_desc', true);
$description = get_bloginfo('description', 'display');
$pagedata = get_post($post->ID);
if (is_singular()) {
if (!empty($seo_desc)) {
$content = $seo_desc;
} else if (!empty($pagedata)) {
$content = apply_filters('the_excerpt_rss', $pagedata->post_content);
$content = substr(trim(strip_tags($content)), 0, 155);
$content = preg_replace('#\n#', ' ', $content);
$content = preg_replace('#\s{2,}#', ' ', $content);
$content = trim($content);
}
} else {
$content = $description;
}
$output .= '<meta name="description" content="' . esc_attr($content) . '">' . "\n";
 
// keywords
$keys = get_post_meta($post->ID, 'mm_seo_keywords', true);
$cats = get_the_category();
$tags = get_the_tags();
if (empty($keys)) {
if (!empty($cats)) foreach($cats as $cat) $keys .= $cat->name . ', ';
if (!empty($tags)) foreach($tags as $tag) $keys .= $tag->name . ', ';
$keys .= $default_keywords;
}
$output .= "\t\t" . '<meta name="keywords" content="' . esc_attr($keys) . '">' . "\n";
 
// robots
if (is_category() || is_tag()) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ($paged > 1) {
$output .=  "\t\t" . '<meta name="robots" content="noindex,follow">' . "\n";
} else {
$output .=  "\t\t" . '<meta name="robots" content="index,follow">' . "\n";
}
} else if (is_home() || is_singular()) {
$output .=  "\t\t" . '<meta name="robots" content="index,follow">' . "\n";
} else {
$output .= "\t\t" . '<meta name="robots" content="noindex,follow">' . "\n";
}
 
// title
$title_custom = get_post_meta($post->ID, 'mm_seo_title', true);
$url = ltrim(esc_url($_SERVER['REQUEST_URI']), '/');
$name = get_bloginfo('name', 'display');
$title = trim(wp_title('', false));
$cat = single_cat_title('', false);
$tag = single_tag_title('', false);
$search = get_search_query();
 
if (!empty($title_custom)) $title = $title_custom;
if ($paged >= 2 || $page >= 2) $page_number = ' | ' . sprintf('Page %s', max($paged, $page));
else $page_number = '';
 
if (is_home() || is_front_page()) $seo_title = $name . ' | ' . $description;
elseif (is_singular())            $seo_title = $title . ' | ' . $name;
elseif (is_tag())                 $seo_title = 'Tag Archive: ' . $tag . ' | ' . $name;
elseif (is_category())            $seo_title = 'Category Archive: ' . $cat . ' | ' . $name;
elseif (is_archive())             $seo_title = 'Archive: ' . $title . ' | ' . $name;
elseif (is_search())              $seo_title = 'Search: ' . $search . ' | ' . $name;
elseif (is_404())                 $seo_title = '404 - Not Found: ' . $url . ' | ' . $name;
else                              $seo_title = $name . ' | ' . $description;
 
$output .= "\t\t" . '<title>' . esc_attr($seo_title . $page_number) . '</title>' . "\n";
 
return $output;
}

Once done, simply replace the $default_keywords in line 3 with your own keywords and add the following line of code into your theme’s header.php file.

1
<?php echo basic_wp_seo(); ?>

Snippet Source/Credit: Jeff Starr

Context Menus With HTML5

By Editorial Staff in WordPress No Comments Tags: Code Snippet, Context, Context Menus, HTML5, Snippet, WordPress, WordPress Snippet

With the HTML5 in effect, context menus allow you to add element to the contextual menu. The menu which appears the time when the user right click somewhere on your page.  The following snippet below will give you an idea that what Context Menus with HTML5 actually is.

1
2
3
4
5
6
7
8
9
<section contextmenu="mymenu">
<p>Yes, this section right here</p>
</section>
<menu type="context" id="mymenu">
  <menuitem label="Please do not steal our images" icon="img/forbidden.png"></menuitem>
  <menu label="Social Networks">
  <menuitem label="Share on Facebook" onclick="window.location.href = 'http://facebook.com/sharer/sharer.php?u=' + window.location.href;">   </menuitem>
  </menu>
</menu>

Snippet Source/Credit: Speckyboy

How To Redirect To First Post If Only One Post Is Found

April 4, 2014 By Editorial Staff in WordPress No Comments Tags: Code Snippet, First Post, Post, Snippet, WordPress, WordPress Snippet

Pasting and copying the following snippet in your theme’s functions.php file will automatically redirect your website visitors to the first post if only one post is found.

1
2
3
4
5
6
7
8
9
add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
    if (is_search()) {
        global $wp_query;
        if ($wp_query->post_count == 1) {
            wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
        }
    }
}

Snippet Source/Credit: Paulund.co.uk

How To Automatically Add Open Graph To Your Posts

By Editorial Staff in Featured, WordPress No Comments Tags: Code Snippet, Facebook, Open Graph, Snippet, WordPress, WordPress Snippet

Facebook’s open graph can be a powerful tool for brands but the only thing important is they must know on how to use it. If you want to add Open Graph metadata to your WordPress posts, simply paste the following snippet into your theme’s functions.php file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function wptuts_opengraph_for_posts() {
    if ( is_singular() ) {
        global $post;
        setup_postdata( $post );
        $output = '<meta property="og:type" content="article" />' . "\n";
        $output .= '<meta property="og:title" content="' . esc_attr( get_the_title() ) . '" />' . "\n";
        $output .= '<meta property="og:url" content="' . get_permalink() . '" />' . "\n";
        $output .= '<meta property="og:description" content="' . esc_attr( get_the_excerpt() ) . '" />' . "\n";
        if ( has_post_thumbnail() ) {
            $imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
            $output .= '<meta property="og:image" content="' . $imgsrc[0] . '" />' . "\n";
        }
        echo $output;
    }
}
add_action( 'wp_head', 'wptuts_opengraph_for_posts' );

Once done, now open your header.php and replace the <html> tag with:

1
<html <?php language_attributes(); ?> prefix="og: http://ogp.me/ns#">

Snippet Source/Credit: WP Tutsplus

 

«< 63 64 65 66 67 >»

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