Categories
Code Uncategorized

Flot – sexy graphs created in JavaScript

I recently had the opportunity to speak at the DC jQuery User Group during an entire meetup dedicated to charts and graphs. Russel Heimlich presented on Google Charts and Chris Bannon presented on Wijmo while I had the honor to present on Flot.

When AddThis switched to Flot, I had a chance to build some really cool charts. This was my opportunity to get some more developers interested in Flot. You can check out my slides and my Sample Code. If you’re looking for more information, check out Flot on Github or the old Google Code site.

Categories
Code Uncategorized

Always check your diffs

One habit that I’ve gotten into that has saved me from looking like an idiot nearly as often is to always look at a diff before I commit. I don’t do this as much in Mercurial, but with SVN when you do automatic deployments to testing servers, a stray alert in your javascript or var_dump in your php can screw up other people’s work. I wrote a small bash script to make it easier for me to check my diffs. Feel free to make it your own. If you have any suggestions for improving it, I’m always looking for ways to improve my dev process. I call it difff for:

Differentiate
Improvements
From
F***ing
Failures

[bash]
difff() {
svn diff > ~/diff.diff
vim ~/diff.diff
}
[/bash]

EDIT: Make sure to check out the comments below to see Jon Cave’s take on this.

Categories
Code Programming Uncategorized WordPress

Adding a twitter box to the Profile page in WordPress

I haven’t used AIM in years and I haven’t used Yahoo Messager in even longer.  What I do use is twitter.  Therefore, having a box in the WordPress admin for twitter name is better and with some code I wrote for the , super easy.

[php]
add_filter(‘user_contactmethods’, ‘jorbin_user_contactmethods’);

/**
* Removes AIM, YAHOO, and Jabber boxes from profile page and adds Twitter in it’s place
*
*/
function jorbin_user_contactmethods($user_contactmethods){
$new_user_contactmethods[‘twitter’] = __(‘Twitter’);

return $new_user_contactmethods;
}
[/php]

Filtering user_contactmethods is one of the easiest things you can do. All you need to do is return an array where the key is the code based name you want and the value is the public facing name. Easy. As. Pie.

Categories
Code Programming Uncategorized WordPress

Add Role

This simple plugin, built for my WordPress DC Presentation on the Users API adds a role called link master, that gets the capability to edit links and also read private posts and pages and also removes the ability for editors to manage categories.

Categories
Code Programming Uncategorized WordPress

Personalized Read More

Want to make the read more link in WordPress call out the user if they are logged in?  As part of my DC WordPress presentation, i showed that it’s easy as pie and only about 10 lines of code.

Categories
Code Programming Uncategorized WordPress

WordPress Shortcodes for Users and Non Users

One of the plugins I wrote for the DC WordPress group’s meeting on Users / Roles / Capabilities demonstrates how to create shortcode that makes some content only visable by users with a certain capability and other content only visable to non logged in viewers.  You can download Shortcodes for Discrimination (along with the other three ) from google code.  Beyond the jump, I go over the code and an explanation of how it works.

Categories
Code Uncategorized WordPress

Remove all default header images in a Twenty Ten Child Theme

Despite the beauty of (and cool stories behind) the default images in twenty ten, If you’re building a twenty ten child theme, there is a chance that you won’t want them to appear as options on the custom header screen. This simple snippit of code in your child theme’s functions.php file will deregister the default images. If you use this, I strongly encourage you to add your own in replacement. Check out my 2010 child theme tutorial to learn more about extending twenty ten.

[php]

function jorbin_remove_twenty_ten_headers(){
unregister_default_headers( array(
‘berries’,
‘cherryblossom’,
‘concave’,
‘fern’,
‘forestfloor’,
‘inkwell’,
‘path’ ,
‘sunset’)
);
}

add_action( ‘after_setup_theme’, ‘jorbin_remove_twenty_ten_headers’, 11 );

[/php]

That’s all. Happy Coding!

Categories
Code Uncategorized WordPress

How to remove a nav menu location in WordPress 3.0.0

This function is in WordPress 3.0.1. You can now just call unregister_nav_menu and don’t need to put it in your child theme

