• Tried using add_post_meta($_user->ID, "info", $data['info'], $unique=true);
    but if i try to use update_user_meta($_user->ID, "info", $data['info']); it can be changed again.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    The various update_*_meta() functions are object specific wrappers for update_metadata(), which handles all meta updates for WP. You can prevent this function from doing updates through the “update_{$meta_type}_metadata” filter. If your callback returns anything but null the function returns without doing anything.

    This does not prevent other functions from altering data, such as an UPDATE query run through $wpdb methods.

    Thread Starter slate

    (@meanwood)

    Yeah, how would a snippet for this look like?

    Moderator bcworkz

    (@bcworkz)

    It’s not clear if you intend to save user or post data. I’ll assume user. You can add additional filters using the same callback if you want to protect more than user meta. In functions.php or a custom plugin:

    add_filter('update_user_metadata', 'my_meta_protection', 10, 5 );
    function my_meta_protection( null, $object_id, $meta_key, $meta_value, $prev_value ) {
      // only lock "info" meta, but allow user ID 1 to alter it
      if ( 'info' != $meta_key || 1 == get_current_user_id()) return null;
      return true;
    }
    Thread Starter slate

    (@meanwood)

    This works perfectly, thank you so much!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Locking a user meta_data field from updating it’ is closed to new replies.