Introducing Bedrock, a Minimal Base Theme for WordPress

Since I started building frontend templates and WordPress themes, I've been collecting my own bag of tricks and reusable code snippets. Now at long last, I've put them together and gotten them up on the web.

Introducing Bedrock - a simple base theme for WordPress web development. There's a few different parts to this:

  • a collection of frontend best practices and conventions, I call this "Bedrock Core"
  • a base WordPress theme kit and WP mini-framework, "Bedrock WP"
  • some extra plugins and other resources, including WP-Lipsum

The framework is built around the idea of being minimal, DRY-based, and as self-documenting as is practical. The goal is to optimize for being easy to understand and quick to use.

The official first version of Bedrock is available for download here. Right out of the box, it has a bunch of features:

  • clean, modular code
  • 6 built-in layout types
  • supports post thumbnails 
  • ready for multi-language translation

This is just the first version, soon I hope to add a bunch more features:

  • responsive layout
  • built-in CSS LESS
  • WordPress options panel
  • custom comments template

The theme is also now up on Github. If you feel like contributing to the project, go ahead and fork it and let's start talking.  A big thanks to everybody who has helped with Bedrock in its long evolution, especially @joepahl, @jeremyivy, @sabraduffiney!

Soon I'll write more about the concepts of Bedrock Core, and add some more pieces to the framework too.

33

857b69e297d311e1abb012313813106f_7

The Secret to Enqueuing Scripts in the WordPress Footer

WordPress offers the enqueue_scripts feature, which allows theme developers to carefully manage their script dependencies and versions.  Here's the syntax:

First your register your script -

<?php wp_register_script( $handle, $src, $deps, $ver, $in_footer ); ?>

Then enqueue it, and pass it the handle that you gave it:

wp_enqueue_script( 
     $handle
    ,$src
    ,$deps
    ,$ver
    ,$in_footer 
);

This is a neat system, and it even offers the ability to put the scripts in your header or your footer, with the $in_footer argument. This is great for optimizing your site's load time.

Theoretically, this would be like:

<?php  

wp_register_script( 'my_script', $script_path );
wp_enqueue_script( 'my_script', false, false, false, true );

?>

Except that $in_footer doesn't work like that.

This was very frustrating for me, as I did everything like the Codex said - and got the script loading fine in the head. But no matter what I set $in_footer to, I couldn't get the script to move to the footer.

The Codex doesn't explain this, as far as I can see. It was only after scouring the interwebs, that I found the answer:

The $in_footer flag does not work if you register the script ahead of time.  

Yes that's right, a WordPress feature does not work if you follow WordPress best practice. Imagine that.

Instead of registering your script ahead of time, name it on the fly when you run wp_enqueue_script:

<?php wp_enqueue_script( 'my_script', $script_path, false, false, true ); ?>

And voila, your script will load in the footer!  (A big thank you to Cloud Four for their blog post!)

 

10 Startup Lessons from James Brown

Happy birthday to the Godfather, he woulda been 79 today.

5443493553_725c18a805

 

Here's 10 lessons I've learned from Jame Brown:

 

#1 - Innovate

 

#2 - Say Something Worthwhile

 

#3 - Amplify Your Identity

 

#4 - Recruit a Rock-Solid Team

 

#5 - Be Versatile

 

#6 - Hype It Up

 

# 7 - Sex Sells

 

#8 - Adapt with the Times

 

#9 - Build a Library of Hits

#10 - Be a Boss

 

Introducing WP Lipsum, a WordPress plugin for instant dummy content and style guides

WP Lipsum is a simple plugin for generating dummy text/style guides for your WordPress site, using fake "lorem ipsum" text and dummy images. This intended for use by theme developers and project managers while building out sites.

http://bedrocktheme.com/wp-lipsum

I had been working on a dummy content kit for my Bedrock theme framework (under construction!). Then my friend @sensoredmedia made the complaint that there's no good Styleguide plugin, like they have in Drupal

