Skip to content

Instantly share code, notes, and snippets.

@kimcoleman
Last active April 8, 2021 22:08
Show Gist options
  • Save kimcoleman/a4f03068e49ea15f3a509a24512e5cca to your computer and use it in GitHub Desktop.
Save kimcoleman/a4f03068e49ea15f3a509a24512e5cca to your computer and use it in GitHub Desktop.
This code will filter ACF fields added to custom templates using the post's membership requirements.
<?php
/**
* pmpro_hide_acf_fields This code will filter ACF fields added to custom templates using the post's membership requirements.
*
* Add this code to your PMPro Customizations Plugin
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*
* https://www.advancedcustomfields.com/resources/acf-format_value/
*
* @param mixed $value Value which was loaded from the database
* @param mixed $post_id Post ID value was loaded from
* @param array $field Array containing all the field settings for the field which was used to upload the attachment
*
* @return mixed Returned value if member has access
*/
function pmpro_hide_acf_fields( $value, $post_id, $field ) {
// Check if the user has access to the post.
if ( function_exists( 'pmpro_has_membership_access' ) ) {
$hasaccess = pmpro_has_membership_access( $post_id );
}
if ( empty( $hasaccess ) ) {
// If user does not have acces to the post, empty the field value.
$value = '';
}
// return
return $value;
}
add_filter( 'acf/format_value', 'pmpro_hide_acf_fields', 10, 3 );
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Restrict the display of Advanced Custom Fields (ACF) fields by Membership Level" at Paid Memberships Pro here: https://www.paidmembershipspro.com/restrict-the-display-of-advanced-custom-fields-acf-fields-by-membership-level/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment