Comments + Feeds – Functions.php / Enque / Header / Footer


/* ////////////////////////////////////////////*/
/* /////////////  COMMENTS AND FEEDS   ////////////////*/
/*//////////////////////////////////////////////////////////////////////////////////*/







 /*------------------------------------*
Manipulate RSS Feed Footer
 *------------------------------------*/

/*

Have you seen blogs that adds their advertisement in their RSS Feeds below each post. You can accomplish that easily with a simple function. Paste the following code:
https://www.wpbeginner.com/wp-tutorials/how-to-add-content-and-completely-manipulate-your-wordpress-rss-feeds/

*/


function wpbeginner_postrss($content) {
if(is_feed()){
$content = 'This post was written by Syed Balkhi '.$content.'Check out WPBeginner';
}
return $content;
}
add_filter('the_excerpt_rss', 'wpbeginner_postrss');
add_filter('the_content', 'wpbeginner_postrss');


 /*------------------------------------*
Add Featured Images to RSS Feeds
 *------------------------------------*/

/*


The post thumbnail or featured images are usually only displayed within your site design. You can easily extend that functionality to your RSS feed with a simple function in your RSS feed.

For more details see our guide on how to add post thumbnails to your WordPress RSS feed.
https://www.wpbeginner.com/wp-tutorials/how-to-add-post-thumbnail-to-your-wordpress-rss-feeds/
*/

/* version 1 */

function rss_post_thumbnail($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$content = '
 
' . get_the_post_thumbnail($post->ID) .
'
 
' . get_the_content();
}
return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');


/* version 2 */

function rss_post_thumbnail($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$content = '<p>' . get_the_post_thumbnail($post->ID) .
'</p>' . get_the_content();
}
return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');



 /*------------------------------------*
REDIRECT FEEDS TO FEEDBURNER
 *------------------------------------*/

/*
Add the following code to .htaccess for turning WP feeds to FeedBurner feeds.

# temp redirect wordpress content feeds to feedburner

<IfModule mod_rewrite.c>

 RewriteEngine on

 RewriteCond %{HTTP_USER_AGENT} !FeedBurner    [NC]

 RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]

 RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/yourfeed [R=302,NC,L]

</IfModule>

Make sure that you customize the above code with your own FeedBurner feed before saving.
*/

 /*------------------------------------*
CATEGORY SPECIFIC RSS FEEDS
 *------------------------------------*/

/*
It is a good idea to offer your readers to subscribe to a particular category of your blog especially if you cover a wide range of categories on your blog.
Simply add /feed to the end of the category URL.

*/




 /*------------------------------------*
DELAY WHEN POSTS GO TO RSS
 *------------------------------------*/

/*

Sometimes you may end up with a grammar or spelling mistake in your article. The mistake goes live and is distributed to your RSS feed subscribers. If you have email subscriptions on your WordPress blog, then those subscribers will get it as well.

Simply add this code in your theme's functions file.

For plugin method and more information, see our detailed guide on how to delay posts from appearing in WordPress RSS feed.
https://www.wpbeginner.com/wp-tutorials/how-to-delay-posts-from-appearing-in-wordpress-rss-feed/

*/

/* VERSINO 1 */


When you publish a blog post, immediately it will send your subscribers notification about the new post via RSS feeds. You can delay posting to RSS feeds for an hour. This can be useful especially in case if you forget to check for broken links or typos before publishing your posts.

function Delay_RSS_After_Publish($where) {

  global $wpdb;

 

  if (is_feed()) {

    $now = gmdate('Y-m-d H:i:s');

    $wait = '60';

    $device = 'MINUTE';

    $where.=" AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, '$now') > $wait ";

  }

  return $where;

}

add_filter('posts_where', 'Delay_RSS_After_Publish');





/ * VESRION 2 */


function publish_later_on_feed($where) {
 
    global $wpdb;
 
    if ( is_feed() ) {
        // timestamp in WP-format
        $now = gmdate('Y-m-d H:i:s');
 
        // value for wait; + device
        $wait = '10'; // integer
 
        // http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
        $device = 'MINUTE'; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR
 
        // add SQL-sytax to default $where
        $where .= " AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, '$now') > $wait ";
    }
    return $where;
}
 