So I went ahead and put my dummy content kit into plugin form.  This is under construction, but already I've found it very useful.  Best of all, it's super easy to add your own additional lipsum templates - great for working on multi-language sites, or other specialized uses.

Update - New Version

Thanks for all the great feedback on my 1st plugin! I've updated this so it works with over 20 template fragments now, useful for lots of diferrent types of pages and content.

Learn more here, or check out examples of the basic content page or the auxiliary content page.

Download it now from the Wordpress plugin directory:

http://wordpress.org/extend/plugins/wp-lipsum/

If this sounds useful to you, please try it out and let me know how it does. And if you have any suggestions I'd love to hear them!

 

The Ultimate Guide to WordPress Post Excerpts

Displaying post excerts in WordPress can be surprisingly confusing. Here's an explanation of the options.

 

WordPress Default Excerpts

A WP post lets you define the main content, and an optional excerpt.  By default, pages do not offer excerpts, but you can change that

In the theme, theme developers have access to the the_excerpt() template tag. If you have filled in the excerpt box, WP displays your custom excerpt (this is the a great way to write up a custom 'teaser' for long posts).

If you left the excerpt field blank, the the_excerpt() tag does the following:

  • strips out all HTML and images
  • truncates your post to 55 words
  • adds this to the end of the text: "[...]"

Sometimes this is okay, but in general the [...] makes it too ugly to use without customization on most sites.  

Note: the [...] text is included inside the last paragraph, without any span or other container around it. If a post has a custom excerpt, then no "more" text is displayed at all afterwards.

 

Modifying the Excerpt

Fortunately, the excerpt word count and [...] text can be modified with a little code - you can even add a "more" link to the end of the excerpt. 

the_excerpt - Changing the Word Count

<?php
function custom_excerpt_length( $length ) {
    return 35; //Change this number to any integer you like.
}
add_filter( 'excerpt_length', 'custom_excerpt_length' );
?>

 

the_excerpt - Replacing [...] with just ...

<?php
function custom_excerpt_more( $excerpt ) {
    return str_replace( '[...]', '...', $excerpt );
}
add_filter( 'wp_trim_excerpt', 'custom_excerpt_more' );
?>

 

the_excerpt - Replacing [...] with a link

<?php
function custom_excerpt($text)
{
    return str_replace('[...]', '<a href="'. get_permalink($post->ID) . '">' . '&raquo; Continue Reading.' . '</a>', $text);
}
add_filter('the_excerpt', 'custom_excerpt');
?>

WP Note: I hope someday these excerpt settings can be options in the Reading settings panel!

 

Excerpt Plugins

For even more flexibility, check out the Advanced Excerpt plugin - which can fine tune or disable the HTML filtering, provides custom "more" links, offers counting by word or character, and other options. Or check out Fancy Excerpt, which provides clean "sentence aware" excerpts.

Note: Some of these plugins work by overriding the_excerpt, while others give you a separate function for getting what you want. Read the instructions. 

Also Note: if you do use a plugin to allow HTML in your excerpts, make sure your plugin takes care to close any open HTML tags. You don't want to allow a random unclosed <strong> or <a> tag to destroy your website's user experience someday. And if you're truncating the excerpt by character count, make sure the plugin won't chop HTML tags in half!

 

WordPress Content Function

Alternatively, we can also use the the_content() function in our themes. This behaves a little different than the_excerpt.

the_content - The Short Version

On list pages - the WordPress blog home page, category list pages, archive pages, and the search results page - the_content() displays the main content of your blog post, up until it encounters the break tag:

 <!--m o r e-->

Once WP encounters the more break, it truncates the post and appends a "Read more..." link. This is the "short version." (Note: actual break tag doesn't have spacing between the letters. I'm trying not to break my blog :)

Unlike the more text for the_excerpt, the_content displays this link in a new paragraph. There is no class on the inserted paragraph, but there is a 'more-link' class on the link:

<p>< a href="http://yourblog.com/some-post#more-97" class="more-link">Test</a></p>

