Title: Programmatically assign read permissions
Last modified: March 19, 2019

---

# Programmatically assign read permissions

 *  Resolved [doubleedesign](https://wordpress.org/support/users/doubleedesign/)
 * (@doubleedesign)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/programmatically-assign-read-permissions/)
 * Hi,
 * I am building functionality where pages are created programmatically when certain
   steps are taken (this part is working fine).
 * Is it possible to assign read permissions on a page to a particular group programmatically?
 * Here’s what I have so far. Note that the page and the group’s slugs are, at this
   stage, assumed to match (as the page is created programmatically when the group
   is created).
 *     ```
       /**
        * When a new group is created, assign read capabilities to their page
        * (relies on doublee_create_group_page to create the page)
        *
        * @param $group_id
        */
       function doublee_assign_group_page_access($group_id) {
   
       	// Get the group name and slug for later use
       	$group_name = doublee_get_group_name($group_id);
       	$group_slug = doublee_build_title_slug($group_name);
   
       	// Get the page ID from the group slug (assumes they match)
       	$page_id = doublee_get_group_page_id($group_slug);
   
       	// Set up array for the post meta. We can assume no-one is currently set to read this page because it'd brand new
       	$can_read = array();
       	array_push($can_read, $group_id);
   
       	// Update the post meta with the new read capability
       	update_post_meta($page_id, 'groups-read', $can_read);
       }
       add_action('groups_created_group', 'doublee_assign_group_page_access', 10);
       ```
   
 * …but it doesn’t work. Is `update_post_meta` the correct way to assign the read
   capability? Is there something that the plugin provides that could be used to
   do this?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [gtsiokos](https://wordpress.org/support/users/gtsiokos/)
 * (@gtsiokos)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/programmatically-assign-read-permissions/#post-11339077)
 * Hi,
 * Yes, the group restriction is a post meta value.
    Although I haven’t run your
   code, the meta_value for meta_key groups-read should be the group id. If you 
   are calculating the group_id and the page_id correctly, then it should work.
 * Hope it helps.
    George
 *  Thread Starter [doubleedesign](https://wordpress.org/support/users/doubleedesign/)
 * (@doubleedesign)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/programmatically-assign-read-permissions/#post-11454102)
 * Thank you for the quick reply George, and apologies that it has taken me so long
   to reply as I only just came back to this.
 * I had been trying to assign an array of IDs instead of one, which isn’t really
   necessary as I’m doing one group per page, I just wrongly thought that was the
   correct format.
 * I also combined this with the creation of the page, as I learnt that `wp_insert_post`
   returns the page ID if assigned to a variable, so the two functions (page creation
   and group permission assignment) do not need to be separate.
 * (I also believe that way is better as combining both steps into the one function
   removes the risk of the page ID being incorrect – as it’s been created immediately
   before the permission assignment with no opportunity for another function or 
   user action to interfere.)
 * Here is the updated, working code – in terms of the specific problem this post
   was about, I just removed the step to create an array containing `$group_id` 
   and popped that variable straight into the `update_post_meta` call.
 *     ```
       /**
        * When a new group is created, create their page
        * and assign read capabilities
        * @param $group_id
        */
       function doublee_create_group_page($group_id) {
   
       	// Get the group name
       	$group_name = doublee_get_group_name($group_id);
   
       	// Set up the page
       	$post_data = array(
       		'post_title'    => $group_name,
       		'post_type'     => 'page',
       		'post_status'   => 'publish',
       		'post_name'     => doublee_build_title_slug($group_name),
       	);
   
       	// Create the page and return its ID as $page_id
       	$page_id = wp_insert_post($post_data, true);
   
       	// Update the post meta with the read capability for the group
       	update_post_meta($page_id, 'groups-read', $group_id);
   
       }
       add_action('groups_created_group', 'doublee_create_group_page', 5);
       ```
   
    -  This reply was modified 7 years, 1 month ago by [doubleedesign](https://wordpress.org/support/users/doubleedesign/).
    -  This reply was modified 7 years, 1 month ago by [doubleedesign](https://wordpress.org/support/users/doubleedesign/).
    -  This reply was modified 7 years, 1 month ago by [doubleedesign](https://wordpress.org/support/users/doubleedesign/).
      Reason: Clarity
 *  [gtsiokos](https://wordpress.org/support/users/gtsiokos/)
 * (@gtsiokos)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/programmatically-assign-read-permissions/#post-11711347)
 * I’m glad it’s working and thank you for sharing your code in case someone else
   finds it useful.
 * Cheers,
    George

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Programmatically assign read permissions’ is closed to new replies.

 * ![](https://ps.w.org/groups/assets/icon-256x256.png?rev=983146)
 * [Groups](https://wordpress.org/plugins/groups/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/groups/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/groups/)
 * [Active Topics](https://wordpress.org/support/plugin/groups/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/groups/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/groups/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [gtsiokos](https://wordpress.org/support/users/gtsiokos/)
 * Last activity: [6 years, 11 months ago](https://wordpress.org/support/topic/programmatically-assign-read-permissions/#post-11711347)
 * Status: resolved