Cleverness – WordPress Resources

WordPress Gallery with Categories

Recently I needed to recreate a specific gallery format in WordPress.
There are probably better ways to go about it, such as custom post types, but I needed something quick and easy.

The main gallery page needed to have a list of “categories” that were represented by images.
I used a WordPress Page that consisted of:

[gallery size="full" link="custom_url"]

Each “category” was an image that linked to a WordPress Page.

On the category pages, for each gallery item, there needed to be a caption on top of the image, the image with no link, and a description under the image.
For each of these WordPress pages I used:

[gallery size="full" link="none"]

To create this, I merged the code from these two sources, moved the image caption tag to a different location, and added the image description in functions.php.

Sources:

The code below goes in functions.php in your theme directory.

<?php
/* -------------------------------
CUSTOM LINK PER GALLERY IMAGE
------------------------------- */
               function rt_image_attachment_fields_to_edit( $form_fields, $post ) {
                       $form_fields["rt-image-link"] = array( "label" => __( "Custom Link" ),
                                                              "input" => "text", // default
                                                              "value" => get_post_meta($post->ID, "_rt-image-link", true ), //"helps" => __("To be used with special slider added via [rt_carousel] shortcode."),
                       );
                       return $form_fields;
               }

               add_filter( "attachment_fields_to_edit", "rt_image_attachment_fields_to_edit", null, 2 );

               function rt_image_attachment_fields_to_save( $post, $attachment ) {
                       // $attachment part of the form $_POST ($_POST[attachments][postID])
                       // $post['post_type'] == 'attachment'
                       if ( isset( $attachment['rt-image-link'] ) ) {
                               // update_post_meta(postID, meta_key, meta_value);
                               update_post_meta( $post['ID'], '_rt-image-link', $attachment['rt-image-link'] );
                       }
                       return $post;
               }

               add_filter( "attachment_fields_to_save", "rt_image_attachment_fields_to_save", null, 2 );

/* -------------------------------
MODIFIED CORE FUNCTION
------------------------------- */
add_shortcode( 'gallery', 'geekee_gallery_shortcode' );

               /**
                * The Gallery shortcode.
                *
                * This implements the functionality of the Gallery Shortcode for displaying
                * WordPress images on a post.
                *
                * @since 2.5.0
                *
                * @param array $attr Attributes attributed to the shortcode.
                * @return string HTML content to display gallery.
                */