WP Note: It would be very helpful if these inserted paragraphs had a class on them, or better yet a custom class. It would be also be useful if the more-link had the option of being inserted inside the last paragraph, like the_excerpt's more text. Last but not least, it would be helpful if we could trigger whether or not the link jumps to the #more- anchor.

The text of the link is customizable by passing in a string, like so:

<?php the_content('Full Content Here...'); ?>

You can also override the more link text on a post by post basis, from with the editor. Just make the break tag like this:

<!--more Keep reading this post-->

FYI - There is a way to manually strip the #more- anchor jump out of link, and other techniques for overriding the more text - details here.

You can check to see if a post contains a break tag by using this 

if ($pos=strpos($post->post_content, '<!--m o r e-->')) { // do something }

WP Note: it would be great to add a has_more_tag() conditional tag.

Be aware, the_content() doesn't filter out any HTML from the short version. So if you split your post in the middle of an HTML tag, you will have problems.

This page shares how to add HTML filtering to the_content.

the_content - The Long Version

If WP doesn't encounter the break tag, then it will display the post's entire contents - the "long version". On singular pages, the_content() ignores the break tag and displays the long version by default.

 

Overriding the Break Tag

The break tag can be overridden pretty easily. WordPress keeps track of the mode to use for the_content via a global variable called $more.  

  • When $more is set to 0, the_content() will display the truncated "short version" of a post.
  • When $more is set to 1, the_content displays the whole post.

Here's some example code:

<?php 
        global $more; 
        $more = 1;       
        the_content(); // long version
?>

A Flexible Excerpt Snippet

Okay wait, this is really confusing and there's a lot of options - what's the best?

It all depends on the project and needs of your theme. Most of the time, you'll probably want to integrate a tool like Advanced Excerpt.

But if you'd like to stick with WP's built in offerings, here's a little template snippet you can use in your loop. This lets you define a global $more_text string, which is used no matter what. And there's a conditional to choose intelligently which excerpt to load.

 

I hope this guide has helped you navigate the mysterious world of WordPress post excerpts! Let me know if you have any questions.

 

The Ultimate List of Useful WordPress Plugins

The WordPress plugin universe is awesome, but sometimes it's just too big. It can be hard to find plugins that are stable (with the latest WP version) and easy to integrate.

So I went ahead and made a list of useful and reliable WP plugins, here it is: 

 

Post Utilities

Custom Post Template - lets you select custom templates for posts, just like pages*

Quick Page/Post Redirect - for when you need to redirect a URL

 

Shortcodes

List Pages Shortcode - Quickly list sibling or child pages in your WP post

Allow PHP in Posts and Pages - provides simple [php] shortcode, and gives you access to all WordPress template tags within a post

WP Lipsum - my plugin for quickly adding dummy content via shortcode

 

 

Excerpts

Advanced Except - gives much more flexibility than the default WP loop

Fancy Excerpt - this plugin offers "sentence aware" excerpts, truncates paragraphs cleanly

Page Excerpts - add excerpts to pages, this is so simple its not even a plugin

 

Widgets

Widget Logic - Adds conditionals to all widgets, for sidebars that are truly dynamic

Enhanced Text Widget - Adds a custom class and other options to the basic text widget.

Image Widget - Adds simple image widget with uploader

Post and Page Excerpt Widgets - display snippets of posts in your sidebar**

Better Blogroll - allows listing links by rating, more

 

Social Media

Twitter Wings - awesome Twitter widget by my friend Joe Pahl

 

Comments

Disqus - the best web comment system

 

CMS Features

Advanced Custom Fields - Make custom fields edit boxes look great, works with custom post types too

Pods - For more robust CMS capabilities, try pods*

 

Contact Forms

Gravity Forms - premium form builder, worth the money

Gravity Forms + Custom Post Types - allow your users to submit custom posts, looks awesome*

Contact Form 7 - free form builder, it's okay but nowhere near as good as Gravity Forms

 

Events

The Events Calendar - free events plugin, lots of premium addons

The Future is Now - Show future-time-stamped posts immediately, useful if you're building your own events list using posts

 

Images and Multimedia

WP-Cycle - very simple slideshow plugin

Regenerate Thumbnails - Quick and easy way to rebuild all images after changing thumb sizes

 

Popups and Calls to Action

Easy Fancybox - flexible and stable popup

HelloBar - popular top bar plugin*

 

System Infrastructure

WPML - multi-language post manager

WP Mail SMTP - fixes your mail server, useful on some web hosts

 

Control Panel

WP Manage - manage multiple sites from 1 dashboard, I haven't tried it yet but heard it was solid

Lorem Ipsum Post Generator - for setting up a bunch of dummy posts and comments quickly

Codepress Admin Columns - add extra columns to your admin screens

 

Performance and Analytics

W3 Total Cache - full-featured yet reliable caching plugin

Yoast SEO and Google Analytics are the best in their class

These are all super useful and reliable. But this is not complete, there's a lot of things I haven't been able to find a good plugin for -

 

Plugins I'd like to find:

  • a good image gallery - not NextGen
  • a good sharing plugin - not ShareThis
  • more social plugins - specifically Instagram, FB updates
  • simple page hits tracking plugin (no third party integration)
  • good google analytics dashboard
  • better search plugin
  • rss aggregation plugins
  • anything else that's cool that I'm missing!

I hope you find this list useful, I'll update with any additions. If you have any reliable suggestions for the needs listed above, please leave a comment or tweet at me!

* Plugins marked with a star I have not used myself yet, but I heard they are pretty cool

Working to Code

The studio is a complex and enigmatic working environment, full of precise rules and principles. We call these principles "The Code" ... Follow this guide carefully, and you probably won't be fired. Proper knowledge Of The Code is the first step in working To Code.

via All Along Press

How to Integrate a Complicated DHTML Plugin Without Pulling Your Hair Out

Frontend developers spend a lot of time integrating "off-the-shelf" plugins that other people created. Sometimes, this is really easy. Other times, it can be tricky.

Then every once in a while, it can just be a nightmare. You've got this cool plugin that isn't playing nice with your template or your WordPress, it seems just perfect but it won't work the way it said it would - meanwhile, the clock ticks away on a deadline.. 

Here's a simple process you can use get a Dynamic HTML/jQuery plugin integrated without complications. The basic idea is that we build a stable "proof of concept", and then move it step-by-step into our existing template.

Steps to Integrate a DHTML Plugin:

1) Make sure the plugin can do what you need

