WordPress Designers and Developers in Minneapolis, MN

How to Limit Access to WordPress Pages by Specific User

I am working on a WordPress plugin that stores login information for websites and online services. I could use LastPass or 1Password for this, but:

  1. I don’t like the idea of shelling out extra money any time a member of my team needs access to a password.
  2. I like to control my own data.
  3. It is more fun to do it in WordPress.

I should note that I do use 1Password on my local machine to help me remember all of my logins. It is great for that.

Goal

My goal was to allow the admins the ability to grant specific users “view” privileges on a page-by-page basis

How I Did It

I considered a number of options when I got started:

I ended up going with Advanced Custom Fields’ “User” field. I would have rather gone with the Members plugin, but I was having problems getting it to respect my custom filters on the_content. Wishlist Member (and similar) are simply too bloated for my taste.

It took me a while to figure out how to grab the user data out of the ACF “User” field. That plugin is awesome, but sometimes documentation is lacking… Thankfully, support for the plugin is out of this world!

See my final code solution below:

<?php 
function does_user_have_access($content) {
 
	// Grab the current user's info so that we can compare it to the "allowed" users from the ACF "User" field later.
	$current_user = wp_get_current_user();
 
	// Store the ACF "User" info
	$values = get_field('user_info');
 
	if($values) { 
		// Create an array of users that will be able to access the page from the ACF "User" field
		$users_that_can_access_this_post = array();
		foreach($values as $value) {
			$user_IDs_that_can_access_this_post[] = $value['ID'];
		} 
		// Check to see if the current user is in the "User" field's array
		if (in_array($current_user->ID, $user_IDs_that_can_access_this_post, false) || current_user_can( 'manage_options' )) {
			// Display the post
			display_all_acf_fields();
		} else {
			// Hide the post content if the user is not in the ACF "User" array
			echo 'You do not have access to this post.  Please let Toby know if you do, indeed, need access.' . edit_post_link('Edit', '', ' ');
		}
	} else {
		// Display something if a post has no users set
		echo 'Please set the user restriction on this post.' . edit_post_link('Edit', '', ' ');
		die();	
	}
 
	return $content;
 
}
?>

Pretty nifty, eh?

Got any thoughts or ideas on how I can improve the above code? I would love to hear them! Please post a comment below if you have an idea.

Thanks!

Building Awesome Online Communities with WordPress

Below are my notes from today’s “Building Awesome Online Communities with WordPress” Minnebar session.

My name is Toby Cryns, and I have been building online communities with WordPress for the last 3 years.  I have been a full-time WordPress developer for 6 years.

Building online communities is no easy business.  In fact, it can be wrenching at times.  There are people problems, technical problems, and even money problems that can get in the way of building an awesome community.

Then there are the rewards of doing it right – smiles, victories, real connections with real people.

The interesting thing about building an online community is that many of the problems are technical, but almost all of the rewards are people-based.  Keep that in mind as we move forward.

 

This session is about people.  It is also about the tools that I have used to connect people with each other.

I would like to note that my wife is attending her first MinneBar today.  We met in an online community – okcupid.com.

I wasted years on Match.com – a meat market for daters.  It is every man, woman, and grandparent for herself on that site!  OkCupid.com was different.  It was a community.  I got the sense that people were looking out for me while I was on that site.  It was fun.  It was playful.  …and that was before I even went out on my first date!  hardy-har-har!

 

When I build online communities, I always think about how match.com made me feel and how okcupid.com made me feel.  Both sites have the same mission, but both sites have vastly different communities.  Why?

 

The WordPress Way

WordPress provides a wonderful foundation on which to build an online community.  But it is a tool.  It is not an end.  It takes real people and real work to build a great online community.

I actively manage many different types of communities that are built on WordPress.  There is the magazine-style, special interest, blog-comment-share community of Lavender Magazine.  We have the hands-on, discussion format, document-sharing, coalition-building powerhouse that is United Front.  You can’t see my 12-member, fond-memory-bringer-backer, Cryns Family Music Archives, but trust me – it is awesome!  Or perhaps you have interacted with the greatest user group in town, MSP WordPress