function geekee_gallery_shortcode( $attr ) {
       global $post, $wp_locale;

       static $instance = 0;
       $instance++;

       // Allow plugins/themes to override the default gallery template.
       $output = apply_filters( 'post_gallery', '', $attr );
       if ( $output != '' ) return $output;

       // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
       if ( isset( $attr['orderby'] ) ) {
               $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
               if ( !$attr['orderby'] ) unset( $attr['orderby'] );
       }

       extract( shortcode_atts( array( 'order'      => 'ASC',
                                       'orderby'    => 'menu_order ID',
                                       'id'         => $post->ID,
                                       'itemtag'    => 'dl',
                                       'icontag'    => 'dt',
                                       'captiontag' => 'dd',
                                       'columns'    => 3,
                                       'size'       => 'thumbnail',
                                       'include'    => '',
                                       'exclude'    => '' ), $attr ) );

       $id = intval( $id );
       if ( 'RAND' == $order ) $orderby = 'none';

       if ( !empty( $include ) ) {
               $include      = preg_replace( '/[^0-9,]+/', '', $include );
               $_attachments = get_posts( array( 'include'        => $include,
                                                 'post_status'    => 'inherit',
                                                 'post_type'      => 'attachment',
                                                 'post_mime_type' => 'image',
                                                 'order'          => $order,
                                                 'orderby'        => $orderby ) );

               $attachments = array();
               foreach ( $_attachments as $key => $val ) {
                       $attachments[$val->ID] = $_attachments[$key];
               }
       } elseif ( !empty( $exclude ) ) {
               $exclude     = preg_replace( '/[^0-9,]+/', '', $exclude );
               $attachments = get_children( array( 'post_parent'    => $id,
                                                   'exclude'        => $exclude,
                                                   'post_status'    => 'inherit',
                                                   'post_type'      => 'attachment',
                                                   'post_mime_type' => 'image',
                                                   'order'          => $order,
                                                   'orderby'        => $orderby ) );
       } else {
               $attachments = get_children( array( 'post_parent'    => $id,
                                                   'post_status'    => 'inherit',
                                                   'post_type'      => 'attachment',
                                                   'post_mime_type' => 'image',
                                                   'order'          => $order,
                                                   'orderby'        => $orderby ) );
       }

       if ( empty( $attachments ) ) return '';

       if ( is_feed() ) {
               $output = "\n";
               foreach ( $attachments as $att_id => $attachment ) $output .= wp_get_attachment_link( $att_id, $size, true ) . "\n";
               return $output;
       }

       $itemtag    = tag_escape( $itemtag );
       $captiontag = tag_escape( $captiontag );
       $columns    = intval( $columns );
       $itemwidth  = $columns > 0 ? floor( 100 / $columns ) : 100;
       $float      = is_rtl() ? 'right' : 'left';

       $selector = "gallery-{$instance}";

       $gallery_style = $gallery_div = '';
       if ( apply_filters( 'use_default_gallery_style', true ) ) $gallery_style = "
               <style type='text/css'>
                       #{$selector} {
                               margin: auto;
                       }
                       #{$selector} .gallery-item {
                               float: {$float};
                               margin-top: 10px;
                               text-align: center;
                               width: {$itemwidth}%;
                       }
                       #{$selector} img {
                               border: 2px solid #cfcfcf;
                       }
                       #{$selector} .gallery-caption {
                               margin-left: 0;
                       }
               </style>
               <!-- see gallery_shortcode() in wp-includes/media.php -->";
       $size_class  = sanitize_html_class( $size );
       $gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
       $output      = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );

       $i = 0;
       foreach ( $attachments as $id => $attachment ) {
               if ( isset( $attr['link'] ) && 'file' == $attr['link'] ) {
                       $link = wp_get_attachment_link( $id, $size, false, false );
               } elseif ( isset( $attr['link'] ) && 'none' == $attr['link'] ) {
                       $link = wp_get_attachment_image( $id, $size, false );
               } else {
                       $link = wp_get_attachment_link( $id, $size, true, false );
               }

               /* -------------------------------
                 MODIFICATION ################
                 ------------------------------- */
               $image           = wp_get_attachment_image( $id, $size, false );
               $attachment_meta = get_post_meta( $id, '_rt-image-link', true );
               if ( $attachment_meta ) {
                       if ( $attr['link'] == 'custom_url' ) {
                               $link = $attachment_meta;
                       }
               }

               $output .= "<{$itemtag} class='gallery-item'>";

               if ( $captiontag && trim( $attachment->post_excerpt ) ) {
                       $output .= "<{$captiontag} class='gallery-caption'>" . wptexturize( $attachment->post_excerpt ) . "</{$captiontag}>";
               }
               $output .= "<{$icontag} class='gallery-icon'>";

               /* -------------------------------
                 MODIFICATION ################
                 ------------------------------- */
               if ( $attachment_meta ) {
                       $output .= "<a href='$link'>$image</a>";
               } else {
                       $output .= $link;
               }

               if ( trim( $attachment->post_content ) ) {
                       $output .= '<br />'.wptexturize( $attachment->post_content );
               }

               $output .= "</{$icontag}>";

               $output .= "</{$itemtag}>";
               if ( $columns > 0 && ++$i % $columns == 0 ) $output .= '<br style="clear: both;" />';
       }

       $output .= "<br style='clear: both;' /></div>\n";

       return $output;
}
?>

Cleverness WordPress Base Theme 3.0

Download WordPress Base Theme v3.0Last Updated 11-19-2011

Version 3.0 of the Cleverness WordPress Base Theme is now available. The code has been updated for the latest WordPress functions and includes many new features in functions.php. The theme files have been mostly rewritten and now utilize loop.php. Check out the base theme’s page for all the functionality.


Please use print stylesheets (or something)!

Tonight I’ve been trying to print off articles on releasing themes to the public. I’ve been to about 8 or 9 sites. One had a link to print the site using printfriendly (a good option). Only one other used a print stylesheet. Some of these places even sell WordPress themes. Yet their content is unreadable when printed due to elements overlapping each other and such. And thanks, but I don’t really want to waste paper printing your comment form and all your menu items. I’m really glad printerfriendly.com is there to make any web page printable. But it’s still something a site owner should take care of themselves.

Please, people, make your sites printer friendly.


To-Do-List Plugin 2.3 Beta Now Available

The beta version of the To-Do List plugin is available for download from here.

Download To-Do List Plugin v2.3betaLast Updated 10-02-2011

The major new feature in this version is front-end administration shortcodes. The two new ones are [todochecklist] and [todoadmin]. Documentation on these are on the help page under the To-Do List menu item in your WordPress install.

Bugs are to be expected. Please don’t use this on your live site.

You can comment here or email me with any bugs you find.

== Changelog ==
= 2.3 =
* Moved dashboard widgets settings to the dashboard widget
* Added ajax to dashboard widget, main plugin page, and category page
* Added front-end shortcode
* HTML in tasks has been fixed
* Started moving code into classes and redoing a lot of it
* Fixed categories not working in multi-site with the plugin network-activated
* Added Czech translation by Tomas Vesely
* Added updated German translation by Janne Fleischer


Upcoming 2.3 Version of the To-Do List Plugin