This seems basic, but a lot of times folks grab the wrong tool for the job, then spend too much time fighting against it. Instead, make sure you're using the right plugin for your needs. Spend the extra 10 minutes in Stack Exchange or the WP Plugin Repo to make sure you know the options. And don't be afraid to turn to commercial sources - CodeCanyon is pretty amazing!

2) Get the demo working on its own

Download the plugin and get its demo working. Typically most plugins come with a static html file as a demo. Get this working and start learning the how its structured, and what the jQuery syntax will be.

You'll want to start by trying it out on your own computer, but after that take the next step and move it up to the remote server environment that you you will use for your end product. (You can stash it its own folder inside the real site).

The idea being, we want to verify everything works like it says it should, before we let it touch our code.

3) Strip the demo down to your desired proof of concept

Oftentimes plugin demos show all the plugins different variations and features. Strip out all the examples you don't need, and remove all unneeded files too - let's get down to the minumum "proof of concept". Then lets go ahead and make any changes to the plugins syntax to get it working like we want to.

After removing files, double check to make sure the demo still works - if so, lets lets keep movin!

4) Rebuild the demo to match your intended folder structure

Now reorganize the plugin so that the asset files mimic the folder structure of your template system, and update the paths in the demo code to reflect this new file organization. Usually this means renaming the CSS and script folders, and image paths, and then updating all the paths.

We're basically rebuilding the plugin so it matches the world its going to be dropped into. Afterwards check to make sure the demo still works. If not, get your paths straight; if so, lets move on!

5) Make sure the paths of any libraries match the ones in your template