3 Keys to Success

  1. Define and be defined by your members/audience.  Be careful about who you let into your community.  Once you let people in, let them set the agenda.
  2. Seed discussions.  People are shy and busy.  Sometimes I need to prod and pull people into a discussion – just like in REAL LIFE!
  3. Address technical problems IMMEDIATELY!

 

Must-have plugins to run on every site:

Cryns Family Music Archives

  • A community of 12 people
  • Custom post type for songs
  • Interactions take place on comments
  • UI/UX: Simplified media upload interface via our “Magic Admin” plugin
  • UI/UX: Duplicate post plugin to make it easier to upload songs with similar meta-data (album name, artist, producer, etc.)
  • UI/UX: WP Email Template
  • TOOLS: Magic Admin, custom post types

United Front

  • A curated community of 1100+ people
  • BuddyPress Profiles
  • BuddyPress Forums
  • Blog Comments and GD Star Rating for Race to the Top
  • UI/UX: Vastly-simplified BuddyPress interface.
  • UI/UX: Turned off lots of BuddyPress features and focused on forums
  • UI/UX: Continue to invest time making system-generated emails more engaging and relevant
  • STAFF: 2 full-time staff members, plus my team.
  • TOOLS: WordPress Multisite + BuddyPress

Lavender Magazine

  • 30,000+ community of GLBT and GLBT allies
  • Blog comments are a big vehicle of engaging the community.  Also podcasts.
  • TOOL: Blubrry PowerPress
  • STAFF: 4-person editorial and design team, plus my team

MSP WordPress

  • 600+ registered community members
  • BuddyPress
  • Google Groups
  • Meetup.com
  • Used MSPWP.com’s BuddyPress installation to plan WordUp

 

Things I have learned over the years

  • User experience should be the primary focus of any community.  Design and functionality are secondary.
  • Building and managing an online community requires significant time and resources.  It is not a “set it and forget it” enterprise.  I am continually tweaking, updating, and fixing existing functionality.
  • Features are important.  Focus on core functionality.
  • Start small and grow.  Call it “beta” if you have to.  Be okay with a very small group for a long period of time.
  • Do user testing!  Ask questions of your users.
  • Usable design is more important than beautiful design.
  • Communities are about PEOPLE.  They are not about features, tools, or websites.
  • Use WordPress for web pages, blogs, comments, forums, profiles.
  • Use non-WordPress tools for payments, shopping carts, email lists.
  • Technical support is a necessity!  Only take on as much technology as you know how to support (or as much as you have money to pay someone else to support).
    For example, if you have no money and only know how to manage a WordPress blog, then run a community on a WordPress blog.  Don’t take on technologies that you can’t support!  (I see this with online shopping carts ALL THE TIME).

Tools of the Trade

…To be continued at BuddyCamp on April 28!

Comments Off

How to Display Posts in a Specific Category on your WordPress Search Results Page

Recently, I needed a search form that searched and resulted in posts from a specific category. I solved it by adding a hidden input field to my searchform.php file as follows:

<form class="search" role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
    <div>
        <input type="text" value="Search Staff Pages" name="s" id="s" onclick='javascript: this.value = ""' />
        <input type="image" src="<?php bloginfo('stylesheet_directory'); ?>/img/search-sidebar-background-button.png" id="searchsubmit">
        <input type="hidden" name="cat" id="cat" value="21" /> 
    </div>
</form>

Note, in the code above, I am searching posts in the category whose id is 21.

Pretty nifty, eh?

Thanks to WP Explorer for the idea.

Pagination With query_posts in a Custom WordPress Page Template

Every now and then I run into the problem where pagination doesn’t work in a custom page template. The problem usually lies in my handling of query_posts.

The solution is to write the query as follows:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
  'author' => 4,
  'paged' => $paged
);
 
query_posts($args); 
?>

[source]

Enjoy!

Comments Off