How to Display the total number of your Twitter followers on WordPress

By pediabook - Last updated: Sunday, March 7, 2010

If you’re on Twitter, you probably display the number of your followers on your wordpress blog, using the chicklet from TwitterCounter.com.

we’ll show you how to display your followers in full text mode.

The first thing to do is to paste the following php functions on the functions.php file from your WordPress theme:

function string_getInsertedString($long_string,$short_string,$is_html=false){
if($short_string>=strlen($long_string))return false;
$insertion_length=strlen($long_string)-strlen($short_string);
for($i=0;$i<strlen($short_string);++$i){
if($long_string[$i]!=$short_string[$i])break;
}

$inserted_string=substr($long_string,$i,$insertion_length);
if($is_html && $inserted_string[$insertion_length-1]==’<’){
$inserted_string=’<’.substr($inserted_string,0,$insertion_length-1);
}
return $inserted_string;
}

function DOMElement_getOuterHTML($document,$element){
$html=$document->saveHTML();
$element->parentNode->removeChild($element);
$html2=$document->saveHTML();
return string_getInsertedString($html,$html2,true);
}

function getFollowers($username){
$x = file_get_contents(”http://twitter.com/”.$username);
$doc = new DomDocument;
@$doc->loadHTML($x);
$ele = $doc->getElementById(’follower_count’);
$innerHTML=preg_replace(’/^<[^>]*>(.*)<[^>]*>$/’,”\\1?,DOMElement_getOuterHTML($doc,$ele));
return $innerHTML;
}

Then, simply paste the following anywhere on your theme files. Just replace username with yours.

<?php echo getFollowers("livexp")." followers"; ?>

Filed in Wordpress • Tags: ,

How to Display the total number of users of WordPress

By pediabook - Last updated: Sunday, March 7, 2010

If your wordpress allow user registration, what about displaying the total number of registered users? This simple code will allow you to do it easily.

Simply paste the following code anywhere in your wordpress theme files:

$users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
echo $users." registered users.";

Once you saved the file and refreshed your wordpress, the total number of users will be displayed.

Filed in Wordpress • Tags:

How to Show Wordpress Posts With a Specific Custom Field and Value

By pediabook - Last updated: Sunday, March 7, 2010

Ever wanted to be able to only get the list of wordpress posts which have a specific custom field key as well as a specific value?

Sometimes you only want to show wordpress posts that you’ve added a specific custom field to. For instance, lets say you write wordpress host and wordpress template reviews and for each you give them a custom field “review_type” with the value set to either “url” or “template”. So how would you show posts that are only wordpress template reviews?

Simply find the typical post loop and add the query_posts() function, as in the example below:

Find a typical post loop begins like this:

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

We’re going to add a simple query_posts function immediately above the loop code. In our scenario it would look like this:

<?php query_posts('meta_key=review_type&metavalue=template');  ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

You’ll get the list of post having review_type as a custom field key and template as a value. Just change theses values to fit your needs.

Filed in Wordpress • Tags: , ,

How to Return wordpress posts for various date ranges

By pediabook - Last updated: Sunday, March 7, 2010

WordPress loop is very powerful, as well as the query_posts() function, which allow you to specify some parameters for the loop to retrieve posts.

Though, there’s no built-in function or parameter to return posts for various date ranges.

Open your index.php file and find the loop. Just before the loop starts, paste the following code.

Of course, don’t forget to change the dates according to your needs.

<?php
function filter_where($where = '') {
//posts in the last 30 days
//$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
//posts 30 to 60 days old
//$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
//posts for May 8 to May 16, 2008
$where .= " AND post_date >= '2008-05-08' AND post_date < '2008-05-16'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>

Filed in Wordpress • Tags:

How to Replace WordPress content with an excerpt without editing theme files

By pediabook - Last updated: Sunday, March 7, 2010

Let’s suppose our front page and archives used the WordPress function <?php the_content(); ?> to retrieve all post content. Let’s further suppose we’re want to modify this with a child theme to show excerpts instead of the entire post.

We could easily just change <?php the_content(); ?> to <?php the_excerpt(); ?>.

But, We don’t want to edit the core files at all. We want to be able to upgrade our theme without our modifications being broken.

The Solution is:

WordPress filters.

In our child theme’s functions.php file, We added this bit of PHP:

// Add filter to the_content
add_filter('the_content', 'my_excerpts');

This tells WP that we want to filter the_content() with our own function.

Here’s what that function looks like:

function my_excerpts($content = false) {

// If is the home page, an archive, or search results
if(is_front_page() || is_archive() || is_search()) :
global $post;
$content = $post->post_excerpt;

// If an excerpt is set in the Optional Excerpt box
if($content) :
$content = apply_filters(‘the_excerpt’, $content);

// If no excerpt is set
else :
$content = $post->post_content;
$excerpt_length = 55;
$words = explode(‘ ‘, $content, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, ‘…’);
$content = implode(‘ ‘, $words);
endif;
$content = ‘<p>’ . $content . ‘</p>’;

endif;
endif;

// Make sure to return the content
return $content;

}

Now, we’ve changed the instances of the_content() that we want to excerpts. This example doesn’t strip the tags of the excerpt like the default WordPress function, but we can easily change that by changing this line:

$content = $post->post_content;
to:
$content = $post->post_content;
$content = strip_shortcodes($content);
$content = str_replace(']]>', ']]&gt;', $content);
$content = strip_tags($content);

There may be a better way to filter the_content(), and we’d love to see what you can come up with if so.

Filed in Wordpress • Tags:

How to Using wordpress shortcodes to show members-only content

By pediabook - Last updated: Sunday, March 7, 2010

This tutorial will cover three methods for setting up shortcodes to use in your posts and pages that will allow you to hide or show content depending on who’s viewing it.

Content for users that are not logged in

Many people want to focus on hiding content from this group of users, but you want to start by showing them content. Most traffic to your site will likely be through non-logged in users, so make sure you give this group of people something.

Open your theme’s functions.php file in your favorite text editor. Add this PHP code:

add_shortcode( 'visitor', 'visitor_check_shortcode' );

function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return $content;
return ”;
}

Anytime you write a post/page, add this to only show content to users that are not logged in:

[visitor]

Some content for the people just browsing your site.

[/visitor]

You should also note that this content will be added to your feeds. The next two techniques will hide content from feed readers and others on your site.

Showing content to logged-in users

Now, you’ll see how to show content only to users that are logged into your site. This will be hidden from all other users and not shown in your feeds.

Add this code to your theme’s functions.php file:

add_shortcode( 'member', 'member_check_shortcode' );

function member_check_shortcode( $atts, $content = null ) {
if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
return $content;
return ”;
}

Then, all you must do is add some content in between your [member] tags when writing a post/page like so:

[member]

This is members-only content.

[/member]

Showing content depending on user’s capability

This bit of code is my favorite because it allows me to check for users based on their capabilities. You can use something like the Role Manager plugin to create custom capabilities or just use the default WordPress capabilities to check against.

Add this code to your theme’s functions.php file:

add_shortcode( 'access', 'access_check_shortcode' );

function access_check_shortcode( $attr, $content = null ) {

extract( shortcode_atts( array( ‘capability’ => ‘read’ ), $attr ) );

if ( current_user_can( $capability ) && !is_null( $content ) && !is_feed() )
return $content;

return ”;
}

Now, we’re going to show content only for someone that has the capabilitiy of switch_themes (an administrator in the default WordPress setup):

[access capability="switch_themes"]

The currently logged-in user is a god on this site.

[/access]

The default capability is read in the code above, so adding this will give access to the content to any logged-in user that can read (this is likely everyone):

[access]

You can read, right?

[/access]

How to show content if user doesn’t meet requirements

These shortcodes are great if you just want to hide something. But, if you want to show a message for people that you’re hiding content from, you’ll need to make a small change.

In each of the code snippets above, the line just before the last is:
return '';
That means nothing will be shown if the user doesn’t meet the requirements defined by the shortcode. In order to leave them a message, change it to this:
return 'Sorry, but you cannot access this content without...';

Set up your members-only content

Of course, this tutorial hasn’t told you how to get users to sign up. You’ll have to figure that out all on your own. Once they do, you can show whoever whatever content you want.

This is so much nicer than having to use a PHP plugin just to check for a user’s info within a post.

Filed in Wordpress • Tags: ,

How to Add private notes to WordPress posts

By pediabook - Last updated: Sunday, March 7, 2010

Sometimes, you may need to leave a private note (e.g. only visible to admin) to a post, but there’s no built-in solution in WordPress to do it.

Here’s the code you need to add to your functions.php file:

add_shortcode( 'note', 'live_note' );

function live_note( $atts, $content = null ) {
if ( current_user_can( ‘publish_posts’ ) )
return ‘<div class=”note”>’.$content.’</div>’;
return ”;
}

Once done, simply add the following shortcode in your posts:

[note]
This is a note that only someone can see…
[/note]

Note that the note will be displayed with a <div class=”note”></div> tags, so you can use it to give a specific style to your notes.

Filed in Wordpress • Tags:

How to preset text in the WordPress post editor

By pediabook - Last updated: Sunday, March 7, 2010

There are built-in action and filter hooks that allow us to change things. We’ll be showing you how to use a simple filter to preset text in the WordPress post or page editor. This technique will work with both the visual and HTML editor.

We shouldn’t edit core files. Then what should we edit? Only our theme’s functions.php file. or, we could go through the process of making a plugin, but this is pretty simple stuff.

Open your functions.php in your favorite text editor and input following PHP code:

<?php

add_filter( ‘default_content’, ‘my_editor_content’ );

function my_editor_content( $content ) {

$content = “This is some custom content we’re adding to the post editor.”;

return $content;
}

?>

It’s as simple as that. Just a few lines of code. You could even add in some XHTML if you want.

This will only add your text to new posts. It will not add it to posts that have already been written or saved. And, you can always delete it when writing the post if it’s not needed.

Filed in Wordpress • Tags: ,

How to Get the latest x number of wordpress sticky posts

By pediabook - Last updated: Sunday, March 7, 2010

Some of you may be wanted to know how to query a specific number of sticky posts for the front page.

we’ll assume you already have the loop set up in one of your template files (i.e., home.php, custom-page.php). What we need to do is call up a specific number of stickies.

For this example, we’ll load the two latest sticky posts.

Just place following php code before your loop:

<?php
/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );

