How to customize the comments template or wp_list_comments()

In single.php file we have function to add comments functionality. Here in this article I will give the code for customize the comments template.

From wordpress 2.7 version we got the functionality of changing or customizing the comments template as per your theme. As we all know in single.php file we called following function to add comments functionality. wordpress comments is very important functionality. Here in this article I will give the code for customize the comment template.

customize the comments template


<?php comments_template(); ?>

This function basically calls the comments.php file and add the data from wordpress database.

If we check the comments.php file. Usually we are having following content in comments.php file

[viral-lock message=”Solution code is Hidden! It’s Visible for Users who Liked/Shared This article on Facebook or Twitter or Google+. Like or Tweet this article to reveal the content.”]


<?php

// Do not delete these lines
 if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
 die ('Please do not load this page directly. Thanks!');

 if ( post_password_required() ) { ?>
 <p>This post is password protected. Enter the password to view comments.</p>
 <?php
 return;
 }
?>

<!-- You can start editing here. -->
<div id="comment">
<?php if ( have_comments() ) : ?>
 <h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</h3>

 <div>
<div><?php paginate_comments_links(); ?></div>
 </div>

 <ol>
 <?php wp_list_comments('callback=wordpressapi_comments'); ?>

 </ol>

 <div>
<div><?php paginate_comments_links(); ?></div>
 </div>
 <?php else : // this is displayed if there are no comments so far ?>

 <?php if ('open' == $post->comment_status) : ?>
 <!-- If comments are open, but there are no comments. -->

 <?php else : // comments are closed ?>
 <!-- If comments are closed. -->
 <p>Comments are closed.</p>

 <?php endif; ?>
<?php endif; ?>

<?php if ('open' == $post->comment_status) : ?>

<div id="respond">

<h3><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?></h3>

<div>
 <small><?php cancel_comment_reply_link(); ?></small>
</div>

<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
<p>You must be <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php echo urlencode(get_permalink()); ?>">logged in</a> to post a comment.</p>
<?php else : ?>

<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">

<?php if ( $user_ID ) : ?>

<p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="Log out of this account">Log out &raquo;</a></p>

<?php else : ?>

<p><input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="22" tabindex="1" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="author"><small>Name <?php if ($req) echo "(required)"; ?></small></label></p>

<p><input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="22" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="email"><small>Mail (will not be published) <?php if ($req) echo "(required)"; ?></small></label></p>

<p><input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="22" tabindex="3" />
<label for="url"><small>Website</small></label></p>

<?php endif; ?>

<!--<p><small><strong>XHTML:</strong> You can use these tags: <code><?php echo allowed_tags(); ?></code></small></p>-->

<p><textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>

<p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
<?php comment_id_fields(); ?>
</p>
<?php do_action('comment_form', $post->ID); ?>

</form>

<?php endif; // If registration required and not logged in ?>
</div>

<?php endif; // if you delete this the sky will fall on your head ?>
</div>

In the comments loop for showing the comments we used the wp_list_comments() function. Use this for customize the comments template.

We can customize this function as per our requirement. We can pass the following parameters to this function. use following for customize the comments template.


[/viral-lock]

For customize the comments template following parameter is important.

callback (string) The name of a custom function to use to display each comment. Defaults to null. Using this will make your custom function get called to display each comment, bypassing all internal WordPress functionality in this respect. Use to customize comments display for extreme changes to the HTML layout. Not recommended.

In my theme I used that in following way. I changed and passed parameter in comments.php file.


<?php wp_list_comments('callback=wordpressapi_comments'); ?>

Then Open the functions.php file put following code in that. This code I am using as per wordpress api.


function wordpressapi_comments($comment, $args, $depth) {
 $GLOBALS['comment'] = $comment; ?>
 <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
 <div id="comment-<?php comment_ID(); ?>">
 <div>
 <?php echo get_avatar($comment,$size='32',$default='<path_to_url>' ); ?>
 <?php $user_name_str = substr(get_comment_author(),0, 20); ?>
 <?php printf(__('<cite><b>%s</b></cite> <span>says:</span>'), $user_name_str) ?>
 </div>
 <?php if ($comment->comment_approved == '0') : ?>
 <em><?php _e('Your comment is awaiting moderation.') ?></em>
 <br />
 <?php endif; ?>

 <div><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date(),&nbsp; get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),'&nbsp; ','') ?></div>

 <?php comment_text() ?>

 <div>
 <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
 </div>
 </div>
<?php
 }

Many times some people add the full URL of website in the name parameter. That time showing author name of comment writer is looks so ugly. So I used following trick in this function. Using this line of code we can show only 20 characters of author.


<?php $user_name_str = substr(get_comment_author(),0, 20); ?>
 <?php printf(__('<cite><b>%s</b></cite> <span>says:</span>'), $user_name_str) ?>

I changed the this function as per my choose. You can customize this function as per your theme requirement.

customize the comments template
customize the comments template

If you found any issue in this article “customize the comments template” about this. please write comments.

 

fetch wordpress posts which starting with specific word or alphabet

If you want to fetch wordpress posts which is starts with specific word or alphabet. That is really easy to achieve in wordpress.

