WordPress Tricks Part 1

Hello, This is my first tricks & tips about WordPress. WordPress is very inspiring to me, so I have decided to create a similar post for all of the useful WordPress code snippets, tips and tricks that I have collected from any source and consist of some parts, so stay to visit my site for next WordPress Tips. Happy Blogging.

WordPress Shortcodes

Save time by replacing your most commonly typed words and phrases with WordPress shortcodes. For example, if you are frequently typing your blog’s URL, you could place the following code your theme’s functions.php file:

function shortURL() {
return ‘http://xenritech.com/’;
}
add_shortcode(‘myurl’, ‘shortURL’);

Now whenever I write a post via “HTML-mode”, all I need to include my blog’s URL is type “[myurl]”. Works great in WordPress 2.5 or better.

Shortcodes with Attributes
To create a shortcode for links, we need to include the href attribute information as well as the anchor text for the link itself. We can do this by placing this function in your theme’s functions.php file:

function shortLink($atts, $content = null) {
extract(shortcode_atts(array(
“href” => ‘http://’ // default URL
), $atts));
return ‘<a href=”‘.$href.’”>’.$content.’</a>’;
}
add_shortcode(‘link’, ‘shortLink’);

Then, when creating a post, emulate the following format to include any links you wish:

[link href="http://xenritech.com/"]xenritech[/link]

..which will output the following code:

<a href=”http://xenritech.com/”>xenritech</a>

When the href attribute is removed from the shortcode, the default URL will be used. You may specify the default URL in the third line of the funtion (see comment).

Source: Digging into WordPress

Display WordPress Permalinks Outside of the Loop

Normally, permalink display via the_permalink() requires the loop. To display permalinks outside of the loop, we may use either of the following methods:

<!– external permalink via post ID –>
<a href=”<?php echo get_permalink(33); ?>” >Permalink</a>

<!– external permalink via global $post variable –>
<a href=”<?php echo get_permalink($post->ID); ?>” >Permalink</a>

Source: Julien Chauvin

Display Custom Message to Returning Visitors

We can greet returning commentators with a custom message by extracting their information from the comment cookie left on their machine. Place the following code into the desired location in your theme and customize the message and markup to whatever you wish:

<?php if(isset($_COOKIE['comment_author_'.COOKIEHASH])) {
$lastCommenter = $_COOKIE['comment_author_'.COOKIEHASH];

echo “Welcome Back “. $lastCommenter .”!”;

} else {

echo “Welcome, Guest!”;
} ?>

Source: ifohdesigns

Display Recently Updated Posts and Pages

Easily display a list of recently updated posts by placing the following code into the desired location in your theme:

<?php

$today  = current_time(‘mysql’, 1);
$number = 5; // number of posts

if($recentposts = $wpdb->get_results(“SELECT ID, post_title FROM $wpdb->posts WHERE post_status = ‘publish’ AND post_modified_gmt < ‘$today’ ORDER BY post_modified_gmt DESC LIMIT $number”)):

?>

<h2><?php _e(“Recently Updated”); ?></h2>
<ul>
<?php

foreach($recentposts as $post) {

if($post->post_title == ”) $post->post_title = sprintf(__(‘Post #%s’), $post->ID);
echo ‘<li><a href=”‘.get_permalink($post->ID).’”>’.the_title().’</a></li>’;

} ?>
</ul>

<?php endif; ?>

Of course, customize the details as necessary and remember to set the number of posts via the “$howMany” variable.

Source: Corey

Display Custom Content to Search Engine Visitors

Display custom content to your search-engine traffic by placing the following code into your theme’s functions.php file:

<?php function scratch99_fromasearchengine() {

$ref = $_SERVER['HTTP_REFERER'];
$SE  = array(‘/search?’, ‘images.google.’, ‘web.info.com’, ‘search.’, ‘del.icio.us/search’, ‘soso.com’, ‘/search/’, ‘.yahoo.’);

foreach($SE as $source) {
if(strpos($ref, $source) !== false) return true;
}
return false;
} ?>

After checking and editing the $SE array with the search-engine referrer information of your choice, place this next chunk of code into the desired display location in your theme file(s):

<?php if(function_exists(‘scratch99_fromasearchengine’)) {
if (scratch99_fromasearchengine()) {

// INSERT YOUR CODE HERE

}
} ?>

Add whatever content or code you wish to the specified area and enjoy targeted delivery to search-engine visitors only.

Source: Stephen Cronin
Display Last Modified Time and Date for Posts

Display the “last-modified” time for your posts by placing this code anywhere within the loop:

Posted on <?php the_time(‘F jS, Y’) ?>
<?php $u_time = get_the_time(‘U’);
$u_modified_time = get_the_modified_time(‘U’);
if($u_modified_time != $u_time) {
echo “and last modified on “;
the_modified_time(‘F jS, Y’);
echo “. “;
} ?>

Source: Kyle Eslick


Display Total Number of Trackbacks and Pingbacks

Display the trackback/pingback count for each of your posts by first placing this code into your theme’s functions.php file:

function comment_count() {
global $wpdb;
$count = “SELECT COUNT(*) FROM $wpdb->comments WHERE comment_type = ‘pingback’ OR comment_type = ‘trackback’”;
echo $wpdb->get_var($count);
}

And then call the number for display anywhere in your theme by adding this code to the desired location:

<?php comment_count(); ?>

Alternately, in WordPress 2.7, you may display the total number of comments by inserting this code directly into your comments.php template:

<?php if(!empty($comments_by_type['comment'])) :
$count = count($comments_by_type['comment']);
($count !== 1) ? $txt = “Comments” : $txt = “Comment”;
?>
<h3><?php echo $count . ” ” . $txt; ?></h3>

Edit the markup as desired. Will display “” or “”, depending on the number of comments.

Source: WPengineer.com

[Keep Share]

Related Posts

Leave a comment

2 Comments.

  1. i am happy to find it thanks for sharing it here. Nice work.

  2. very good information you write it very clean. I’m very lucky to get

    this info from you.

Leave a Reply


[ Ctrl + Enter ]