Floating sidebar for twentythirteen

The javascript code to make the sidebar float and stick to screen as you scroll down. This code is specific to twentythirteen wordpress theme and most probably won’t work with any other theme.

I didn’t like it when the sidebar area goes blank as i scroll down the page. The content in the main content area(towards left) is more than the content(widgets) in sidebar(towards right) so as one scrolls down the page, the sidebar area becomes empty.

The following javascript does the trick. As you scroll down the page and scroll past the bottom of sidebar, the sidebar sticks to the bottom right of the page. You keep going down and it stays there. And when you scroll up to a sufficient height, it again acquires its normal position. The script also prevents sidebar from overlapping with ‘header’ and ‘footer’ areas.

Any demo ? : yes, this very page(or any other page on this website) is a live demo of the script. Try scolling!! Its more apparent on longer pages like home page.
This script results in nice dirty trick on this website. On a fresh page reload, you don’t see ads in the sidebar becuase that widget at the end of the sidebar and becomes visible only when you scroll down. And once you scroll down, only those ads are visible, and you can’t get rid of them :) :) ..

Anyway so here’s the javascript, you can save it in any of the existing javascript of your theme or save it as a new file and enqueue the file properly:

That’s it here. I didn’t bother to check it in IE! so be warned :)

Load more activities on scroll

Modify your buddypress theme to load more activity posts when user scrolls down!
All you need to do is to add the following javascript in any of your javascript files (and make sure that javascript file is included! lol). The script loads more content by ajax and appends at the bottom of the page. The server side code to process the ajax request is already there in buddypress. God bless those guys at automattic!

That’s it here.

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:

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):

    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:

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

Buddypress remove email id from private messages

Add the following code in functions.php of your theme

It’ll remove all email id’s from message subject and content. While replying(ajax-no postback) the message is displayed to sender unaltered, but don’t panic, it actually is removed and receiver will never see the email id.

To avoid any complications, while copying the code first click on the below link which will appear once you hover over the code
copying code in crayon syntax higlighter

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.

To avoid any complications, while copying the code first click on the below link
copying code in crayon syntax higlighter
which will appear once you hover over the code

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.

Generate youtube embed code from url

php Code to convert Youtube url into embed code. Pass in the youtube video url and generate the embed code to embed the youtube video on your website.

Without talking any more, here’s the php code:

The main method youtubeEmbedFromUrl uses helper methods extractUTubeVidId (description here) and generateYoutubeEmbedCode

How to use:

Just call the method youtubeEmbedFromUrl with 3 parameters:

  1. youtube_url : the url of the youtube video
  2. width : desired width of the youtube player
  3. height : desired height of the youtube player

e.g:

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

Will soon update a c# version of the code

Get youtube video id from url

Extract you tube video id from youtube url. The following code retrieves the video id from youtube url.

It works with only 3 types of youtube url supplied:
Type1: http://www.youtube.com/watch?v=9Jr6OtgiOIw
Type2: http://www.youtube.com/watch?v=9Jr6OtgiOIw&feature=related
Type3: http://youtu.be/9Jr6OtgiOIw

type1 and 2 is acutally one and the same, only the extra paramters

The code:

How to use:

call this function passing a youtube url as parameter:

You can use it on your website: user enters youtube url, you pull out the video id, generate an embed code and embed the youtube video on your website.

That’s it here. Hope you find it useful.
You might as well like the code for generating youtube embed code

Will soon update a c# version of the code

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:

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.

Custom styled checkboxes

Bored of the good old checkbox design?
Time for styling checkbox elements, giving custom design to checkboxes.

custom-styled-checkboxes
First of all, here’s the demo link

What are we doing?

Basically i want to replace this with this

So here’s one way(hack) to do it. Hide the default checkbox, instead use background images for the associated label. Use different images for checked/unchecked state of the checbox. We are not removing the checkboxes completely, those are there, just hidden by css with display:none. So clicking on the associated label does cause checking/unchecking of the checkbox, so its still available in forms post data, no issues there.

The HTML

standard checkbox html with labels, nothing fancy here

I’ve wrapped those checkboxes in ul li just for the purpose of applying one single css rule to all of them. You can use any way you want.

The CSS

Here’s the trick:
We’ll hide the checkboxes, toggle the background image of associated label with css and js. I’ve used this image . 3 states – normal, hover, checked.

A little javascript

Now when user clicks with the intention to check/uncheck the checkbox, we show it visually by adding/removing a css class of ‘checked’. So a simple change event:

We can use css3 adjacent selector in place of the above javascript.

So instead of the above javascript, the following css will also work:

This essentially does the same thing. But, as usual, it won’t work in IE, so lets just go with javascript.

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

Add target blank to user link in recent comments widget

Open author link in new tab in wordpress default recent comments widget.

I just discovered it today itself on webdeveloeprswall. I’ve added a recent comments widget in my sidebar. The widget is a default wordpress widget, many of us use it. But when you click on the author link in the widget(sidebar) the commentor’s url (whatever he/she has provided) open in the same window!! Taking visitors away from your own website.

I immediately felt the need to add a target="_blank" attribute to those links. So here’s the small code snippet i came up with. Just add the below lines of code in your theme’s functions.php file:

and now the author links from recent comments open in new tab, hurray!! :)

Hope you find it useful. Cheers