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

How To Detect End Users From Twitter In WordPress

January 21, 2020 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet

The following snippet will let you detect users from Twitter in WordPress.

1
2
3
4
5
<?php
if (strpos($_SERVER[HTTP_REFERER], "twitter.com") == true) {
    echo "Hello Twitter User!";
}
?>

Snippet Source/Credit: WP-Snippets.com

 

How To Display The Results In WordPress Search

January 7, 2020 By Editorial Staff in Featured, WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

To achieve the same, simply find a file name search.php and locate the following:

1
<h2 class="pagetitle">Search Results</h2>

Now, simply replace it with to:

1
2
3
4
5
6
7
8
9
<h2 class="pagetitle">Search Results for <?php
/* Search Count */
$allsearch = &new WP_Query("s=$s&showposts=-1");
$key = wp_specialchars($s, 1);
$count = $allsearch->post_count; _e('');
_e('<span class="search-terms">');
echo $key; _e('</span>'); _e(' — ');
echo $count . ' '; _e('articles');
wp_reset_query(); ?></h2>

Snippet Source/Credit: ProBlogDesign

How To Redirect To A Custom Page After Registration In WordPress

January 2, 2020 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Simply copy the snippet and paste it to your theme’s functions.php file and you will be able to redirect to a custom page after registration.

1
2
3
4
function __my_registration_redirect(){
    return home_url( '/my-page' );
}
add_filter( 'registration_redirect', '__my_registration_redirect' );

Snippet Source/Credit: TheDeadMedic

 

How To Display Thumbnail Of A YouTube Video

November 26, 2019 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the snippet below in your theme’s functions.php file will let you display thumbnail of a YouTube video.

Shortcode to display youtube thumbnail on your wordpress blog. Usage:

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

 

How To Embed A Page Into Another Page In WordPress

November 19, 2019 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 embed a page into another page in your WordPress blog.

1
2
3
4
<?php $recent = new WP_Query("page_id=**ID**"); while($recent->have_posts()) : $recent->the_post();?>
       <h3><?php the_title(); ?></h3>
       <?php the_content(); ?>
<?php endwhile; ?>

Snippet Source/Credit: CSS Tricks

 

How To Set A Maximum Word Count On Post Titles In WordPress

November 12, 2019 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following snippet to your theme’s functions.php file, will let you set a maximum word count on post titles.

1
2
3
4
5
6
7
function maxWord($title){
    global $post;
    $title = $post->post_title;
    if (str_word_count($title) >= 10 ) //set this to the maximum number of words
        wp_die( __('Error: your post title is over the maximum word count.') );
}
add_action('publish_post', 'maxWord');

Snippet Source/Credit: Pippin

 

Adding Featured Image In WordPress Website RSS Feed

November 5, 2019 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 add featured image in WordPress website RSS feed.

1
2
3
4
5
6
7
8
9
10
11
add_filter( 'the_content', 'featured_image_in_feed' );
function featured_image_in_feed( $content ) {
    global $post;
    if( is_feed() ) {
        if ( has_post_thumbnail( $post->ID ) ){
            $output = get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:right; margin:0 0 10px 10px;' ) );
            $content = $output . $content;
        }
    }
    return $content;
}

Snippet Source/Credit: WPSnipp

Adding Facebook Comment Box In WordPress

October 29, 2019 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 add Facebook comment box in your WordPress website.

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
<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=Your App ID Here";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<meta property="fb:app_id" content="Your App ID Here"/>
<div class="fb-comments" data-href="<?php the_permalink() ?>" data-num-posts="2" data-width="470" data-colorscheme="light" data-mobile="false"></div>
// Get combined FB and WordPress comment count
function get_full_comment_count() {
global $post;
$url = get_permalink($post->ID);
$filecontent = file_get_contents('https://graph.facebook.com/?ids=' . $url);
$json = json_decode($filecontent);
$count = $json->$url->comments;
$wpCount = get_comments_number();
$realCount = $count + $wpCount;
if ($realCount == 0 || !isset($realCount)) {
$realCount = 0;
}
return $realCount;
}
function the_full_comment_count() {
$realCount = get_full_comment_count();
echo $realCount;
}

Snippet Source/Credit: TutsPlus

Creating Page On WordPress Theme Activation

October 22, 2019 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 create page on theme activation in WordPress.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if (isset($_GET['activated']) && is_admin()){
        $new_page_title = 'This is the page title';
        $new_page_content = 'This is the page content';
        $new_page_template = ''; //ex. template-custom.php. Leave blank if you don't want a custom page template.
        //don't change the code bellow, unless you know what you're doing
        $page_check = get_page_by_title($new_page_title);
        $new_page = array(
                'post_type' => 'page',
                'post_title' => $new_page_title,
                'post_content' => $new_page_content,
                'post_status' => 'publish',
                'post_author' => 1,
        );
        if(!isset($page_check->ID)){
                $new_page_id = wp_insert_post($new_page);
                if(!empty($new_page_template)){
                        update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
                }
        }
}

Snippet Source/Credit: WPCanyon.com

How To Detect iPhone Users In WordPress

September 30, 2019 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet

The following snippet will let you detect iPhone users in WordPress.

1
2
3
4
5
6
<?php
global $is_iphone;
if($is_iphone) {
// User in on an iPhone.
}
?>

Snippet Source/Credit: WP-Snippet.com

«< 3 4 5 6 7 >»

Recently Added Snippets

  • Removing Widgets From WordPress Dashboard
  • Adding Custom Pinterest Button In WordPress Website
  • Setting Up WordPress Post Expiration Code
  • Changing WordPress Role Access To Menu And Widgets
  • Deleting Post Revisions In WordPress

Search The Snippet

Featured Snippets

  • Removing Widgets From WordPress Dashboard
  • Setting Up WordPress Post Expiration Code
  • Getting Email Alerts For 404 Pages In WordPress
  • Removing News Feed In WordPress Dashboard
  • How To Use Gravatar Image As WordPress Favicon
Code & Me
  • About
  • Recently Added
  • Advertise
  • Terms
  • Privacy Policy
  • Contact
© 2014-19 CodeAndMe.
Proud Member Of G2One Network Family.

↑ Back to top