synapticism/ubik

A library of useful theme-agnostic WordPress snippets, hacks, and functions for WordPress


Keywords
plugin, wordpress, wordpress-plugin
License
MIT

Documentation

UBIK Status: Experimental WordPress 4.7 PHP 5.5

Ubik is a modular library of WordPress hacks and snippets. It is designed to be both powerful and lightweight, offering a small footprint and no unnecessary admin screens or option table bloat.

Ubik modules do not feature any sort of back-end interface; all settings are managed by the WordPress internal add_theme_support() function and the filter and hook system.

This project is beta software. I use it in production on all of my WordPress sites and will provide some support through the issues system here on GitHub. That being said, I am developing this plugin as if nobody else is using it and I may move fast and break things until I see some sign that others are interested in it. I should also mention that Ubik is presently undergoing a rewrite and the documentation below may be outdated or incomplete. The source code is meant to be read so I encourage you to take a close look at specific modules that interest you.

Requirements: PHP 5.5 and WP 4.7.

Please note that these docs aren't very up-to-date; browse the well-commented source for more about any modules you're interested in!

Functions

These files contain useful and common functions that require no configuration.

CJK

  • "Unpinyin" function; converts accented characters to their non-accented equivalents.
  • CJK (Chinese/Japanese/Korean) character wrapping; provides a styling hook e.g. to enforce font-style: normal; and other CSS declarations. (Experimental!)

Text

Simple text processing functions for WordPress. Most of these functions are used internally but you can hook into them in your theme as well.

  • ubik_text_replace() extends wptexturize with additional text replacement shortcuts (browse the source for a full list or look below for a few examples).
  • ubik_text_truncate() truncates, neatens, and trims text for excerpts and meta descriptions, among other things.
  • Strip opening <aside> tags from post contents; this way you can open a post with an aside (e.g. "This post was originally written three years ago...") without it dominating SEO.
  • Strip code, footnotes (added by Markdown Extra), paragraphs surrounding media, and orphaned <!--more--> tags.
  • Strip specific shortcodes with ubik_text_strip_shortcode.

Titles

  • Generic archive/search/etc. title generator.

Modules

Admin

  • Links: disable the now-deprecated built-in link manager; useful on older installations.
  • Post editor: disable visual editor for all users. Default: false.
  • Post list: add a column with post thumbnails. Defaults: false and 60 (px).
  • Post slugs: removes non-Latin characters (designed for use with bilingual English/Chinese post titles). Default: false.
  • User contact methods: add Facebook, Flickr, GitHub, Google+, Instagram, and Twitter. Default: false.
  • User contact methods: remove AIM, Jabber, and Yahoo. Default: true.
  • User descriptions: allow HTML. Default: false.
  • View all settings hack (activate and browse to the Options menu). Default: false.

Admin Terms

  • Edit terms: add a link to term archives for the specified taxonomies. Default: ['category', 'post_tag'].
  • Edit: enable term-based dropdown filter for an arbitrary list of post types and taxonomies. Defaults: ['post'] and ['post_tag'].
  • Term list: add term descriptions to the quick edit box for specified taxonomies (default: categories and tags). Also compatible with HTML, Markdown, shortcodes, etc. Default: ['category', 'post_tag'].
  • Term list: strip all HTML tags from terms descriptions in the list of terms; useful when working with HTML/Markdown. Default: true.

Colophon

  • Colophon generator: glyph, date range, credit, technology stack/powered by statement.

Filter

  • Exclude posts from the homepage by category, tag, or post format using the WordPress filter system.
  • Create a virtual alias of the homepage to show all content at a specified URL e.g. /all. This alias functions just like your homepage in every other respect. (Remember to flush permalinks after activation.)
  • Combining these two functions you can create a homepage that is free of clutter alongside a link to a busier stream of content containing absolutely everything you post. Makes for a nice separation of top-shelf content and trivial micro-blogging and lifestreaming.

Formats

  • Post format rewrite: change the base ("type" to whatever).
  • Remove specified post formats from the feed (defaults to aside, chat, link, quote, and status post formats; filter ubik_feed_disable_formats and return an array to modify this behavior).
  • See also: my fork of WP-Post-Formats, an admin interface for working with post formats.

Google

  • Adds Google Analytics code (universal or legacy) to the footer, where it belongs.
  • Some extra settings for the new UA script.
  • Google Fonts support; pass a valid font request string and it'll be output in the appropriate spot.
  • Experimental inline or async font loading support.

Main

  • A cleaner implementation of the core comments_popup_link function without all the popup code and other cruft.
  • Modify what tags are allowable in comments. WordPress ships with a lot of stuff that nobody uses (e.g. <abbr>). This trims the list to just those elements that are most useful.
  • Turns off comments on all attachments. Also alters the text that commonly appears below the comment entry form to match what tags are actually allowed.
  • Nicer, smarter excerpts with sensible defaults.
  • Also used to generate meta description tag content for SEO.

Markdown