add_filter('posts_where', 'publish_later_on_feed');

/* In this code we have used 10 minutes as $wait or delay time. Feel free to change that into any number of minutes you want.*/


 /*------------------------------------*
EXCLUDE CATEGORIES FROM RSS
 *------------------------------------*/

/*

Exclude Specific Categories from RSS Feed
Do you want to exclude specific categories from your WordPress RSS feed? Add this code to your theme’s functions file.


*/

/* version 1 */

function exclude_category($query) {
if ( $query->is_feed ) {
$query->set('cat', -2, -3');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');


/* version 2 */

function exclude_category($query) {
    if ( $query->is_feed ) {
        $query->set('cat', '-5, -2, -3');
    }
return $query;
}
add_filter('pre_get_posts', 'exclude_category');



 /*------------------------------------*
DISABLE RSS FEEDS
 *------------------------------------*/

/*

Disable RSS Feeds in WordPress
Not all websites need RSS feeds. If you want to disable RSS feeds on your WordPress site, then add this code to your theme’s functions file.

*/

/* version 1 */

function fb_disable_feed() {
wp_die( __('Sorry, we don't use RSS!') );
}
add_action('do_feed', 'fb_disable_feed', 1);
add_action('do_feed_rdf', 'fb_disable_feed', 1);
add_action('do_feed_rss', 'fb_disable_feed', 1);
add_action('do_feed_rss2', 'fb_disable_feed', 1);
add_action('do_feed_atom', 'fb_disable_feed', 1);



/* version 2 */

function fb_disable_feed() {
wp_die( __('No feed available,please visit our <a href="'. get_bloginfo('url') .'">homepage</a>!') );
}
 
add_action('do_feed', 'fb_disable_feed', 1);
add_action('do_feed_rdf', 'fb_disable_feed', 1);
add_action('do_feed_rss', 'fb_disable_feed', 1);
add_action('do_feed_rss2', 'fb_disable_feed', 1);
add_action('do_feed_atom', 'fb_disable_feed', 1);






   /*------------------------------------*
   EXPORT EMAIL OF USER SUBMITTED COMMENTS
   *------------------------------------*/

/* 
Execute the below SQL query against your database to export all the user submitted email addresses with no duplicates. This can be helpful for building your email list.
SELECT DISTINCT comment_author_email FROM wp_comments;

Following the implementation of GDPR, this needs to be exclusively specified and agreed to as part of your privacy policy. You also need to double opt-in and users which are added to your mailing list in this way.
*/


  /*------------------------------------*
   DISABLE HTML IN COMMENTS
   *------------------------------------*/

/* 
Spammers used to submit tons of HTML links while commenting, which can have negative impact in bringing organic traffic to your website. Simply open functions.php and insert the following code to disable HTML elements in comments.

// This will occur when the comment is posted

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( "'", ''', $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( ''', "'", $comment_to_display );

return $comment_to_display;




 /*------------------------------------*
SEPARATE COMMENTS AND TRACKBACKS
 *------------------------------------*/

/*

By default, WordPress combines both comments and trackbacks together. Separating both can make the things look more organized.
Step 1: Find the below code in the comments.php file.

<?php foreach ($comments as $comment) : ?>

Paste the below code after it.

<?php $comment_type = get_comment_type(); ?>

<?php if($comment_type == 'comment') { ?>

Step 2: Then, look for the below code

<?php endforeach; /* end for each comment */ ?>

Paste the below code before it

<?php } else { $trackback = true; } /* End of is_comment statement */ ?>

Step 3: Then, look for the following code

<?php else : // this is displayed if there are no comments so far ?>

Paste the below code before it

<?php if ($trackback == true) { ?>

  <h3>Trackbacks</h3>

  <ol>

  <?php foreach ($comments as $comment) : ?>

  <?php $comment_type = get_comment_type(); ?>

  <?php if($comment_type != 'comment') { ?>

  <li><?php comment_author_link() ?></li>

  <?php } ?>

  <?php endforeach; ?>

  </ol>

<?php } ?>

*/


   /*------------------------------------*
   PREVENT COMMENT SPAN
   *------------------------------------*/

/*


Instead of marking comments as spam each time, you can block the spammers outright from publishing comments on your blog. The below code will look for HTTP referrer and automatically blocks the comment if the referrer is not valid.
Add the below code in the functions.php file.
*/

function check_referrer() {

    if (!isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] == “”) {

        wp_die( __('Please enable referrers in your browser, or, if you're a spammer, bugger off!') );

    }

}

add_action('check_comment_flood', 'check_referrer');




 /*------------------------------------*
 EDIT COMMENTS FROM FRONT END
 *------------------------------------*/

/*
 Add actionable buttons on published comments
actionable buttons
Often, you might want to edit some published comments. By default, if you are logged in to your site, on top of every comment you published you can see a link where you can click to edit the comment. Additionally, how about marking a comment as spam or even delete it from your blog post itself? This is one of those WordPress tricks which can really help you maintain your website easier and faster, because you're doing everything from the frontend.

Add the below code to functions file.
*/
function delete_comment_link($id) {

  if (current_user_can('edit_post')) {

    echo '| <a href="'.admin_url("comment.php?action=cdc&c=$id").'">del</a> ';

    echo '| <a href="'.admin_url("comment.php?action=cdc&dt=spam&c=$id").'">spam</a>';

  }

}

/*
Then, add the below code snippet to comments.php file.

delete_comment_link(get_comment_ID());

Bonus Tip

Have you ever had the needed to create great looking popups to increase conversions to your email list? We found the best WordPress popup plugin to help you increase conversions.
*/


 /*------------------------------------*
 CUSTOMIZE TEXT AFTER OCMMENT FIELD
 *------------------------------------*/

/*

Add the following code to your theme’s functions.php to customize the text before the comment form. Replace the 'Your text here.' with your preferred text.
*/

function wpbeginner_comment_text_after($arg) {

$arg['comment_notes_after'] = "Your text here.";

return $arg; }

add_filter('comment_form_defaults', 'wpbeginner_comment_text_after');



 /*------------------------------------*
 HIGHLIGHT AUTHOR COMMENTS
 *------------------------------------*/

/*

Highlight author comments
To highlight the author comments, find the following code in your CSS file.

*/
.bypostauthor { background: #eee; }


 /*------------------------------------*
 DISPLAY MOST RECENT COMMENTS
 *------------------------------------*/

/*

Display most recent comments
Add the following changes anywhere in your theme where you want to display the 5 most recent comments. As with other WordPress tips we've mentioned here, you can change the number 5 to show more or less comments as needed.

<?php

$query = "SELECT * from $wpdb->comments WHERE comment_approved= '1'

ORDER BY comment_date DESC LIMIT 0 ,5";

$comments = $wpdb->get_results($query);

if ($comments) {

echo '<ul>';

foreach ($comments as $comment) {

$url = '<a href="'. get_permalink($comment->comment_post_ID).'#comment-'.$comment->comment_ID .'" title="'.$comment->comment_author .' | '.get_the_title($comment->comment_post_ID).'">';

echo '<li>';

echo '<div class="img">';

echo $url;

echo get_avatar( $comment->comment_author_email, $img_w);

echo '</a></div>';

echo '<div class="txt">Par: ';

echo $url;

echo $comment->comment_author;

echo '</a></div>';

echo '</li>';

}

echo '</ul>';

}

?>

*/

 /*------------------------------------*
DISPLAY MOST COMMENTED POSTS
 *------------------------------------*/

/*


Add the following lines of code to enable another from the plenty of WordPress tips which uses hooks and the functions.php file of your theme

*/

function wpb_most_commented_posts() {

ob_start();?>

<ul class="most-commented">

<?php

$query = new

WP_Query('orderby=comment_count&posts_per_page=10');

while($query->have_posts()) : $query->the_post(); ?>

<li><a href="/<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> <span class="wpb-comment-count"><?php comments_popup_link('No Comments;', '1 Comment', '% Comments'); ?></span></li>

<?php endwhile; ?>

</ul>

<?php// Turn off output buffering

$output = ob_get_clean();

return $output; }

add_shortcode('wpb_most_commented', 'wpb_most_commented_posts');

add_filter('widget_text', 'do_shortcode');


/*
Then, add this shortcode in a widget wherever you want to display the most commented posts.

[wpb_most_commented]

*/


 /*------------------------------------*
ERASE 1000'S OF COMMENTS
 *------------------------------------*/

/*

Erase thousands of unfiltered spam comments in seconds

Often, spam comments survive the spam filters and reach your ‘awaiting moderation’ list. Deleting it manually could be time-consuming. Follow the procedure to instantly delete thousands of such spam comments.
Log in to phpMyAdmin, select your website's database, click SQL and paste the code given below in the SQL command window.
DELETE from wp_comments WHERE comment_approved = '0';



*/








 /*------------------------------------*
DISABLE SUPPORT FOR COMMENTS AND TRACKBACKS
 *------------------------------------*/

/*



*/

function df_disable_comments_post_types_support() {
	$post_types = get_post_types();
	foreach ($post_types as $post_type) {
		if(post_type_supports($post_type, 'comments')) {
			remove_post_type_support($post_type, 'comments');
			remove_post_type_support($post_type, 'trackbacks');
		}
	}
}
add_action('admin_init', 'df_disable_comments_post_types_support');









 /*------------------------------------*
CLOSE COMMENTS FRONTENT
 *------------------------------------*/

/*



*/

function df_disable_comments_status() {
	return false;
}
add_filter('comments_open', 'df_disable_comments_status', 20, 2);
add_filter('pings_open', 'df_disable_comments_status', 20, 2);










 /*------------------------------------*
HIDE EXISTING COMMENTS
 *------------------------------------*/

/*



*/

function df_disable_comments_hide_existing_comments($comments) {
	$comments = array();
	return $comments;
}
add_filter('comments_array', 'df_disable_comments_hide_existing_comments', 10, 2);











 /*------------------------------------*
REMOVE COMMENTS PAGE IN MENU
 *------------------------------------*/

/*



*/


function df_disable_comments_admin_menu() {
	remove_menu_page('edit-comments.php');
}
add_action('admin_menu', 'df_disable_comments_admin_menu');








 /*------------------------------------*
REDIRECT USERS TRYING TO ACCESS COMMENTS PAGE
 *------------------------------------*/

/*



*/

function df_disable_comments_admin_menu_redirect() {
	global $pagenow;
	if ($pagenow === 'edit-comments.php') {
		wp_redirect(admin_url()); exit;
	}
}
add_action('admin_init', 'df_disable_comments_admin_menu_redirect');



 /*------------------------------------*
REMOVE COMMENTS META BOX FROM DASHBOARD
 *------------------------------------*/

/*



*/

function df_disable_comments_dashboard() {
	remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
}
add_action('admin_init', 'df_disable_comments_dashboard');







 /*------------------------------------*
REMOVE COMMENTS LINK FROM ADMIN BAR
 *------------------------------------*/

/*



*/

function df_disable_comments_admin_bar() {
	if (is_admin_bar_showing()) {
		remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
	}
}
add_action('init', 'df_disable_comments_admin_bar');









 /*------------------------------------*
HIDE COMMENT ICON IN ADMIN BAR
 *------------------------------------*/

/*



*/

add_action( 'admin_bar_menu', 'clean_admin_bar', 999 );
function clean_admin_bar( $wp_admin_bar ) {
    $wp_admin_bar->remove_node( 'comments' );
}




   /*------------------------------------*
  GOOGLE + COMMENTS
   *------------------------------------*/

/* 


Replace your existing code in your comments.php file with the below given code to enable Google+ comments on your blog posts.

<script src="https://apis.google.com/js/plusone.js">

</script>

<div class="g-comments"

data-href="<?php the_permalink(); ?>"

data-width="800"

data-first_party_property="BLOGGER"

data-view_type="FILTERED_POSTMOD">

</div>

If you’re using Twenty Fourteen theme, here’s how your comments.php file will look like after the modification.

<?php

/**

 * The template for displaying Comments

 *

 * The area of the page that contains comments and the comment form.

 *

 * @package WordPress

 * @subpackage Twenty_Fourteen

 * @since Twenty Fourteen 1.0

 */

 

/*

 * If the current post is protected by a password and the visitor has not yet

 * entered the password we will return early without loading the comments.

 */

if ( post_password_required() ) {

    return;

}

<script src="https://apis.google.com/js/plusone.js">

</script>

<div class="g-comments"

data-href="<?php the_permalink(); ?>"

data-width="800"

data-first_party_property="BLOGGER"

data-view_type="FILTERED_POSTMOD">

</div>

*/

   /*------------------------------------*
   PINTEREST
   *------------------------------------*/

/* 


Add the following code to the footer.php file.

<script type="text/javascript">

(function() {

    window.PinIt = window.PinIt || { loaded:false };

    if (window.PinIt.loaded) return;

    window.PinIt.loaded = true;

    function async_load(){

        var s = document.createElement("script");

        s.type = "text/javascript";

        s.async = true;

        s.src = "http://assets.pinterest.com/js/pinit.js";

        var x = document.getElementsByTagName("script")[0];

        x.parentNode.insertBefore(s, x);

    }

    if (window.attachEvent)

        window.attachEvent("onload", async_load);

    else

        window.addEventListener("load", async_load, false);

})();

</script>

Then, add following code to your single.php file

<?php $pinterestimage = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?>

<a href="http://pinterest.com/pin/create/button/?url=<?php echo urlencode(get_permalink($post->ID)); ?>&media=<?php echo $pinterestimage[0]; ?>&description=<?php the_title(); ?>" class="pin-it-button" count-layout="vertical">Pin It</a>

*/



   /*------------------------------------*
   SEND TO TWITTER
   *------------------------------------*/

/* 

With one click, your readers can share your blog post URL to Twitter.


Simply add the following code snippet anywhere on your posts.

<a href="http://twitter.com/home?status=Currently reading <?php the_permalink(); ?>" title="Click to send this page to Twitter!" target="_blank">Share on Twitter</a>

*/


   /*------------------------------------*
   SHOW SELECTIVE TWEETS
   *------------------------------------*/

/* 

If yours is a personal twitter account, you might not want to show every tweet to your blog readers. In such cases, you can display only selective tweets to your blog readers with a certain keyword or hashtag.

Go to Twitter Widgets>> click the button ‘create new’ >> click the ‘search’ tab

In the search query box, enter from:dartcreations webdesign

Replace the dartcreations with your twitter handle and webdesign with your preferred query. Save your widget and copy it to your widgets section of your WordPress site.
*/


   /*------------------------------------*
   FACEBOOK COMMENTS
   *------------------------------------*/

/* 

Integrating Facebook Comments to your blog posts will offer an easy way for your readers to comment on your blog as well as share it on Facebook.
Read the ultimate guide to display Facebook Comments on your blog.

*/





 /*------------------------------------*

 *------------------------------------*/

/*



*/






 /*------------------------------------*

 *------------------------------------*/

/*



*/





 /*------------------------------------*

 *------------------------------------*/

/*



*/





 /*------------------------------------*

 *------------------------------------*/

/*



*/





 /*------------------------------------*

 *------------------------------------*/

/*



*/





 /*------------------------------------*

 *------------------------------------*/

/*



*/





 /*------------------------------------*

 *------------------------------------*/

/*



*/





 /*------------------------------------*

 *------------------------------------*/

/*



*/





 /*------------------------------------*

 *------------------------------------*/

/*



*/





 /*------------------------------------*

 *------------------------------------*/

/*



*/










 /*------------------------------------*

 *------------------------------------*/

/*



*/









 /*------------------------------------*

 *------------------------------------*/

/*



*/











 /*------------------------------------*

 *------------------------------------*/

/*



*/









 /*------------------------------------*

 *------------------------------------*/

/*



*/











 /*------------------------------------*

 *------------------------------------*/

/*



*/









 /*------------------------------------*

 *------------------------------------*/

/*



*/











 /*------------------------------------*

 *------------------------------------*/

/*



*/









 /*------------------------------------*

 *------------------------------------*/

/*



*/



















Scroll to Top