Add a parent count class to WordPress nav menu containers

Mar.04.2015 | 01:22 pm

I am currently working on a project where the designer has created 5 top level items in the primary nav of site. These items are meant to look like buttons and have fixed widths, so there will be a problem if the end user of the theme then decides they want 6 top level elements and they retain that fixed width. I needed a way to add a class to the nav defining how many parent/top level items there are, and then I could style the nav based on that.

Here's what I've come up with:

Full post »

WordPress jQuery widget added callback

Jan.12.2015 | 08:51 pm

So, I keep expanding my bag of tricks for WordPress, and one of the tricks I have is a media uploader that be placed in a meta box or in widget settings by just adding a couple lines of HTML, jQuery does the rest from there. The problem I ran into was when a new widget was added the click events were not bound to them, which is because the html for the widget is dynamically added, but you already knew that.

I dug into the WordPress jQuery code (/wp-admin/js/widgets.js to be exact) and found this:

This will allow you target the newly created widget with a function you've created:

Full post »

Adding CSS classes to a WordPress Widget

Jan.08.2015 | 01:57 pm

Let me preface this, this is only if you are creating a widget and not if you just want to add a class to an existing widget, there are plugins that do that.

It's really actually simple, I just could not find the answer right away because it isn't one of those things mentioned in the Widgets API section of the WordPress Codex.

The above link has this in the Example when creating a widget:

If there were one more line (like the below), then this post wouldn't exist. The array that...

Full post »

Auto resize WordPress editor after unhiding it

Oct.15.2014 | 11:50 am

The project I am working on has several templates, some which do not use the main WordPress editor. For those templates, I typically hide the editor via jQuery's .hide() or .slideUp methods.

The problem I ran into was that when editing a page using a template that did not use the editor, then switching the template to one that used the editor, the editor would be broken until the user scrolled, resized the window, etc. This is due to how WordPress 4.0 now resizes the editor window automatically to fit the content in it.

The trick to fix this is to turn the auto resizing off and on again, which fires off all of the auto resize functions. The methods are:

Full post »

Getting a WordPress page by its template

Oct.13.2014 | 01:11 pm

When I create a WordPress theme, I like to make things dynamic, clean and easy for the user(s) of the theme. Today I came across an issue where I needed to be able to link back from a single custom post type to the page that is using the template that displays the listing of the custom post type. The page with the template is currently named 'Portfolio' and it displays all of the 'Galleries', the link will take the visitor back to the 'Portfolio' from a 'Gallery'...exciting!

I could use get_permalink( ID ) to get the permalink by the ID, or get_page_by_path( 'Portfolio' ) to get the page (and then its ID) by its name; but what if the user decides to use a differe...

Full post »