Article for fetch wordpress posts which starting with specific word.

Just follow my steps:

Open your functions.php file from your wordpress theme folder.


add_action( 'posts_where', 'starting_word' );
function starting_word( $sql ){
 global $wpdb;
 $starting_word = get_query_var( 'starting_word' );

 if( $startswith ){
 $sql .= $wpdb->prepare( " AND $wpdb->posts.post_title LIKE %s ", $starting_word.'%' );
 }

 return $sql;
}

Open your index.php file or any file where you want show the wordpress posts which is starting with specific word or alphabet.

Use following code in that file.


<?php  query_posts('paged='.$paged.'&starting_word=wordpressapi&category_name=Your Category&posts_per_page=10'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>

<br />
<?php the_content(); ?>
<?php endwhile; ?>

fetch wordpress posts which starting with specific word
fetch wordpress posts which starting with specific word

how can we execute custom queries on wordpress

We can use the custom queries in wordpress. Using the custom queries we can fetch the wordpress table data as per our custom requirement.

how can we execute custom queries on wordpress

We you create your table then you can fetch values from that table using following command.

$ourdata = $wpdb->( "SELECT id, name FROM yourtable" );

If you want to create the table in wordpress then use following article.

https://purabtech.in/create-add-tables-wordpress-theme/

More example about executing the own custom quries.

$wpdb->query("
	DELETE FROM $wpdb->postmeta WHERE post_id = '123'
	AND meta_key = 'google'");

another example about adding the data into table which is from wordpress:

$table_name = "your_table";
 $welcome_name = "Mr. WordPressapi";
  $welcome_text = "Congratulations, you just completed the installation!";

  $insert = "INSERT INTO " . $table_name .
            " (time, name, text) " .
            "VALUES ('" . time() . "','" . $wpdb->escape($welcome_name) . "','" . $wpdb->escape($welcome_text) . "')";

  $results = $wpdb->query( $insert );

Getting the single value from any table

$user_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->users;"));

Get the single row value from wordpress table

$mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10", ARRAY_A);
how can we execute custom queries on wordpress
how can we execute custom queries on wordpress

you can use inner join, outer join and multiple join in wordpress custom queries.

wordpress create archive page for wordpress theme

Given code for wordpress create archive page for wordpress theme. search engine sites will look for two main file. First sitemap.xml file and archive page. First I would say somthing about archive page in any website. Archive page is very important for SEO purpose.

Create an Archive Page in your WordPress theme

If you do not having archive page in your website so you are lacking somewhere in SEO for your website.  What I am going to show in this article, How to create the archive page for worpdress theme or website.  In wordpress creating the archive is very easy.

Follow my steps to create the archive page in wordpress site.

  • Open your wordpress theme folder and copy the single.php as named archive.php
    Note: dont copy index.php file as archive.php. It may have different UI and programming attribute in index.php.
  • Then copy archive.php as archive_template.php new file.
  • Open the archive_template.php file and copy paste the following code in top of file.
<?php
/*
Template Name: Archives Page
*/
?>
  • Again open the archive_template.php file and find the post loop. Between the loop copy past the following code.
<h1> All Archives in dropdown</h1>
<select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'>
 <option value=""><?php echo attribute_escape(__('Select Month')); ?></option>
 <?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?> </select>

<br><br>
<h1> All category</h1>
<ul><?php wp_list_cats('sort_column=name&optioncount=1') ?></ul>

<br><br>
<h1> All Archives</h1>
<ul><?php wp_get_archives('type=monthly&show_post_count=1') ?></ul>

Above code will give you the archive with dropdown and archive with category name with post count and archive with month and post count.

  • After doing this upload two new files to server in wordpress theme folder (archive.php and archive_template.php).
  • Go to your wordpress Admin panel using browser
  • Create the new page with title “Archive” and put some information about your website in that page.(Dont publish the page wait and follow next steps.)
wordpress create archive page for wordpress theme
wordpress create archive page for wordpress theme
  • Check the template section (right side under attribute section). Choose archive page as template and publish and save the page.

That sit. You are able to see the archive page for your website.

How to get first category name or id from wordpress post

Many people want to know about post category and know more information current category. Some time we need to use the first category of wordpress post. As we know we can define the multiple category to single article. So if we want retrive the wordpress category then we will get the result in simple php array format. Using that array we can easily extract the first category of post.

if we want first category than, we will get result in php array format. Using that array we can easily get first category name or id from wordpress post.

How to get first category name or id from wordpress post

In this post I will show how we can achieve that. Using following code we can get the first category ID.

< ?php
$category = get_the_category();
$currentcat = $category[0]->cat_ID;
?>

Using following code we can extract the first category name from the wordpress post.

< ?php
$category = get_the_category();
$currentcat = $category[0]->cat_name;
?>

Above code we can use only in the loop but using the following code we can extract the category
outside the loop also

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
global $post;
$categories = get_the_category($post->ID);

$currentcat = $category[0]->cat_ID;
?></pre>
<pre>

same like that.. cat name

 <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
 global $post;
 $categories = get_the_category($post->ID);

$currentcat = $category[0]->cat_name;
 ?>

</pre>
<pre>
How to get first category name or id from post
How to get first category name or id from post

How to use Custom Page as Homepage in WordPress

wordpress tutorial, How to use Custom Page as Homepage in WordPress. Here we given full step by step explanation about using custom template.

This is one of the most wanted hacks that users want to know how to accomplish. First you need to learn how to create a custom page. You will need to duplicate your page.php or create a wordpress.php file and add the following code at the very top:

How to use Custom Page as Homepage in WordPress

wordpressapi */ ?>

How to use Custom Page as Homepage in WordPress
How to use Custom Page as Homepage in WordPress

You can change the template name. Change any styling, that you want in that page. Go to your WordPress admin panel and create a new page and select this template(wordpressapi).

custompagetemplate

Select your page to be the homepage as per your choice. Now you have yourself a Custom Home Page.

Have fun!

Desktop application for WordPress post

We use to write my wordpress blog daily. That is like our habit. That is like my habit. WordPress visual editor is so good. We need to login each time to wordpress and write a blog post. page refreshing, checking post that part is boring. List of Desktop application for WordPress.

Desktop application for WordPress post

So I looked for solution to write blog from desktop application. I found very use full application which is free for wordpress.

Website: Zoundry.com

Desktop application for WordPress post
Desktop application for WordPress post

Price: Free

Summary: This software is excellent! This is what I had in mind when I set out to find desktop blogging software for wordpress. The usability is excellent. Initially, I expected some type of configuration wizard or walk-through. Instead, what I received was a panel that had the steps with links on how to set up my blog for Zoundry lovin’. I believe it worked better than a walk-through could have, since it taught me the way to set up another blog if I needed it. This app got out of my way and let me do what I set out to do. Zoundry was definitely built by a programmer with excellent usability experience.

Advantages:

  • Automatic blog-type discovery (just provide the URL!)
  • Easy to set up accounts
  • Imports your blog posts
  • Getting started tab guides you through blog set up
  • HTML validator built into the code view (big plus)
  • WYSIWYG features still usable in code view
  • Includes a publish menu to finalize your decisions, tags, trackbacks, etc.

Disadvantages:

  • Scroll wheel on my mouse crashed the entire app on the publish menu. Luckily, Zoundry has an auto save feature which recovered the post I had started. Nice.
  • Interface is a little cluttery

How to show Twitter Followers Count on wordpress widget

We all are having the twitter account. If we want to show Twitter Followers Count on wordpress widget then just use our code.

How to show Twitter Followers Count on wordpress widget

Open your sidebar.php file and just copy paste the following code.

First you would need to create a file twitter.php and paste the following code in there:


<?php

//This xml file will return the all user information about from twitter account
$xml=file_get_contents('http://twitter.com/users/show.xml?screen_name=wordpressapi');
if (preg_match('/followers_count>(.*)</',$xml,$match)!=0) {
$twitter_follower_count['count'] = $match[1];

// this will show the twitter followers count
echo $twitter_follower_count['count'];
?>

WordPressapi is my twitter username. So you can change it your username.

How to add HTML code on WordPress home page

Some time we want to show Some custom text or HTML code on home page. In this article we given tricks and steps to add HTML code on WordPress home page.

How to add HTML code on WordPress home page

  • First method

you need go to wordpress admin panel.Create page with home name. Add your HTML or custom text in that page.

Go to Setting->reading section. There is “Front page displays”  section. We can found following two radio box over there.

  • Your latest posts
  • A static page (select below)

Select “A static page (select below) ” radio box. Than from “Front page:” select box, select the Home page. That sit.

How to add HTML code on WordPress home page
How to add HTML code on WordPress home page
  • Second Method

If you want to add some different text or HTML code, than you need open functions.php file which you found in wordpress themes folders. Just copy and paste following code in that file.


<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php

if (is_home() {
echo "HTML CODE GOES HERE OR TEXT";

}

?>

Note: You need replace HTML code. I used sample text in code.

Have fun!

How to fetch Feedburner Subscriber Count in wordpress

We all are using the feedburner button in wordpress sites. In this article showing, How to fetch Feedburner Subscriber Count in wordpress website using our code.

How to fetch Feedburner Subscriber Count in wordpress

Just copy paste the following code in your sidebar.php file(This file you will find in your template folder.)

<?php

//You need to change the uri parameter to your website name
$whaturl=”http://api.feedburner.com/awareness/1.0/GetFeedData?uri=wordpressapi/ZoqV”;

//Initialize the Curl for using the feedburner api
$wordpressapi = curl_init();

//Set curl to return the data instead of printing it to the browser.
curl_setopt($wordpressapi, CURLOPT_RETURNTRANSFER, 1);

//Set the URL
curl_setopt($wordpressapi, CURLOPT_URL, $whaturl);

//fetching data
$data = curl_exec($wordpressapi);

//Closing connection
curl_close($wordpressapi);
$xml = new SimpleXMLElement($data);
$fbcount = $xml->feed->entry[‘circulation’];
echo $fbcount;
//end get cool feedburner count
?>

Echo $fbcount will print the feed Subscriber count.