One of the great additions to WordPress 3.0 is navigation menus.  One of the aspects that it contains is the ability for a theme to register a nav menu location and for the user to assign any menu they want to that location.  GREAT!  Except that a missing piece of functionality is the ability for Child Themes to remove a location that it might no longer want.  I’ve created a ticket and submitted a patch, but if you need that functionality now, just add the code below to you child theme’s functions.php file:
[php]
if (! function_exists(‘unregister_nav_menu’) ):
function unregister_nav_menu( $location ) {
global $_wp_registered_nav_menus;

if (is_array($_wp_registered_nav_menus) && array_key_exists( $location, $_wp_registered_nav_menus ) ){
unset( $_wp_registered_nav_menus[$location] );
return true;
}

return false;

}
endif;
[/php]

Then all you need to do is call:
[php]
//Remove Twenty Ten Default Menu Location
unregister_nav_menu(‘primary’);[/php]
At some point after the nav menu is registered. I like to use the init hook.

Categories
Code Uncategorized WordPress

PHPXref for WordPress, BuddyPress, bbPress and Thematic: Local and Updated

I often am working on a variety of WordPress, BuddyPress, and BBPress projects and enjoy working from a lot of not internet connected locations such as trains, buses, parks and the occasional rooftop. As such it’s very handy for me to a local version of PHPXref documents for the current trunk of these products. I’ve written a handy bash script to handle this for me.

[bash]
#!/bin/bash

# define locations
PHPXREFLOCATION=’/full/path/to/phpxref/folder/with/no/trailing/slash’

# check our internet connection
wget -q –tries=10 –timeout=5 http://core.svn.wordpress.org/trunk/ -O /tmp/wordpress.svn &> /dev/null
if [ ! -s /tmp/wordpress.svn ];then

# Remove old version
rm -r $PHPXREFLOCATION/output/*

# SVN up each piece
cd $PHPXREFLOCATION/source/thematic
svn up
cd $PHPXREFLOCATION/source/wordpress
svn up
cd $PHPXREFLOCATION/source/bbpress
svn up
cd $PHPXREFLOCATION/source/buddypress
svn up
cd $PHPXREFLOCATION/

# Build our new phpxref
perl phpxref.pl

#remove the tmp file so it’s not there for next time
rm /tmp/wordpress.svn

fi
[/bash]

I have a cron set up to run this at a few intervals that are least likely to bother me (currently 5am and 4pm). I hope this script helps you out. If you have a similar or better one, please let me know.

Categories
Code Uncategorized WordPress

Dirty Ten, a Twenty Ten Child Theme using Output Buffers

After making Thirty Ten, my first Twenty Ten Child Theme, Devin Price asked for an example of a child theme that used output buffers.I’m not a big fan of using output buffers in my themes, but it’s a worthwhile trick to have in your bag. There are two main reasons for this:

  1. It makes code harder to read.
  2. It makes it so html isn’t being sent immediately to the browser, which often means your loading time increases

That being said, you shouldn’t be afraid to use them. Just make sure you are documenting your code (which you should be doing anyway), and that you are using some sort of caching so you don’t have to regenerate the page on every load (also something you should be doing anyway).

I’m going to do a quick example and show you how to make a twenty-ten child theme that adds a widget area above the image and below the Site title and Description. To do this we are going to start our buffer at wp-head and then use the filters used for the header image as an action to end it and add our widget.

[php]<br><br>add_action(‘wp_head’, ‘head_buffer’);
/**
* Start our buffer and Use filters as actions and add them to end our buffer depending on the header image
*
*/
function head_buffer(){
ob_start();
if ( is_singular() && has_post_thumbnail( $post-&gt;ID ) && $width > HEADER_IMAGE_WIDTH ) :
add_filter(‘post_thumbnail_html’, ‘clear_buffer_post_thumbnail’);
else :
add_filter(‘theme_mod_header_image’, ‘clear_buffer_header_image’);
endif;
}

/**
* Our filter is really an action
*
*/
function clear_buffer_header_image($html){
clear_buffer();
return $html;
}