/* Sort the stickies with the newest ones at the top */
rsort( $sticky );

/* Get the 2 newest stickies (change 2 for a different number) */
$sticky = array_slice( $sticky, 0, 2 );

/* Query sticky posts */
query_posts( array( ‘post__in’ => $sticky, ‘caller_get_posts’ => 1 ) );
?>

We have to use rsort() to sort the sticky posts in reverse order by post ID (newer posts first). The reason for this is because sticky posts are added to the sticky array according to when they were made sticky, not by ID.

We have to set caller_get_posts to 1. Otherwise, we’d get all stickies added.

Filed in Wordpress • Tags:

Howt to Disable wordpress sidebars without editing template

By pediabook - Last updated: Sunday, March 7, 2010

What we’ll be showing you is two quick and easy ways to disable widget areas using your theme’s functions.php file.

Technically, what we’ll be doing is disabling the widgets, which is basically the same thing when you get right down to it.

This article assumes that your theme sets no default code if no widgets are shown. Otherwise, it is outside the scope of this tutorial.

Removing all widget areas

What we’ll be doing in this first function is disabling all widgets on the home page. First, open your theme’s functions.php file and input following php code:

<?php
add_filter( 'sidebars_widgets', 'disable_all_widgets' );
function disable_all_widgets( $sidebars_widgets ) {
if ( is_home() )
$sidebars_widgets = array( false );
return $sidebars_widgets;
}
?>

This is a function that will remove widget areas from our home page. Well, you might have other pages, posts, archives, or whatever in mind that you want to disable widget areas on. In that case, you need to look up the appropriate WordPress conditional tag.

Removing a single widget area

Let’s suppose your theme has widgets in multiple places. Let’s further suppose that you want to remove only the footer widget area on single posts. This will be basically the same thing as above, but you need to know the ID of the widget area. This will be something you’ll either have to find in your theme’s code or ask your theme author about.

For the sake of this tutorial, the ID of our footer widget area is simply footer. We’ll create a new function for this as well. Add this to your theme’s functions.php file.

<?php
add_filter( 'sidebars_widgets', 'disable_footer_widgets' );
function disable_footer_widgets( $sidebars_widgets ) {
if ( is_single() )
$sidebars_widgets['footer'] = false;
return $sidebars_widgets;
}
?>

Filed in Wordpress • Tags: