March 5th, 2012
I’ve been using the excellent WP-Members plugin, to extend the Swansea Community Church website, and have been pleased with its ability to offer all the membership/registration functionality that I need to add a membership section.
However there a couple of elements that I wanted to add to it to focus it for my needs.
This blog article will address (1), and I’ll focus on (2) in a second post.
Currently the user has to add the following custom fields to make a post public or private:
If a user does not set one of these values, the visibility will depend on the setting that they have set in the Settings -> WP Members screen, which if you don’t change any settings, defaults to block posts to non-logged in users.
Custom fields on their own (with no UI) have a couple of problems:
I should point out that this is no fault of the plugin author, rather WordPress continues to add functionality and has made it very easy to add a user interface over custom fields.
The solution to this is to create a meta box that will set these fields in a more user friendly interface.
We will create a meta box that will allow the user to select one of three states:
A set of radio buttons is the ideal solution for this – allowing users to choose only one option.
First we must hook into the ‘add_meta_boxes’ WordPress action. This fires when meta boxes are to be added to a post screen, and we pass it our function name that will draw the meta boxes.
We are using the add_meta_box function, passing it the id (arbitrary, but I normally set it to be the same as the callback function name), the title (‘Members’), our callback function name, the post type (more on this in a second), its position (on the side), and if it should appear at the top or the bottom (we’re happy with the default here).
We have to tell it which post type we are adding it to. Most examples add it to just ‘post’ and ‘page’, but what if your system is using custom post types (e.g. The Events Calendar uses an Event post type)? We can use the get_post_types() function to list out all the post types. We only care about the names of the post types so we call it with a second parameter of ‘names’ (so we can reduce the database query load).
We loop through the post types and add the meta box to each post type.
Now we need to implement our meta box callback function. This is the one that outputs the meta box UI itself.
Here we’re getting the meta box values. We first get the custom fields ‘block’ and ‘unblock’ using get_post_meta(). We decide if it’s got any of those fields set – if not, we know that the user is using just the ‘default’ setting.
Then we output the radio buttons, and set them to be ‘checked’ if the appropriate value has been set.
Now we need to be able to save the settings when the post type is saved. We use the ‘save_post’ action to save our custom fields:
First we do some default checking (you can also check a member’s capability and access level here, but I’ve left this out for clarity’s sake. See the add_meta_box example for more details).
Then we check whether our radio button field has been set in the $_POST array, and depending on the value that was set, we use update_post_meta() to set the value, and delete_post_meta() to delete the value that we do not want set (in case the user has changed their mind).
And that’s it, you’ve now got a nice user interface that less technical people can use to administer your membership-based website, allowing them to set posts to be public or private without knowing the details of how to set custom fields.
Let me know if this was useful to you, or if you have any comments. Check back in soon for the second part addressing the visibility of posts.
Tags: add_meta_box, Wordpress, WP Members
This entry was posted on Monday, March 5th, 2012 at 11:01 and is filed under Uncategorized, Wordpress. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Take your website to the mobile arena
Website development - custom, low cost, high quality, high impact.
Mobile App Development for iPhone, iPad, and Android
Custom websites, website management, website reports & more
Hello,
First thank you for this article that I find great because it will make my life easier. However, novice, I have a question to ask yourself.
Everything works very good, but the authors not accessing the meta box in a post or page ; reserved only for administrator. Is there a setting, a line of code to add in “add_action( ‘add_meta_boxes’, ‘ds_members_addmetabox’ );”.
Thank you very much for your reply and cordially.