Both your existing template and the new plug demo both probably reference a jQuery library (and possibly other libraries too, like jQuery UI). However they might not be the same version or located in the same place.

Rebuild the path to any libraries on your demo file so that it matches the ones on your existing template. You might want to copy the libraries from your template over to the demo folder; or take this as an opportunity to download the latest version.

(Best practice actually says to link to a remote version of jQuery, so you should probably just do that both places actually).

Also take a moment to make sure that the demo's jQuery syntax matches in both places - and turn on noConflict mode on the demo if needed. 

After you get the libraries matching, make sure your demo still works and then carry on!

6) Move the demo files into your template folder

Now we can go and drop the demo into our existing codebase, so that it sites just beside your existing template. There's a few steps to this:

  • move the plugin HTML file over to your existing theme's codebase. It will probably help if you rename your demo html file something other than "index".
  • move the plugin's CSS and Javascript over, into the place you plan to keep them permanently. If we did step #4, this should already be correct in your HTML
  • If we did Step #5, the paths of any shared libraries will already match.

If you're dropping the demo into a static html site, this should be pretty trivial.

If you're dropping the demo into a WordPress theme, you can view the demo directly by pointing at the file inside the theme structure (something like "/wp-content/themes/your_theme_name/demo.html").

You should now be able to view the demo and have it work at its new location. If not, look back at what you had in step #5 and figure out what changed when you moved it into the codebase.

7) Move the demo code into your template

Last but not least, you should be able to drop the plugin code into your actual template. There's 2 main parts to this:

  • make sure your template loads the plugin's Javascript and CSS files
  • drop in the proof of concept HTML - there might be multiple snippets to drop in

Pow! It should all work. Now you can move forward onto customizing things.

If it doesn't work, we can look back at the proof of concept we had in #6 and figure what changed when we dropped it into the template. If we did steps 1-6 right, then there should be very little different between the final demo and the version that's not working. Deducing what's wrong should be pretty straightforward.

(You want to take extra care to make sure there isn't any errors from conflicting Javascript.)

In comparison, if we had just jumped right into this step and tried to make the plugin work without building the proof of concept first, there could be a wide variety of problems - mismatched paths, wrong syntax, conflicting libraries, bad css, or more.

8) Consolidate and customize the frontend

Now that your plugin is working in your template, you can streamline things and make changes. Typically I'll do the following steps:

  • for any sections of CSS that need to be changed, I'll move them into my main CSS files and customize them there
  • if possible, I'll move any Javascript snippets into my common.js file
  • don't forget to remove the demo html file and any assets that you stop using

9) Integrate the plugin code into your backend

Last but not least, go ahead and integrate the plugin into your backend. Typically this means getting your content management system to spit out the correct html; sometimes you might need to generate Javascript parameters too, or even CSS.

Voila! Now you've got your plugin fully integrated. Every project has its own challenges which might cause this to be adjusted, but the basic idea here is to build a proof of concept and then walk it, step by step, towards its eventual use. Best of all, you've always got a backup plan if things get crazy.

Hopefully this helps you from getting into a jam in the future!

Trey Anastasio On the Community-Building Power of Drug Courts

Here's an inspiring video of one of my heroes, Trey Anastasio sharing his experience in a Drug Court alternative sentencing program, and how it helped get his life mission back on track.

Really inspiring to see hear how such a simple program - diverting non-violent offenders into monitored treatment - can have a bigger effect of helping families start over and communities begin to thrive. (And what a touching note about Veterans Courts!)

As Trey describes, programs like this save money, but more importantly they allow offenders to focus on their real priorities and rebuild a life of service. Jail just serves to create more and better criminals, but rehabilitation has ripple effects beyond the individual.

And wow has the change in his life been apparent, it's pretty clear that his legal troubles have literally saved Trey's life (not to mention allowing Phish to return to the stage!) Go Trey!

As a student of civics, a beneficiary of alternative sentencing, and the child of a lifelong addict, I hope Drug Courts continue to catch on. Our communities and families really need people-centered programs like this; "the system" can do wonders when the rough edges are sanded down a little.