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

Making Your WordPress Website SEO Friendly

April 2, 2021 By Editorial Staff in Featured, WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following snippet, you will be able to make your WordPress website SEO friendly.

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;
}

Now, add the following line of code into your theme’s header.php file.

1
<?php echo basic_wp_seo(); ?>

Snippet Source/Credit: Jeff Starr

How To Use Shortcode To Display A YouTube Video Thumbnail

March 26, 2021 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following snippet, you will be able to display a YouTube video thumbnail using shortcode.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
    Shortcode to display youtube thumbnail on your wordpress blog.
    Usage:
    [youtube_thumb id="VIDEO_ID" img="0" align="left"]
    VIDEO_ID= Youtube video id
    img=0,1,2 or 3
    align= left,right,center
*/
function wp_youtube_video_thumbnail($atts) {
     extract(shortcode_atts(array(
          'id' => '',
          'img' => '0',
          'align'=>'left'
     ), $atts));
    $align_class='align'.$align;
    return '<img src="<a href="http://img.youtube.com/vi/'.$id.'/'.$img.'.jpg&quot" rel="nofollow">http://img.youtube.com/vi/'.$id.'/'.$img.'.jpg&quot</a>; alt="" class="'.$align_class.'" />';
}
add_shortcode('youtube_thumb', 'wp_youtube_video_thumbnail');

Once done, all you have to do is to use the shortcode below:

1
[youtube_thumb id="rNWeBVBqo2c" img="0" align="center"]

Snippet Source/Credit: Gunay

Counting Retweets In Full Text

March 19, 2021 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following lines of code, you will be able to count retweets in full text.

1
2
3
4
5
6
7
8
9
10
function tweetCount($url) {
    $content = file_get_contents("http://api.tweetmeme.com/url_info?url=".$url);
    $element = new SimpleXmlElement($content);
    $retweets = $element->story->url_count;
    if($retweets){
        return $retweets;
    } else {
        return 0;
    }
}

Now copy and paste the following lines of code in your theme’s single.php file.

1
2
$rt = tweetCount(get_permalink());
echo "Retweeted ".$rt." times.";

Snippet Source/Credit: WPRecipes

Using Shortcodes In WordPress Theme Files

March 12, 2021 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following snippet, you will be able to use shortcodes in WordPress theme files.

1
<?php echo do_shortcode("[your_shortcode]"); ?>

Counting Your Blogroll Links

March 5, 2021 By Editorial Staff in Featured, WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following snippet, you will be able to count your blogroll links.

1
2
3
4
5
<?php
$numlinks = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'");
if (0 < $numlinks) $numlinks = number_format($numlinks);
echo $numlinks;
?>

Snippet Source/Credit: Jeff Starr

How To Solve Multiple Loops Problem In WordPress

February 23, 2021 By Editorial Staff in Featured, WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet

The following snippet will let you solve multiple loops problem in WordPress.

1
<?php  wp_reset_query(); ?>

How To Automatically Notify Members On New Posts In WordPress

February 16, 2021 By Editorial Staff in Featured, WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following snippet below, you will be able to automatically notify members on new posts in WordPress.

1
2
3
4
5
6
7
8
function email_members($post_ID)  {
    global $wpdb;
    $usersarray = $wpdb->get_results("SELECT user_email FROM $wpdb->users;");    
    $users = implode(",", $usersarray);
    mail($users, "New WordPress recipe online!", 'A new recipe have been published on http://www.wprecipes.com');
    return $post_ID;
}
add_action('publish_post', 'email_members');

Snippet Source/Credit: WordPress Codex

Adding An Extra Contact Methods To User Profiles In WordPress

February 9, 2021 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following lines of codes below, you will be able to add an extra contact methods to user profiles in WordPress.

1
2
3
4
5
6
add_filter('user_contactmethods', 'my_user_contactmethods');
function my_user_contactmethods($user_contactmethods){
  $user_contactmethods['twitter'] = 'Twitter Username';
  $user_contactmethods['facebook'] = 'Facebook Username';
  return $user_contactmethods;
}

Snippet Source/Credit: TutsPlus

How Not To Allow HTML In Comments

February 2, 2021 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following snippet, you will be able to restrict the use of HTML in comments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function plc_comment_post( $incoming_comment ) {
// convert everything in a comment to display literally
$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
// the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
$incoming_comment['comment_content'] = str_replace( "'", '&apos;', $incoming_comment['comment_content'] );
return( $incoming_comment );
}
// This will occur before a comment is displayed
function plc_comment_display( $comment_to_display ) {
// Put the single quotes back in
$comment_to_display = str_replace( '&apos;', "'", $comment_to_display );
return $comment_to_display;
}
add_filter( 'preprocess_comment', 'plc_comment_post', '', 1);
add_filter( 'comment_text', 'plc_comment_display', '', 1);
add_filter( 'comment_text_rss', 'plc_comment_display', '', 1);
add_filter( 'comment_excerpt', 'plc_comment_display', '', 1);

Snippet Source/Credit: TheBlog.ca

Automatically Notifying Members On New Posts

January 26, 2021 By Editorial Staff in Featured, WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following snippet, you will be able to automatically notify members on new posts in WordPress.

1
2
3
4
5
6
7
8
function email_members($post_ID)  {
    global $wpdb;
    $usersarray = $wpdb->get_results("SELECT user_email FROM $wpdb->users;");    
    $users = implode(",", $usersarray);
    mail($users, "New WordPress recipe online!", 'A new recipe have been published on http://www.wprecipes.com');
    return $post_ID;
}
add_action('publish_post', 'email_members');

Snippet Source/Credit: WordPress Codex

< 1 2 3 4 5 >»

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