Array Question

22 views
Skip to first unread message

Toby Cryns

unread,
May 8, 2013, 2:59:37 AM5/8/13
to mpls-stpau...@googlegroups.com
I am using Advanced Custom Fields' "User" field.  

I want to select a couple of users on each page and restrict access to that specific page to those users that I have selected from the multi-select.

When I do a print_r on the "user" custom field, I get the following: 

Array ( [0] => Array ( [ID] => 7 [user_firstname] => Jim [user_lastname] => Smith [nickname] => eric [user_nicename] => jim [display_name] => Jim Smith [user_email] => j...@jimsmith.com [user_url] => [user_registered] => 2013-05-08 05:40:42 [user_description] => [user_avatar] => IMAGE_HERE ) [1] => Array ( [ID] => 8 [user_firstname] => Test [user_lastname] => User [nickname] => toby2 [user_nicename] => toby2 [display_name] => Test User [user_email] => to...@cryns.com [user_url] => [user_registered] => 2013-05-08 05:56:52 [user_description] => [user_avatar] => IMAGE_HERE ) )

I want to grab the current user's id and compare it to the ids in the above array.  Then, if the current user's id matches up with one of the ids in the array, I will display the page content.

I understand the logic here, but I need some help constructing the array parsing function.  Any thoughts on how I might do this?

Thanks!
Toby

Jerry Milo Johnson

unread,
May 8, 2013, 8:15:59 AM5/8/13
to mpls-stpau...@googlegroups.com
to access the ID key in the user array for the first item would be

$newvalue = $user[0]->ID;

or

if ( 9 == $user[0]->ID ) { echo 'Yeah!'; }

if you would rather walk the outer array, checking each one

foreach ( $user as $user_item ) {

if ( 9 == $user_item->ID ) { echo 'Yeah!'; }

}


your use case:

$current_user = wp_get_current_user();

foreach ( $user as $user_item ) {

if ( $current_user->ID == $user_item->ID ) { echo 'Yeah!'; }

}


or, if you are feeling evil to future maintenance programmers,

if ( in_array( wp_get_current_user()->ID, wp_list_pluck( $users, 'ID' ), true ) ) echo 'Show me the money';



I hope this remotely answers the question you were asking. Too early for confidence.

No code was injured or inconvenienced or tested in any way during this answer.

Jerry Milo Johnson



--
You received this message because you are subscribed to the Google Groups "Minneapolis St. Paul WordPress User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mpls-stpaul-word...@googlegroups.com.
To post to this group, send email to mpls-stpau...@googlegroups.com.
Visit this group at http://groups.google.com/group/mpls-stpaul-wordpress?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

JustinF

unread,
May 8, 2013, 9:34:24 AM5/8/13
to mpls-stpau...@googlegroups.com
Speaking of arrays... I put together a 20 slide talk/discussion about PHP arrays for the last meetup, but we didn't have enough time with only one room.  I can save it for a future meetup or we can go over it on WordPress Wednesday sometime.

Justin

Nicholas Ciske

unread,
May 8, 2013, 11:22:28 AM5/8/13
to mpls-stpau...@googlegroups.com
Toby,

Curious why you're going the per user route vs. a group or capability model?

Capabilities are built into WordPress -- you can add custom roles & capabilities via a role manager plugin (e.g. sales_manager can view_sales_reports) and check for those with current_user_can('view_sales_reports').

Groups should not be terribly hard to add with ACF and would make maintenance and upkeep simpler (e.g. with turnover and the like).

Toby Cryns

unread,
May 21, 2013, 4:54:54 PM5/21/13
to mpls-stpau...@googlegroups.com
I wanted to loop back on this discussion thread.

@Justin, Can we get your array presentation on the docket for June?

@NickC, The reason I don't want to use roles is because I want to limit access by user.

@Jerry, Thanks for the guidance; it helped a ton!

I wrote a blog post about how I solved my user restriction problem: http://www.themightymo.com/2013/05/20/how-to-limit-access-to-wordpress-pages-by-specific-user/

At the end of the day, I know there are plugins that could have gotten me most/all of the way there, but I wanted to play around with arrays a bit.

Thanks for all the help!

Toby
Reply all
Reply to author
Forward
0 new messages