bbPress Tag Cloud Widget

A very small wordpress plugin. If you’ve been using bbPress plugin, you know that it provides a shortcode to display the topics-tag-cloud. I’ve wrapped that into a small widget, so one can just easily display the tag cloud wherever required with just drag-and-drop.

You can download the plugin form its github repository.
**Note: Github adds the word ‘-master’ into the plugin’s folder name, so uploading the zip file bbpress-tagcloud-widget-master.zip (downloaded from github) directly to install the plugin won’t work. So, just rename the zip file to bbpress-tagcloud-widget.zip.

That’s it here.


bbPress loading more topics with ajax

Created a plugin which allows you to add one ‘load more’ button on your bbPress topics archive. And obviously, that button click loads more topics and appends it at the end of the topics list.

The plugin can be downloaded from here.

You can use it alongside the pagination. The plugin internally loads the same template files to display new topics, so it integrates with your existing theme without any extra effort.

Once you’ve installed and activated the plugin. You need to add 2-3 lines of code in your template file. The file that we will be editing is loop-topics.php. If you’ve not done already, create a folder inside your active theme, and name it as ‘bbpress’, then open the bbPress plugin folder, copy the file templates/default/bbpress/loop-topics.php and paste it inside the new folder that you created in your theme.
Open the file loop-topics.php and look for

<li class="bbp-body" >

An unaltered file will have the following code in that region

<li>

	<?php while ( bbp_topics() ) : bbp_the_topic(); ?>

		<?php bbp_get_template_part( 'loop', 'single-topic' ); ?>

	<?php endwhile; ?>

</li>

We need to add the code for the ‘load-more’ button there. The code that we need to add is:

<?php if( function_exists( 'bbpresslmt_loadmore_button' ) ): ?>
	<?php bbpresslmt_loadmore_button(); ?>
<?php endif; ?>

So after adding this code, our updated file should contain:

<li>

	<?php while ( bbp_topics() ) : bbp_the_topic(); ?>

		<?php bbp_get_template_part( 'loop', 'single-topic' ); ?>

	<?php endwhile; ?>

	<?php if( function_exists( 'bbpresslmt_loadmore_button' ) ): ?>
		<?php bbpresslmt_loadmore_button(); ?>
	<?php endif; ?>

</li>

That’s all we need to do. If you are viewing topics inside any forum, then topics from inside that forum is loaded. If you are viewing all topics list, all topics are loaded. So no configuration required on that part.

Drop in your comments for any issues with the plugin or any other feedback.


Order by date in post meta

Here’s a quick tip to order posts by a date stored in custom fields (postmeta).

The problem

I have a custom field called ‘event-date’ in which, obviously, a date value is stored. Now i want all the posts sorted by that custom field value. How do i do it?

To order posts by custom field, we’d do something like:

<?php
query_posts(array(
	'meta_key'=> 'event_date',
	'orderby'=> 'meta_value',
	'order' => 'DESC'
));
?>

But this won’t work since the value of the postmeta is taken as string and sorted accordingly, which can given unexpected results.

The solution

  1. Add the following code in functions.php of your theme (or at any appropriate place, if you are writing a plugin):
    <?php 
    /* sorts the post based on a date value in custom fields 
     * check: http://webdeveloperswall.com/wordpress/order-by-date-in-post-meta
    */
    function wdw_query_orderby_postmeta_date( $orderby ){
    	$new_orderby = str_replace( "wp_postmeta.meta_value", "STR_TO_DATE(wp_postmeta.meta_value, '%m/%d/%Y')", $orderby );
    	return $new_orderby;
    }
    ?>

    I’ve my date format as ‘mm/dd/yyyy’, if yours is something else, don’t forget to change ‘%m/%d/%Y’ above with a suitable value.

  2. Next, our query_post code will remain the same, but we will add a filter just before this code:
    <?php
    //the filter to sort by date in custom field
    add_filter( 'posts_orderby', 'wdw_query_orderby_postmeta_date', 10, 1);
    query_posts(array(
    	'meta_key'=> 'event_date',
    	'orderby'=> 'meta_value',
    	'order' => 'DESC'
    ));
    ...
    ...
    ...
    //once done, dont forget to remove that filter
    remove_filter( 'posts_orderby', 'wdw_query_orderby_postmeta_date', 10, 1);
    ?>

Thats a quick, dirty way of getting it done! 🙂
That’s all here.

Crop image script

So you have a wordpress website and you have a form where user enters data, you have given an ‘upload image’ option but you want the uploaded images to be cropped for proper display ?
Below is the code that might help you crap crop it.

How to use it

Just paste the above code in functions.php file of your active wordpress theme. Then call the function wdw_crop_img passing proper parameters.

The above function out of the box is ‘wordpress specific’ because line no 23 in the code makes use of wordpress core function to get to the uploads directory (check line no 23, 25, 26 in the code). But you can obviously modifyl those 3 lines to suit to your website’s directory structure and make it work on any non-wordpress php website.

Rest, read the comment section above function declaration to figure out about the parameters passed and output.

Drop in your comment if you find any problem.
That’s it in here. Hope you find it useful.

Change wordpress registration email

The following code allows you to change the default email that wordpress sends after user registers on the website. You can modiy the default email and add your own custom email message and email subject.

The code:

<?php
/** 
* http://webdeveloperswall.com/wordpress/change-wordpress-registration-email 
*Add filter for registration email subject and message
**/
add_filter('wp_mail','my_custom_registration_mail');

function my_custom_registration_mail($email) {
    if (isset ($email['subject']) && substr_count($email['subject'],'Your username and password')>0 ) {
	if (isset($email['message'])) {

		$messg = "Hello,\r\nWe have received your sign-up request on our website. Your details are as follows:\r\n \r\n";
		$messg .= $email['message'];
		$messg .= "You can now login at the link given.\r\n \r\nWe look forward to your active participation on our website. \r\n \r\nkind regds,\r\nWebMaster \r\nYourDomain.com";
		$email['message'] = $messg;
		$email['subject'] = "YourDomain.com - New Member Sign-up.";
	}
    }
    return ($email);
}
?>

What to do with the code ?
Just add the code inside functions.php file of your current theme. And dont forget to change ‘YourDomain.com’ to something useful 🙂

That’s it here. Hope you find it useful.