This module assumes that JP Markdown or equivalent are active.

  • Enable additional Markdown Extra processing within specified block elements (or false to disable). Default ['address', 'aside', 'blockquote'].
  • Make footnote links absolute; a hack to avoid problems with the use of Markdown Extra footnotes and the <!--more--> tag. Default: true.
  • Markdown Extra adds a non-standard rel="footnote" attribute doesn't validate; nuke it from orbit. Default: true.
  • Enable additional Markdown transformations using the specified filters.

Meta

This component features an assortment of low-level utility functions to deal with retrieving and displaying post metadata (date, author, categories, tags, etc) in the loop. WordPress handles this stuff well enough but the API is all over the place; this is meant to make it as easy as calling ubik_meta_author() or ubik_meta_categories() to get whatever data you need.

Also included are several "recipes", wrapper functions that return some common metadata patterns e.g. a string saying when a post was published and when it was last updated. Browse the source for a full list of all features; the code itself is quite simple for the most part.

  • ubik_meta() returns a long-form string featuring all sorts of pertinent metadata.
  • ubik_meta_date() returns an array of useful data for displaying dates.
  • ubik_meta_date_published() and ubik_meta_date_updated() return dates wrapped in appropriate HTML5 markup.
  • ubik_meta_date_humanize() will optionally convert a date into human-readable form (e.g. "1 week ago"). It defaults to a time horizon of two months (anything less than that will be humanized) but not on archives (where the actual date will continue to be displayed). This feature is not switched on by default as JavaScript is a much better way of doing this if you're caching (which you should be). Consider using jQuery-Timeago!
  • ubik_meta_categories() and ubik_meta_tags() returns categories and tags respectively. ubik_meta_terms() is a generic function allowing you to specify taxonomy.
  • ubik_meta_taxonomies() is a hook for adding additional taxonomies to metadata output.
  • ubik_meta_type() returns a string describing the type of content: (standard) post, image, attachment, page, post format (gallery, status, etc.), or custom post
  • ubik_meta_parent() returns the post parent optionally wrapped in a link.
  • ubik_meta_author() returns the author optionally wrapped in a link with appropriate microformat tags.
  • Standards-compliant post metadata generator with many filters to hook into.
  • Experimental Microformats2 and schema.org structured data support (e.g. proper use of dt-updated and dt-published classes).

More

  • Several user-configurable options including excerpt length, <!--more--> tag string, etc.

Optimize

  • Cleaner feed archives titles.
  • Disable comments feeds.
  • Head cleaner; removes a lot of junk from the page header, a common feature of most starter themes.
  • Removes the .recentcomments style injected into the header.
  • Add the defer attribute to script tag output.
  • Load jQuery in footer.
  • Remove jQuery migrate from default scripts; adapted from Paulund (disabled by default).
  • Remove Open Sans from themes (where applicable) and the admin panel; via Disable Google Fonts (disabled by default).
  • Strips "protected" out of protected post titles.
  • Remove duplicate page template classes (e.g. 'page-template-page-templates') that come up when you store page templates in a subdirectory.
  • Tidy language attributes, style tags, and self-closing tags; via Soil.
  • Remove emoji added in WordPress 4.2.
  • Remove embeds added in WordPress 4.4.
  • Disable pingbacks entirely; functionality courtesy of Soil.

Password Form

  • A configurable post password form with class and styling hooks.

Places

  • Place archives act like regular term archives with the addition of breadcrumb navigation and a list of descendent or related places.
  • Hooks into entry metadata to display places next to tags and categories.
  • No custom mapping function at present. Just use simple Google Maps embeds.

