Category: WordPress

  • Automattic!

    Starting Monday, April 22nd, I’ll be working full time at Automattic!

    When I first started working at Speck Products, I’d remarked to a friend that I thought I’d be there for good.  I loved the environment, I loved the people, and I said the only reason I’d ever leave is if Automattic ever wanted me and I could spend my days working full time on WordPress — more in a joking way, as I never really expected it to happen.

    Eight plus months later, I find that to be exactly the situation I’m in.

    I can’t find a single thing to gripe about regarding my tenure with Speck.  Everyone there was an utter joy to work with.  Challenges were plentiful to keep me engaged, but never overbearing.  I was kept occupied, but never overburdened.  Everyone was friendly and provided a great atmosphere.

    However, now I’ll get to do something that I count myself as incredibly fortunate for.  I get to spend my days  doing the sort of work that I’ve volunteered my time doing for the past year and a half.  The environment that has been my passion, is now my job.  And I couldn’t be happier.

    I’ll be spending my days on the Jetpack team for Automattic, increasing the tools available to WordPress.org users through WordPress.com by way of the Jetpack plugin.  I’m very excited by the road map we’ve got going forward, and I can’t wait for some of you to see the features that we’ve got in store.

    The best is yet to come.

  • The best phrased response to the current GPL spat between WordCamps and Envato

    As stated by Chip Bennett:

    I will preface my comments by saying that I disagree completely with the approach the WordPress Foundation is taking here. The problem is a disagreement between the WPF and Envato, and developers are merely caught in the crossfire.

    This approach makes developers choose between putting food on the table and being a persona non grata to the WPF, or else risking their legitimate revenue stream, and be in the WPF’s good graces. Unfortunately, for Jake and thousands of developers like him, the WPF’s good graces don’t put food on the table.

    And while the tactic may ultimately work, there are only so many times you can turn the 50-mm barrels on the rank-and-file in the community itself, and not have adverse affects.

    That said, I take issue with Envato’s stance, as well:

    To my mind, it doesn’t make sense that a regular license sold on ThemeForest should give such a buyer the right to on-sell a creator’s work at that volume – if only for the simple reason that volume reselling can significantly reduce demand for the original work.

    You are arbitrarily restricting the ability of your marketplace suppliers to offer their work under the license of their choice. The way I read this, your real concern is that Envato would lose commissions if Themes in their marketplace were offered as 100% GPL, and led to downstream distribution. If that is the real concern, it may or may not be valid, but it is disingenuous to couch such concern as concern for your marketplace sellers.

    If that is *not* the real concern, then I don’t see how any real concern exists. Just let your marketplace sellers *choose* to offer their works under 100% GPL. Put up huge banners decrying the risks of doing so. Strongly suggest that they don’t do so. Rail against the GPL all you want. Make them click through 3 “are you sure?” dialogue boxes.

    But offer the choice.

    I guarantee you that the WordPress Theme developers who opt-in to offering their works under a 100% GPL license do so under full understanding of the license terms, and either disagree with your risk assessment, or have evaluated the risk-reward differently. You don’t need to “protect” them from the license.

    Just offer them the choice.

    This.  A thousand times this.

  • Taxonomy List Page

    Have you ever wanted to have the archive page for a custom taxonomy be a listing of the taxonomies? Easy! Just create a Page and have the slug match the base slug for the custom taxonomy! Then on that page, you could just create the index by hand, or use THIS shortcode!

    In short, if you just call it as [taxonomy-list taxonomy="my-taxonomy"] it will pull in a list of all of the taxonomies available! You can tweak some other options, but that’s the gist.

    The bulk of the code, however, is to let you pull in an image for each taxonomy item!

    It does this by grabbing the first post (or other post type — you can specify this via the post_type argument) in that taxonomy that has a featured image, and using that as the featured image for the category! Easy peasy!

    Just use that method via [taxonomy-list taxonomy="my-taxonomy" images="yup"] — or anything else that doesn’t evaluate to empty.

    Also threw in some caching for good measure. It shoves the result in a transient for an hour.

    There’s probably some other bits in here worth fleshing out — proper usage for title_li and depth, when images is turned on, etc etc, but for now, it serves my needs, and hopefully some of yours as well!

    
    add_shortcode( 'taxonomy-list', 'my_taxonomy_list' );
    function my_taxonomy_list( $atts ) {
    	$args = shortcode_atts( array(
    		'taxonomy' => 'category',
    		'post_type' => 'post',
    		'title_li' => '',
    		'depth' => 1,
    		'hide_empty' => 1,
    		'images' => 0,
    	), $atts );
    	
    	$get_posts_args = array(
    		'post_type' => $args['post_type'],
    		'number posts' => 1,
    		'meta_query' => array(
    			array(
    				'key' => '_thumbnail_id',
    				'compare' => 'EXISTS',
    			),
    		),
    	);
    	
    	$key = 'my_taxonomy_list-' . md5( serialize( $args ) );
    	if ( false === ( $result = get_transient( $key ) ) ) {
    		ob_start();
    		?>
    		<ul class="taxonomy-list taxonomy-<?php echo $args['taxonomy']; ?>-list" data-taxonomy="<?php echo $args['taxonomy']; ?>">
    			<?php
    			if ( empty( $args['images'] ) ) {
    				wp_list_categories( $args );
    			} else {
    				$cats = get_categories( $args );
    				if( empty( $cats ) ) break;
    				foreach( $cats as $cat ) {
    					$img = '';
    					$get_posts_args[$args['taxonomy']] = $cat->slug;
    					if ( $posts = get_posts( $get_posts_args ) ) {
    						$img = get_the_post_thumbnail( $posts[0]->ID );
    					}
    					?>
    					<li><a href="<?php echo get_term_link( $cat ); ?>">
    						<?php echo $img; ?>
    						<?php echo $cat->name; ?>
    					</a></li>
    					<?php
    				}
    			}
    			?>
    		</ul>
    		<?php
    		$result = ob_get_clean();
    		set_transient( $key, $result, HOUR_IN_SECONDS );
    	}
    	return $result;
    }
    
    
  • Draw Something Cool

    So I’ve had a lot of awesome feedback for the “Draw Something Cool” bit that I’ve added to my Contact Form.  It’s in actuality just the Signature add-on for GravityForms!

    That being said, here are some of the best images that I’ve had people submit through it thus far:

    1211108976534

  • Displaying Custom Post Type Archive on Your WordPress Homepage

    Here’s how to do it!

    add_action( 'pre_get_posts', 'my_query_mods' );
    function my_query_mods( $query ){
        if( is_admin() ) return;
        if( is_main_query() && is_home() )
            $query->set( 'post_type', 'texture' );
    }
    
  • Need a category index page?

    Why not just use a normal page?

    Oh, sure, you want a dynamically loading list of the categories.

    Try this on for size:

    
    add_shortcode( 'taxonomy-list', 'my_taxonomy_list' );
    function my_taxonomy_list( $atts ) {
        $args = shortcode_atts( array(
            'taxonomy' => 'category',
            'title_li' => '',
            'depth' => 1,
            'hide_empty' => 1,
        ), $atts );
    
        ob_start();
        ?>
            <ul class="taxonomy-list taxonomy-<?php echo $args['taxonomy']; ?>-list" data-taxonomy="<?php echo $args['taxonomy']; ?>">
                <?php wp_list_categories( $args ); ?>
            </ul>
        <?php
        return ob_get_clean();
    }
    
    
  • How to change post thumbnail crop position in WordPress WITHOUT HACKING CORE

    DISCLAIMER: This requires WordPress 3.4 to work correctly. The filter was added based on Trac Ticket #15989

    Okay, to start, here is the function that we’re going to be working with, from ~/wp-includes/media.php :

    /**
     * Retrieve calculated resized dimensions for use in imagecopyresampled().
     *
     * Calculate dimensions and coordinates for a resized image that fits within a
     * specified width and height. If $crop is true, the largest matching central
     * portion of the image will be cropped out and resized to the required size.
     *
     * @since 2.5.0
     * @uses apply_filters() Calls 'image_resize_dimensions' on $orig_w, $orig_h, $dest_w, $dest_h and
     *		$crop to provide custom resize dimensions.
     *
     * @param int $orig_w Original width.
     * @param int $orig_h Original height.
     * @param int $dest_w New width.
     * @param int $dest_h New height.
     * @param bool $crop Optional, default is false. Whether to crop image or resize.
     * @return bool|array False on failure. Returned array matches parameters for imagecopyresampled() PHP function.
     */
    function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) {
    
    	if ($orig_w <= 0 || $orig_h <= 0)
    		return false;
    	// at least one of dest_w or dest_h must be specific
    	if ($dest_w <= 0 && $dest_h = $orig_w && $new_h >= $orig_h )
    		return false;
    
    	// the return array matches the parameters to imagecopyresampled()
    	// int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
    	return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
    
    }
    

    Some people have looked at this and felt that there was no way to override the $s_y = floor( ($orig_h - $crop_h) / 2 ); line without hacking core. Well, that’s iffy, but if you scroll up above, you’ll see something that lets you basically ‘short-circuit’ the function in question:

    // plugins can use this to provide custom resize dimensions
    $output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop );
    if ( null !== $output )
    	return $output;
    

    So here’s our function to make all crops start at (0,0) — customize it as you like:

    function my_awesome_image_resize_dimensions( $payload, $orig_w, $orig_h, $dest_w, $dest_h, $crop ){
    
    	// Change this to a conditional that decides whether you 
    	// want to override the defaults for this image or not.
    	if( false )
    		return $payload;
    
    	if ( $crop ) {
    		// crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
    		$aspect_ratio = $orig_w / $orig_h;
    		$new_w = min($dest_w, $orig_w);
    		$new_h = min($dest_h, $orig_h);
    
    		if ( !$new_w ) {
    			$new_w = intval($new_h * $aspect_ratio);
    		}
    
    		if ( !$new_h ) {
    			$new_h = intval($new_w / $aspect_ratio);
    		}
    
    		$size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
    
    		$crop_w = round($new_w / $size_ratio);
    		$crop_h = round($new_h / $size_ratio);
    
    		$s_x = 0; // [[ formerly ]] ==> floor( ($orig_w - $crop_w) / 2 );
    		$s_y = 0; // [[ formerly ]] ==> floor( ($orig_h - $crop_h) / 2 );
    	} else {
    		// don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
    		$crop_w = $orig_w;
    		$crop_h = $orig_h;
    
    		$s_x = 0;
    		$s_y = 0;
    
    		list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
    	}
    
    	// if the resulting image would be the same size or larger we don't want to resize it
    	if ( $new_w >= $orig_w && $new_h >= $orig_h )
    		return false;
    
    	// the return array matches the parameters to imagecopyresampled()
    	// int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
    	return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
    
    }
    add_filter( 'image_resize_dimensions', 'my_awesome_image_resize_dimensions', 10, 6 );
    
  • Create New Admin Account in WordPress via FTP

    Another handy little snippet for WordPress …

    Have you ever had a client need help on their WordPress site, and have FTP access, but not actually give you a WordPress account to use? Just paste this snippet into their current theme’s functions.php file, or add it to a new file in their ~/wp-content/mu-plugins/ folder, and WordPress will automatically create an admin account for you to use!

    Oh — and don’t forget to change the credentials that are included to your own. If there is already an account with the username or email address specified, it will fail and not do diddly squat.

    
    function add_admin_acct(){
    	$login = 'myacct1';
    	$passw = 'mypass1';
    	$email = 'myacct1@mydomain.com';
    
    	if ( !username_exists( $login )  && !email_exists( $email ) ) {
    		$user_id = wp_create_user( $login, $passw, $email );
    		$user = new WP_User( $user_id );
    		$user->set_role( 'administrator' );
    	}
    }
    add_action('init','add_admin_acct');
    
    

    Remember … with great power comes great responsibility. Don’t abuse it.

  • Toggle All Checkboxes with jQuery

    Just a little snippet I worked up that may be useful to someone …

    jQuery(document).ready(function($){
    $('div#checkall-wrapper input[type=checkbox]').click(function(){
    	if( $(this).attr('checked') ){
    		$('tdiv#wraparound-targets input[type=checkbox]').attr('checked','checked');
    	}else{
    		$('div#wraparound-targets input[type=checkbox]').removeAttr('checked');
    	}
    });
    });
    

    Make sense?

  • Ndizi Project Management 0.9.6 PRE-RELEASE

    So, largely still a work-in-progress, I overhauled how most of the functions are written and abstracted many of the grunt work out. The client front-end page probably breaks like crazy, I’ll be getting that tomorrow, but for anyone who wants to take a look at it as-is, here goes:

    Ndizi Class 0.9.6 Snapshot

    The code in the back is a -lot- cleaner. I’ve abstracted alot of the form creation into sub-functions, as well as tabular display. Also built in a capability for filters, and added ‘active’ properties to clients and projects. The structure is in place to add comments and file attachments to things, but I need to sort out use-cases and how to actually display them. I’m currently thinking maybe a lightbox pop-up. Not sure.

    Any bug reports appreciated. Feature suggestions also welcome, but they’ll be addressed in the coming week or two.