Category: Tutorials

Show Recent Comments

The following code will create a wordpress shortcode that you may use anywhere on your site.

function recent_comments()
    $recentcomments = get_comments(array( 
        'number'      => 10, // number of comments
        'status'      => 'approve',
        'post_status' => 'publish',
        'author__not_in' => 1 // exclude admin

    if ($recentcomments) {
        $output .= 'Recent 10 comments<br />';
        $output .= '<ul>';

        foreach ((array)$recentcomments as $comment) {
            $comment_author_link = get_comment_author_link($comment); // commentator name and link
            $comment_post_link = esc_url(get_comment_link($comment)); // link of post commentator comment on
            $comment_post_title = get_the_title($comment->comment_post_ID); // title of post commentator comment on
            $comment_excerpt = wp_trim_words(get_comment_excerpt($comment), 9); // the comments, trimmed to first 9 words

            $output .= '<li class="recent_comments">' . $comment_author_link . ' on <a href="' . comment_post_link . '">' . $comment_post_title . '</a> &rarr; ' .   $comment_excerpt .'</li>';
            // sample output:
            // Guest on Post Title
            // Hello there, this is my comment to your blog…

        $output .= '</ul>';	

    return $output;
add_shortcode('recent_comments', recent_comments);

You may see it in action on this page.

Make Link to Comment Author Opens in New Window

Just open and edit your comment-template.php inside the wp-includes folder. Search the file’s source code until you find the function get_comment_author_link as below;

function get_comment_author_link( $comment_ID = 0 ) {
    $comment = get_comment( $comment_ID );
    $url     = get_comment_author_url( $comment );
    $author  = get_comment_author( $comment );
    if ( empty( $url ) || 'http://' == $url )
        $return = $author;
        $return = "<a href='$url' rel='external nofollow' class='url'>$author</a>";
    return apply_filters( 'get_comment_author_link', $return, $author, $comment->comment_ID );

Now you only need to add target='_blank' inside the link tag, of the second $return variable;

$return = "<a href='$url' rel='external nofollow' target='_blank'>$author</a>";

Save the file and upload it to the wp-includes folder.

Exclude Certain Category Posts From the Blog Page

The correct way to exclude categories from main blog page, is to make use of pre_get_posts which change the query variable before the main query is executed. The following is an example;

function exclude_category($query) {
    if ($query->is_home() && $query->is_main_query()) {
        $query->set(‘cat’, ‘-3,-6’);
add_action(‘pre_get_posts’, ‘exclude_category’);

For the full list of available parameters you can use with pre_get_posts, please visit WP_Query codex page.

The Best Way to Redirect URL

Terdapat 3 cara utama on how to redirect a URL;

Cara 1: Javascript

<script type="text/javascript">
    window.location = ""

Cara ini akan take viewers anda terus ke URL yang disetkan, as soon as mereka start loading the page. window.location property dalam javascript ini yang akan menentukan URL apa yang akan di load pada browser. Anda hanya perlu setkan URL yang anda kehendaki pada property tersebut.

Cara 2: Meta Refresh Redirects using Http Meta Tag

<meta http-equiv="refresh" content="2;" />

Antara cara yang seringkali dan paling mudah digunakan. Meta tag ni terletak di bahagian
<head> dokumen HTML anda.

Bagi attribute content;
[code lang=”html”]content=”2;”[/code]
nombor tersebut mewakili masa (dalam saat), until the page akan di redirect. Kemudian, separated by semi-colon (;) setkan url of page that will be loaded (in this example,

But these first 2 ways are not highly recommended. It’s quite bad for your site’s SEO actually. Search engine akan unindex / delete from search engine index, domain yang banyak mengguna meta refresh tag untuk redirect page ni. Ini kerana pada pandangan search engine, your site might be seen as a spam site.

Both cara ini banyak digunakan oleh spammers untuk fool search engines and visitors. Spammers akan set up a site dengan berbagai2 bagai keywords yang akan menampakkan ianya seperti web yang sebenar. Kemudian mereka akan meletakkan Meta Refresh Redirects tag atau Javascripts Redirect code ini di page supaya anda akan being redirected to their actual spam page.

Therefore, bila your site dah being unindexed, anda akan lose banyak traffic kerana tidak tersenarai dalam search results.

Cara 3: 301 Server Redirects using .htaccess

Redirect 301 /old-path/old-page.html

Cara ini merupakan cara terbaik untuk redirect web page / site untuk mengekalkan ranking di search engine. This peace of code terbahagi kepada 3 elemen;

Redirect 301 membawa maksud the page is moved permanently.

/old-path/old-page.html pula adalah folder path and file name lama yang hendak di redirect. Note: Jangan tambah “http://www” pada bahagian ini, hanya letakkan path from the top level of your site to the page. pula untuk path dan file baru you want the pages redirected to.

Eh, saya takde .htaccess file tu lah. Macam mana nak setkan?
Sonang bai tuh. Just follow these simple steps;

1. Jika anda tiada file .htaccess dalam root directory, just open a new text file, and save the file as .htaccess (there is no extension).

2. Jika anda sudah ada file tersebut, download it, and open the file for editing.

3. Add the peace of code as above;
[code]Redirect 301 /old-path/old-page.html[/code]

4. Jika file .htaccess anda sudah ada lines of codes in it, just skip a line, kemudian tambahlah the code above.

5. Save the .htaccess file and uploadkannya ke root directory web server anda.

6. Test your redirect dengan menaip the old address yang hendak di redirect tadi. Anda akan immediately being taken to the new location.

Why is this the best method for SEO? Ini kerana, apabila search engine menghantar spider untuk crawl ke website anda, mereka akan follow rules yang di create di .htaccess file – not actually reading it, but recognizes responces dari server sebagai valid. So, during their next update (which according to pakcik Google, takes about 6-8 weeks), old file tersebut akan di drop dan digantikan dengan the new one ;D