/**
* The Buffer ender. Clear it, add to it, echo it.
*
* Replace the blank space after our site description div with our header widget. This is called by both
* of our action psuedo filters
*/
function clear_buffer(){
$buffer = ob_get_clean();
$header = preg_replace(‘#&lt;div id="site-description"&gt;.*&lt;/div&gt;#’,"$0".new_header_widget() ,$buffer);
echo $header;
}

function clear_buffer_post_thumbnail($html){
clear_buffer();
return $html;
}
[/php]

Our next step is to register our widget and add a function that will add the content of our widget.

[php]
/**
* This adds the content of our widget. Could we use a new sidebar-*.php file? yes, but it’s easier to keep all of this in one file
*
*/

function new_header_widget(){
ob_start();
?>
<div id="headerwidget" class="widget-area">
<ul class="xoxo">
<?php if ( ! dynamic_sidebar( ‘header-widget-area’ ) ) : // begin Header widget area ?>
<li id="search" class="widget-container widget_search">
<?php get_search_form(); ?&>
</li>
<li id="archives" class="widget-container">
<h3 class="widget-title"&><?php _e( ‘The Maker’, ‘dirtyten’ ); ></h3>
</ul>
&lt;li&gt;&lt;a href=’http://aaron.jorb.in’&gt;Wordpress Developer Aaron Jorbin&lt;/a&gt; creates themes and plugins for both the masses and the individual&lt;/li&gt;<br> &lt;li&gt;&lt;a href=’http://aaron.jorb.in/thirtyten’&gt;Thirty Ten&lt;/a&gt; is a Twenty Ten Child theme designed to teach you how to make child themes&lt;/li&gt;<br> &lt;/ul&gt;<br> &lt;/li&gt;<br><br> &lt;li id="meta" class="widget-container"&gt;<br> &lt;h3 class="widget-title"&gt;&lt;?php _e( ‘Meta’, ‘twentyten’ ); ?&gt;&lt;/h3&gt;<br> &lt;ul&gt;<br> &lt;?php wp_register(); ?&gt;<br> &lt;li&gt;&lt;?php wp_loginout(); ?&gt;&lt;/li&gt;<br> &lt;?php wp_meta(); ?&gt;<br> &lt;/ul&gt;<br> &lt;/li&gt;<br>&lt;?php endif; // end Header widget area ?&gt;<br> &lt;/ul&gt;<br> &lt;/div&gt;&lt;!– #headerwidget .widget-area –&gt;<br>&lt;?<br> $widget_area = ob_get_clean();<br> return $widget_area;<br>}<br><br>add_action( ‘init’, ‘dirtyten_widgets_init’ );<br><br>/**<br>* Register our widget<br>*<br>*/<br>function dirtyten_widgets_init() {<br> // Area 1<br> register_sidebar( array (<br> ‘name’ =&gt; ‘Header Widget Area’,<br> ‘id’ =&gt; ‘header-widget-area’,<br> ‘description’ =&gt; __( ‘The widget area in the Header. Optimized to use three widgets’ , ‘dirtyten’ ),<br> ‘before_widget’ =&gt; ‘&lt;li id="%1$s" class="widget-container %2$s"&gt;’,<br> ‘after_widget’ =&gt; "&lt;/li&gt;",<br> ‘before_title’ =&gt; ‘&lt;h3 class="widget-title"&gt;’,<br> ‘after_title’ =&gt; ‘&lt;/h3&gt;’,<br> ) );<br><br>}<br><br>[/php]

And that’s about it. It’s 111 lines after comments and white space. I’d strongly encourage you to add your own widgets, unless you want to be advertising for me ;).

The only other part is our css to style the widgets. This is also simple:

[css]<br>@import url('../twentyten/style.css');<br><br>#headerwidget{<br>clear:both;<br>width: 100%;<br>}<br><br>#headerwidget ul li{<br>float: left;<br>width: 280px;<br>margin-right: 30px;<br><br>}<br>[/css]

You can check out Dirty Ten, or Download it and use it.  You can also clone it from the mercurial repository.