Related

  • Get related post IDs for a given post ordered by relatedness.
  • Works with single or multiple taxonomies, weighted or unweighted.
  • Optionally "extend" certain taxonomies to broaden the search (matches posts with sibling terms).
  • Filter ubik_related_score to adjust the results (e.g. for having comments or a featured image or being in a particular category).
  • Works with posts by default (but there's a hook to allow using this with custom post types).

Search

  • Replace the default WordPress form with an improved HTML5 version. Default: true.
  • Posts per search page (integer). Default: false.
  • Redirect search results; e.g. ?s=cat to /search/cat. Default: true.
  • Redirect empty search to random post. Default: false.
  • Singleton search redirect; if there's only one result go directly to that URL. Default: true.

SEO

Functionality specific to Yoast's WordPress SEO:

  • Add default meta descriptions even when post type templates aren't filled out.
  • Sanitize meta descriptions. (Processes shortcodes, uses wp_trim_words, adds an ellipsis to the end, etc.)
  • Output meta tags for all images associated with posts that have a featured image/post thumbnail. (Yoast only displays one!)
  • Output additional image-related meta tags (type, width, and height) for Open Graph.
  • Optionally remove the Yoast SEO button from the admin bar.
  • Optionally remove the Yoast post filter dropdown.
  • Designed and tested for Yoast WordPress SEO 3.3+ (but it might be backwards compatible to something around 1.6).

General functionality (mostly from a previous iteration of this plugin when it was meant to be a standalone SEO plugin):

  • Optional credits at the bottom of posts in the feed (duplicating Yoast's WordPress SEO functionality for those who aren't using it).
  • 'ubik_seo_image_attachments()' returns all images attached to a post (with featured images/post thumbnails displayed first).
  • Meta description generator (based on code from Ubik Excerpt).
  • SEO-friendly title filter for wp_title.

Series

  • A lightweight implementation of a post series taxonomy. Designed to easily link posts together in a series.
  • ubik_series() retrieves all posts in a series along with titles and permalinks.
  • ubik_series_list() formats the results of the above function into an ordered list for use in themes.
  • Series archives themselves are in chronological order (i.e. oldest first).
  • Additional customization is necessary; this component only provides the building blocks.

SVG

  • ubik_svg_icon( name, title, description, class ): request an icon from an external icon sheet loaded elsewhere; title and description are both optional but useful for SVG accessibility. Output includes a wrapper class, icon-wrap for styling hooks.
  • ubik_svg_icon_text( name, title, description, class ): same as above except the title is also output after the icon (a common use pattern). Output includes two styling hooks: icon-text and no-icon (in cases where no icon name was passed to the function).
  • ubik_svg_icon_sheet_inline(): inject an SVG file into the document. Should be used with a custom action in your theme.

Taxonomy Explorer

  • Provides the skeletal outlines of a taxonomy term explorer using a page template provided by your theme.
  • The idea here is to provide a paginated interface to listing terms in a taxonomy, probably with the help of something like the Taxonomy Images plugin.
  • You can squat on an existing taxonomy base e.g. you can publish a page with the slug tags, show 10 per page, and still browse a paginated list of tags or access tag/example as needed.
  • The only exception to the aforementioned scenario is if your paginated page is resting on the base of a hierarchical taxonomy; then you'll need to activate the simple rewrite feature.

Terms

This needs a serious update:

  • Taxonomy shortcodes: [shortcode]Example[/place], [shortcode slug="example"]Something else[/shortcode] or [shortcode slug="example"/] for an arbitrary list of taxonomies and customizable shortcode names. Defaults back to plain text when a term does not exist.
  • \Ubik\Terms\categorized() conditional: an adaptation of the "uncategorized blog" function from _s. This version also allows you to explicitly declare that your blog uses no categories.
  • Admin: hide the category filter on uncategorized blogs.
  • Allow shortcodes in term descriptions.
  • Return an edit term link and empty description prompt for authorized users on the front-end. Useful for anyone interested in maintaining term descriptions (a common SEO practice).
  • Utility functions for getting and listing popular terms in a given taxonomy.
  • Generate an array of ancestor term IDs for a specific term.
  • Generate breadcrumbs navigation for term archives.
  • Count how many terms a post has.

Views

Developer tools for setting up URL rewrites that call content templates specified in the theme. Core functionality is provided but considerable customization is required at the theme level. This component mainly provides rewrites and query variables with a few helper functions. Examples: a gallery view that calls the "thumb" content template and shows 20 posts per page instead of 10, or places taxonomy archives that display posts using a list content template.

  • Creates URL endpoints for views per taxonomy e.g. http://synapticism.com/meta/photography/gallery will load the specified template for each post.
  • is_view('view') tests whether the current page is one of the pre-defined views. You can also pass a specific view to check whether the current page is a gallery or whatever.
  • this_view() returns the current view slug for testing purposes.
  • get_links() returns an array of data useful for creating buttons and navigation elements to switch between views.
  • Template loading must be handled by the theme. Your theme will also need matching templates for all defined views.

Configuration

[To be completed.]

Remember to flush your permalinks after messing around with certain components; this is not handled automatically.

Philosophy

All Ubik modules aspire to some simple guidelines (in case anyone would like to contribute or simply understand a little more about what this project is about):

  • Keep things simple--Ubik modules are meant to be understood at a glance. Anything that requires more complicated architecture may be better suited for a full-fledged plugin.
  • Stick to simple procedural functions where possible; don't use classes for namespacing. This makes it easy to cut and paste functions into other contexts.
  • Options should be handled with the built-in options class for easy integration with the WordPress filter/action system. No options pages or other admin panel bloat.
  • Principle of least surprise: features should be switched off by default where possible (unless activating a module suggests that it is safe to do otherwise).
  • Only include PHP code. Let themes handle front-end dependency management (e.g. CSS/JS). This requires more work by the developer but avoids that horrible problem in the WordPress world where sites are loading dozens of different script and stylesheet fragments.
  • Avoid generating markup; give theme authors the raw data they need to implement a particular function wherever possible.
  • All functions, constants, variables, and translation-ready strings should be in the Ubik\Module namespace and __( 'String', 'ubik' ).
  • Comment liberally and in proper English; Ubik's code should be easy to follow.
  • Credit original sources where sensible to do so.
  • Follow semantic versioning.

Final note: I don't presume that everything you might want to do with WordPress can or should be done this way. Ubik deviates from what you might call "the WordPress way" but it isn't meant to be a political statement of any kind. Mostly it's just a bunch of code that I have needed for one reason or another and have seen fit to clean up and standardize in a way with some potential benefit for others.

If you'd like to contribute by identifying issues or submitting pull requests, please do!

License

GPLv3.