Category: Development

Multisite Post Duplicator – Actions and Filters

v06

For the latest version of my WordPress plugin (version 0.6) I have added various actions and filters to allow anyone to create addons or to extend the plugins functionality without having to edit the core.

Actions

‘mpd_before_core’ inc/core.php Perform actions just before duplication begins
‘mpd_during_core_in_source’ inc/core.php Just after data has been collected form source post but before we being to process the new post
‘mpd_end_of_core’ inc/core.php Immediately after all duplication actions have been performed
‘mpd_before_metabox_content’ inc/postform_ui.php Before the metabox markup is displayed
‘mpd_after_metabox_content’ inc/postform_ui.php After the metabox markup is displayed
‘mdp_start_plugin_setting_page’ in/settings-ui.php After settings page has been registered but before and settings fields have been generated
‘mdp_end_plugin_setting_page’ in/settings-ui.php After all core setting fields have been registered

Filters

‘mpd_activation_options’ mpd.php add/edit/delete the option fields added on activation of plugin
‘mdp_default_options’ mpd.php add/edit/delete the default values of each setting in mpd
‘mpd_is_active’ various locations change logic determining if plugin tools are available or not
‘mpd_setup_destination_data’ inc/core.php Hook into the post values for the destination post
‘mpd_returned_information’ inc/core.php add/edit/delete the information on the created post returned by the core function
‘mpd_ignore_post_types’ inc/mpd-functions.php Hook into the array which stores the post types that the mpd metabox will not display on.
‘mpd_featured_image’ inc/mpd-functions.php Filter details on the copied featured image
‘mpd_featured_image_filename’ inc/mpd-functions.php Do stuff with the copied featured image’s filename only
‘mpd_alt_tag_array_from_post_content’ inc/mpd-functions.php Do stuff with the alt tags for the images within the new post content
‘mpd_admin_notice_text’ inc/mpd-functions.php Hook into the ‘success’ admin notice.
‘mpd_global_filter_sites’ inc/mpd-functions.php Control the sites on your network that appear in the MPD functionality
‘mpd_show_settings_page’ inc/settings-ui.php Control the logic that determines when to show the Settings page
‘mpd_source_data’ inc/core.php Control the source data being added to the core duplication process
‘mpd_available_post_statuses’ inc/mpd-functions.php Control what post statuses are available to the user

Please note that this is a very brief summary of the filters and actions, for greater detail into parameters and data that you can gain access to with these hooks please check the source code.

More

Event Espresso 4 – Helpful Functions

event-espresso-logo-600x213-black1-600x198

I’ve recently been working on a project using Event Espresso 4 and I’ve had to customize it quite extensively. I wrote a few functions that I think might be handy for others so I thought I would share them:

Get Event ID From Registration Code

I wrote this one to help me get the event information when a user clicks on a ‘cancel registration’ link. The reg code is more secure way to pass as a GET variable in a link than the user/event/registration id.

The Function

Usage Example

Get Event Date

This one is handy if you have the event id and you are just looking for the date of the event.

The Function

Usage Example

Get the Status of the Registration From Registration Code

This one can be used to see the status of a particular registration. I wrote it to provide confirmation that a registration had been cancelled in a custom built ‘cancellation journey’.

The Function

This function returns a status code:

“RAP” (registration status of approved. This is the default behavior.)
“RCN” (registration status of cancelled.)
“RDC” (registration status of declined.)
“RIC” (registration status of incomplete.)
“RNA” (registration status of not approved.)
“RPP” (registration status of pending payment.)

Usage Example

Get the Status of the Event From Registration ID

Same as above just using the Registration ID.

The Function

This function returns a status code:

“RAP” (registration status of approved. This is the default behavior.)
“RCN” (registration status of cancelled.)
“RDC” (registration status of declined.)
“RIC” (registration status of incomplete.)
“RNA” (registration status of not approved.)
“RPP” (registration status of pending payment.)

Usage Example

Get Registrant Details From Registration ID

This function gives you the all the registration details of a Registrant if you have the registration ID.

The Function

This function returns an object with ‘first_name’, ‘last_name’ and ’email’ as the parameters.

Usage Example

 

More

Multisite Post Duplicator

Last post I mentioned a wee function I has written to help me with copying posts and pages from one multisite to another. Well I have packaged it into a plugin and submitted it to WordPress. After a few days I got notice that they approved it!

banner-1544x500

Check it out on GitHub also:

FireShot Capture - magicstickuk_multisi_ - https___github.com_magicstickuk_multisite-post-duplicator

It doesn’t finish here though. I have several ideas for future features. Stay tuned!

More

Multisite: Copy a Page From One Site to Another

photo-1421977870504-378093748ae6

I’ve been working on a multisite recently and my client requested something off the cuff that made me gulp. They said “I take it we will be able to copy a page from one site to the other to save us having to replicate it manually?”

Coping a page on the same site is pretty straightforward and there are a multitude of plugins out there that make it a pretty easy process. However. Doing this across a mulitsite is different. Not a lot of plugins out there.

wpmaz says*For this to work full with advanced custom fields you need to ensure your custom fields setup on both sites are the same!
I thought I would share a function I wrote today to achieve this. This is pretty basic at the moment but gives the core functionality and works with Advanced Custom Fields too*!

I’m pretty happy with this, but think I might make a wee plugin for it!

More

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