Posted on May 24 , 2009 In Uncategorized

How to preset text in the WordPress post editor

Okay, I’m going to be straight-up with you guys, I’m coding a WordPress plugin that allows users, once they register, to add a certain amount of text that will be posted after they comment on a WordPress blog. Well, running with this idea, I had to figure out a way first, to preset text in a WordPress post editor, which would then lead me to the comments section.

While scouring the interwebs to no prevail (thanks Google), I decided to troll around the “official” WordPress forums, and found a link to who else, but the WordPress guru’s website, Justin Tadlock. Well, without a doubt, he had just the thing that is needed for preseting text in WordPress so that you don’t have to type it, and all you have to do is edit your theme’s functions.php file.

Okay, so edit your theme’s functions.php file, and post in the following text code:

<?php

add_filter( 'default_content', 'my_editor_content' );

function my_editor_content( $content ) {

	$content = "This is some custom content I'm adding to the post editor because I hate re-typing it.";

	return $content;
}

?>

And that’s it! Just edit the text, add a little xhtml if you want to, and there you have it! The problem seems so daunting and questionable at first, and you have a simple answer just like this. Well, thanks Justin, and hopefully this will help me with my plugin!

Posted on May 22 , 2009 In Uncategorized

WordPress shortcode: Display content to registered users only

Justin Tadlock has another great tutorial for WordPress users, and it is how to hack/modify your WordPress template so that content on the site will only be available to registered users only.

First, place the following code in 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, you can use the following shortcode tags so that you can put certain content/links, etc within the availability of that of registered users:

[member]
This text will be only displayed to registered users.
[/member]

There you have it! Using that code, you can make anything on your blog only viewable by registered users.

Posted on May 17 , 2009 In Uncategorized

Wordpress Recent Posts from specific category

Ready WP Themes has made a nicce post about how to show the most recent posts from a specific category, instead of just recent posts in general. Well, here it is:

This is the regular code that we use, with 6 posts being shown from every category:

<h2>Recent Posts</h2>
<ul>
<?php get_archives('postbypost', 6); ?>
</ul>

Now, we’re going to replace all of that code with:

<ul>
<?php $recent = new WP_Query("cat=1&showposts=10"); while($recent->have_posts()) : $recent->the_post();?>
<li><a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
</ul>

And as you probably now, you can change the category from which your posts are being pulled, by changing the code that says “cat=1″, and the amount of posts being shown by changing “showposts=10″, and change 10 to the number that you want.

Posted on May 13 , 2009 In Uncategorized

Create a Static Front Page for WordPress

Today, I’m going to do something a little bit different than my usual simple tricks. While scouring the internet, I found this video on how to create a static front page in WordPress, and upon viewing it, thought I would share it with you guys. Enjoy!

Posted on May 11 , 2009 In Uncategorized

Display the total number of users of your WordPress blog

Got a lot of users on your blog…or a bunch of porn/spam bots? Well, if you’d like to show off the number of registered userse on your WordPress blog to show how popular you are, here’s some simple code to achieve just that.

Just put the following code in any file in your WordPress theme, and voila! Your number of users will be displayed:

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

Thanks WPRecipes!

Posted on May 9 , 2009 In Uncategorized

Show Recent Comments for Post On Front Page

If your blog gets a lot of comments, or just gets comments in general, you might want to encourage more comments or show people the most recent comment on a certain post. Well, while searching the interwebs to no prevail for this, I decided to mess around and see what I could come up with.

Pretty much, all you’ve got to do is copy and paste the following code anywhere in your index.php file, just make sure that it’s inside the loop, but preferably after <?php the_content(); ?>

<!-- recent comment of each post -->

<div class="recent-comment">
 <?php
	$comment_array = array_reverse(get_approved_comments($wp_query->post->ID));
	$count = 1;
 ?>

<?php if ($comment_array) {  ?>
	<span class="comment"> <?php comments_number('No comment','1 comment','% comments'); ?></span> - Latest by:
	<ul class="commentlist">
		<?php foreach($comment_array as $comment){ ?>
			<?php if ($count++ <= 1) { ?>
				<li><?php comment_author_link(); ?> <br /> <?php comment_excerpt(); ?> </li>
			<?php } ?>
		<?php } ?>
	</ul>
<?php } else { ?> <!-- if there was no comment in that post,yet -->
		<span class="comment">No comment so far</span>
<?php } ?>
</div>

<!-- end recent comment-->

There you have it! You can see that I made some divs already for it, so you can go ahead and create those in your theme’s css file, so people can easily distinguish the recent comment from the post. You can also change how many comments are shown on the front page by changing the one on this line:

<?php if ($count++ <= 1) { ?>

To however many comments you wanted to be shown.

Thanks flisterz!

Posted on May 4 , 2009 In Uncategorized

Get short urls for social bookmarking

When you’re twittering or posting links or sending links to friends on sites that restrict how many characters you can type, it can be hard to fit your entire URL in for a blog post if you have the long, fancy, complicated permalinks. Well, here’s a simple WordPress hack thanks to WPrecipes on how to achieve this.

Okay, all we’re going to be doing here is getting the post ID for a shorter URL.

Copy the following code in your single.php file:

<?php echo get_bloginfo('url')."/?p=".$post->ID; ?>

You’re result will look like a URL in this form:

http://problogtuts.com/?p=21

Posted on May 3 , 2009 In Uncategorized

Display the latest author who modified the post

I haven’t run into this too much on blogs, but it is quiet common on forums, and if you can do it on something else, it can be accomplished on WordPress. What we’re going to do now is get some simple code to show the user who last modified the post on the page.

First, you’ve got to copy and paste the code into your theme’s functions.php file:

if (!function_exists('get_the_modified_author')) {
  function get_the_modified_author() {
    global $post;
    if ( $last_id = get_post_meta($post->ID, '_edit_last', true) ) {
      $last_user = get_userdata($last_id);
      return apply_filters('the_modified_author', $last_user->display_name);
    }
  }
}

if (!function_exists('the_modified_author')) {
  function the_modified_author() {
    echo get_the_modified_author();
  }
}

Once you’ve saved that modified file, you can use the following code on any file in your theme to show the latest author who modified a post:

<?php the_modified_author(); ?>

Big thanks to Valentin Brandt for this cool WordPress hack!

Posted on April 30 , 2009 In Uncategorized

First Screenshot of WordPress 2.8 Widgets Options Page!

Well, the first development version of WordPress 2.8 out, there have been a lot of announcements and screenshots out there. What we’ve got here from WPEngineer is the first screenshow on the web of the WordPress 2.8 widgets options page.

Plus, the new widget option pages saves changes via Ajax, so no need for the page to refresh anymore!

Posted on April 24 , 2009 In Uncategorized

Show Username of Logged-in User

Okay, I was extremely confused a couple of days ago, because I searched google for the longest time, and couldn’t find any code on how to show the username of the current user who’s logged in. Well, I searched some forums, looked through the codex, and 10 hours later, there I get it. Well, I thought I’d just have to share it with you guys cuz I’m just that kind of guy.

<?php global $current_user; if ( isset($current_user) ) {     echo $current_user->user_login; } ?>

There, place that anywhere and you’ll get the username shown of the current user logged-in to your WordPress blog. Another idea could be to couple that with the gravatar code I showed in another post, and have a gravatar shown (maybe aligned to the left), with the username on the right. Could be used in a lot of different ways huh?

So why did I want to find this code? Well, keep checking out the site to find out. (It’ll help you to register on this site and get an avatar at gravatar.com as well). :P