I have been working on version 2.3 of the To-Do List Plugin. It does include front-end support. My goal is to release a beta version at the end of this month. I’ve re-written a lot of the code, converting things into classes. I’ll still have more work to do on that after the beta comes out.

The front-end support will not show competed items and as such will not have a delete all completed link at release. Category management will not have a front-end version. There is unlikely to be any front-end widgets at beta release, only shortcodes. Visual styling options of the front-end will need to be added in the feature (colors and such).

I’ve fixed a few bugs, including the multi-site category issue. I’ve also added a few new features.

I’ll need people to beta test the new version when it comes out. There is likely to be bugs.


WordPress and Ajax

I bought WordPress & Ajax by Ronald Huereca awhile ago and finally got around to reading it. When I bought it, it wasn’t available in print, like it is now (WordPress & Ajax print version on Amazon). I had a really hard time getting myself to actually read an ebook on my computer, but I recently bought a nook color and can now read all my ebooks more pleasantly.

But back to the book. I was excited when I found this book because I wanted to add ajax functionality to my to-do list plugin, but didn’t really understand how. This book shows you exactly how to do it and you can download the code from the book at the website. I admit I didn’t fully get the javascript in all the examples, but that’s not the book’s fault. I’m sure I’ll understand it better once I actually use it instead of just looking at it. He uses jQuery in the book, which I liked since that’s what I was planning on using.

I don’t have any complaints about the book. I would have loved to have seen some code that I could relate more directly to my plugin’s functionality, but that’s just wishful thinking.

Aesthetically, the book looked very nice. The ebook comes as a PDF. The nook color has the bad habit of showing non-DRM PDFs in the document viewer instead the book viewer. You can’t easily read them that way. I used the ezPDF Reader app and the book looked great.

I would encourage anyone who is interested in using ajax with WordPress to read this book. You really don’t have any excuse not to because the ebook is now available for free. Some people are upset that something they paid for is now available for free. And maybe I would be too if it was some big publisher, but it’s one person who took a lot of his own free time to write this book. He deserves whatever you may have paid him.


Coming Soon: WPCodeDB

As the final project of a PHP/MySQL 3 course I’m taking, we have to create a social web application.

There are many code snippet repositories out there, but none that I found that are specific to WordPress. I thought it’d be great to have them all in one spot, so I decided to create one as my final project (which needs to be done before the end of this month).

I hope that once it launches, other people will use it besides myself so it will become a great WordPress resource.

In July I’m planning on resuming development on the To-Do List plugin. I’ve tested it in single user mode with 3.2RC1 but not in group or master mode yet. Single user with or without categories is working fine.


Development, bug fixes, and support will be very limited until July

This year I have been planning lots of improvements to the plugin, especially adding Ajax. This past winter and early spring was not good to me health-wise. I have chronic medical conditions plus I had bronchitis a few times. And a couple ER visits. Now that my health is stable for the moment, I was looking forward to getting back into development. But, it is not to be yet.

The company I work for received a grant last July for training. Unfortunately, the company owner did not approve the training until February. All the training has to be completed by June 30. With me being sick and it being busy at work, I still have 1 class to finish and one to complete (there were three). I’m taking them through O’Reilly. I took Javascript 2: Ajax first. The assignments were not overwhelming and took a reasonable amount of time to complete. Then, I started PHP/MySQL 2: Relational and Logical Database Design. Wow. This class has been a lot of work. The assignments take forever, it seems. I still have PHP/MySQL 3 to complete, which sounds like it will be even more work. So, from now until the end of June I will be spending a good amount of free time working on these courses and between that and coding at work, I won’t be up to working on the plugin.


To-Do List Plugin Issue with Multi-site

There’s a problem with the plugin with version 3.1+, multi-site, and network activation.

I haven’t figured out why yet it’s happened with mine, but apparently this isn’t the only plugin that developed that problem with 3.1.

The temporary work-around is to use individual activation rather than network activation. I will get a bug fix update as soon as I can.


Professional WordPress Plugin Development Book Review

If you develop WordPress plugins, you need to read Professional WordPress Plugin Development by Brad Williams, Ozh Richard, and Justin Tadlock.

Professional WordPress Plugin Development Review

I first read this book around the beginning of April. I learned a lot of new things. And then promptly forgot it all. So yesterday and today I went through the book again and took notes. This book is around 500 pages and covers pretty much any topic you could wish for. I made sure to pay special attention to the security section since that is probably the most important feature of plugin development.

I found things that I want to do to improve my To-Do List plugin (and wrote them down this time) and things that I will keep in mind for my next plugin.

You need to know PHP to understand the code in this book (you can’t develop a plugin without knowing PHP anyway). I’m already familiar with a lot of WordPress’s hooks and functions and how WP works. I felt that this book was right at my skill level.

I recommend this book 100%.