Stephanie Leary

Writer and WordPress consultant

  • Books
    • Content Strategy for WordPress (2015)
    • WordPress for Web Developers (2013)
    • Beginning WordPress 3 (2010)
  • Blog
    • Fascism Watch (2016-17)
    • Content Modeling for WordPress series
    • WordPress Hidden Gems series
  • Work
    • Portfolio
    • Services
    • WordPress Plugins
    • WordPress Themes
    • Presentations and Interviews
    • on GitHub →
  • About
    • Press Kit
  • Contact
    • Mailing List

Add sortable, striped table script — only when needed

January 21, 2010 Stephanie Leary 2 Comments

Ever wondered how I dressed up the tables on this site? I’ll show you! We’ll use the code for adding stylesheets and scripts conditionally to include Yoast’s improved sortable.js only when a post or page contains a table.

// enqueue sortable.js on pages containing tables
function add_sortable ($posts) {
	if (empty($posts)) return $posts;
	$found = false;
	foreach ($posts as $post) {
		if (stripos($post->post_content, '<table')) {
			$found = true;
			break;
		}
	}
	if ($found) {
		wp_enqueue_script('sortable', get_bloginfo('template_url').'/js/sortable.js');
	}
	return $posts;
}
add_filter('the_posts', 'add_sortable');

Add this to your functions.php file. Then add class="sortable" and a unique ID to your tables and view your post or page. If it worked, your table headings should be clickable, and your table rows should be alternating colors (assuming your stylesheet includes .odd and/or .even classes).

WordPress javascript, tables, Templates

Comments

  1. Joe Van Steen says

    August 3, 2011 at 10:36 am

    Stephanie,
    Line 6 is missing some code for the stripos statement.

    Reply
    • Stephanie says

      August 8, 2011 at 9:45 am

      Thanks. WordPress really did not like that line, but I think I’ve fixed it. It should be:

      if (stripos($post-&gt;post_content, '&lt;table')) {
      
      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Fascism Watch

The Fascism Watch is a daily(ish) news roundup. View all the previous Fascism Watch posts »

Latest WordPress Book

Content Strategy for WordPress

A short book for content strategists and managers on implementing a complete content strategy in WordPress: evaluation, analysis, content modeling, editing and workflows, and long-term planning and maintenance.

Read the sample chapter

Kindle Nook iBooks Kobo Smashwords

WordPress for Web Developers

WordPress for Web Developers (9781430258667)

This is a book for professional web designers and developers who already know HTML and CSS, and want to learn to build sites with WordPress. The book begins with a detailed tour of the administration screens and settings, then digs into server-side topics like performance and security. The second half of the book is devoted to development: learning to build WordPress themes and plugins.

This is the second, much-revised and updated edition of Beginning WordPress 3, with a more accurate title. Everything’s been updated for WordPress 3.6.

WordPress for Web Developers is out now. See what's inside...

The best WordPress features you’ve never noticed

  • WordPress Hidden Gems: Screen Options
  • WordPress Hidden Gems: Bulk Edit
  • WordPress Hidden Gems: Private Status
  • WordPress Hidden Gems: Dashboard Feed Readers
  • WordPress Hidden Gems: Options.php

Content Modeling for WordPress series

  • Content modeling for WordPress, part 1: analyze content
  • Content modeling for WordPress, part 2: functional and organizational requirements
  • Content modeling for WordPress, part 3: a sample content model

This is an excerpt from Content Strategy for WordPress.My latest books are Content Strategy for WordPress (2015) and WordPress for Web Developers (2013). Sign up to be notified when I have a new book for you.

Copyright © 2021 Stephanie Leary