0

I've tried reading all the questions about this topic, and configured the code to fit my needs, but can't seem to get it to work. This is the code I have now:

function my_exclude_custom_fields( $protected, $meta_key, $user ) {

    $user = wp_get_current_user();
    if ( in_array( 'udlejer', (array) $user->roles ) ) {
        if ( in_array( $meta_key, array( 'email', 'text' ) ) ) {
            return true;
        }
    }

    return $protected;
}
add_filter( 'is_protected_meta', 'my_exclude_custom_fields', 10, 2 );

It doesn't make any change. Even though I'm logged in with another role than udlejer, I can still see these custom fields in front-end.

The custom fields are on a product page created by WooCommerce. Do I then need to include more to the code then? Hope you guys can help!

  • Where is $currentUser coming from? Your codes formatting is broken, can you fix it? Code is easier to read/fix with good formatting :) – Tom J Nowell Mar 27 '18 at 18:12
  • I think it's fixed now. Thanks for pointing it out :) I'm new at coding. But still not working. – Mark Nielsen Mar 27 '18 at 18:24
0

You can use the current_user_can() function to test the current user's role. I would also recommend making the custom field protected by default and allowing access only to the proper users.

In theory, something like this should work:

function my_exclude_custom_fields( $protected, $meta_key) {

    if ( in_array( $meta_key, array( 'email', 'text' ) ) ) {
        if ( current_user_can( 'udlejer' ) ) {
            // meta key is not protected for user role 'udlejer'
            return false;
        } else {
            // make the meta keys protected by default
            return true;
        }
    }

    return $protected;
}
add_filter( 'is_protected_meta', 'my_exclude_custom_fields', 10, 2 );
  • Thanks for the reply! It gives me an error regarding: 'if ( current_user_can( 'udlejer' ) ) {' - syntax error, unexpected 'if' (T_IF) - What to do in this case? I've tried adding semicolon, but still same error. – Mark Nielsen Mar 28 '18 at 10:47
  • Oops! It looks like I missed a ")" to end the IF statement that is checking the meta key (the first IF). I updated the code to fix the error. – Marc Mar 28 '18 at 13:48
  • Thanks for quick reply and fix! No errors now, but all users can still view the custom fields on the product page :/ – Mark Nielsen Mar 28 '18 at 14:48
  • Hmmm... How did you create the 'udlejer' role? You could experiment with testing against an existing role or capability. Those can be seen here: codex.wordpress.org/Roles_and_Capabilities It may also be worth troubleshooting by returning false after the first IF no matter what to make sure that protecting the meta_key will even hide it from users. – Marc Mar 28 '18 at 14:54
  • It's created with Paid Memberships Pro. I figured out, that udlejer is - pmpro_role_3 and changed it in the code, but still no difference. Afterwards i tried return false; after the first IF, but still nothing different. The custom fields are created with WP User Frontend Pro, as meta keys - "email" and "text" – Mark Nielsen Mar 28 '18 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.