Monthly Archive: March 2015

Dynamically Naming Elements in Nested Loops

Wanted to share a technique I use quite a lot at the moment for dynamically naming elements in a loop or, more specifically for this method, in multiple nested loops.

Consider the following code which is pretty standard for most jquery modals at present.

To render these modals in a loop you might be familiar with the following code which I have seen a million times.  This set up makes sure that each link points to a corresponding container div with a matching unique id. Fine and dandy:

The problem with this method occurs when you are using this type of markup as a function or if you have some sort of dynamic setup where the above code may be outputted on the same page more than once. This would break the code as the link would just point to the first container div with that name.

I’ve seen setups with some very strange methods to get round this.

The Solution

Here is a simple solution that utilises php’s  uniqid function. This ensures you will NEVER get two id’s matching no mater how many nested loops you have going.

Implement it like so:

If you want to store the id’s of these for use at a later point, like I’ve done recently, you can do this:

Then you have access to the id’s later like so:

I’ve used this not just with modals, but when generating unique ids for other jQuery functions, when using with Advanced Custom Fields ‘Flexible Field’ addon to make sure all generated sections of content have a nice unique ids to point to.

More

Correct Capitalisation of WordPress

So yesterday, when I wrote my first blog post on this site, I found myself having to go back to my post two or three times because I had noticed that I had used the incorrect capitalisation on the word ‘WordPress’. Should be a capital W and P.

So in my warped mind, tonight I decided to write a wee plugin that will auto fix this issue whenever I publish or update a post. It’s probably the most straightforward plugin you could hope to write but I like the exercise as you never know what you might learn on the way.

What I Did

In WordPress there is a Filter called wp_insert_post_data this filter basically allows you to intercept the POST data from when the user presses ‘Publish’ or ‘Update’ on the CMS and edit the data in any way you want BEFORE the data is added to the database.

The syntax for running this filter looks like this:

So in my function I have basically done a find and replace and returned the new data for WordPress to add to the database.

The Final Plugin:

Screen-Shot-2015-03-19-at-20.54.39

Download Now

More

ACF Option Fields on a Multisite

Get Options Fields From Advanced Custom Fields on a WordPress Multisite Installation

file5509689aae448

I’ve recently been working on a multisite project where it became apparent that it would be nice to have a ‘global options’ page. This would hold values to be used over all other sites on that same WordPress multisite network. At the time of writing this post there is no way to achieve this with functions built into Advanced Custom Fields. So I wrote my own.

Detail

wpmaz says'wp_' is the default database prefix added when installing WordPress. This can different if it pleases you.
In Advanced Custom Fields when an Option Field is saved by a user in the WordPress CMS it is saved into the ‘wp_options’ table of the database.

Like so:

option_name option_value
options_date_of_event 20150410

The ‘option name’ can be broken up into two sections. First ‘options_’ which is the prefix for an Advanced Custom Field option key and then then name of the field which is, in the above example, ‘date_of_event’. We could create a WordPress function to get this option value by querying the database:

Note that the above example is redundant because you can already do the same thing much easier with:

However! The reason we are talking about this is if we wanted to get a value from another site on the same WordPress multisite installation!

To do this we need to look at how WordPress manages its multisite database.

In a normal installation we may see a list of tables like so:

wp_commentmeta, wp_comments, wp_links, wp_options, wp_postmeta, wp_posts, wp_terms, wp_term_relationships, wp_term_taxonomy, wp_usermeta, wp_users.

With each site that is added WordPress will increment the names of the tables in the database like this:

wp_2_commentmeta, wp_2_comments, wp_2_links, wp_2_options, wp_2_postmeta, wp_2_posts, wp_2_terms, wp_2_term_relationships, wp_2_term_taxonomy, wp_2_usermeta, wp_2_users.

Where the ‘2’ is actually the site or ‘blog’ ID number.

We can therefore adjust the query to be dynamic enough so we can point the query to the correct options table for a desired sub site on the multisite network.

The Function

Add this to your functions.php file:

You can now call the function in your template file(s) like:

Where ‘1’ is the ID for the site holding the Global options, or the site options you wish to use and ‘date_of_event’ is the ‘Advanced Custom Field’ field name.

Notes on Final Code

Line 5: This is an if statement in php shorthand to provide logic to get round the fact that the first table in the database (ID = 1) doesn’t have a numerical prefix.

Line 8: I used $wpdb->base_prefix incase the normal WordPress database prefix wasn’t used in your WordPress multisite installation.

More