Removing user contact methods works from functions.php but not from a plugin

I’d like to remove user fields and I used this code:

add_filter( 'user_contactmethods', 'newfields' );
function newfields( $user_contact ) {

    return $user_contact;

If I use this code in functions.php of my theme, it works, but if I use this code in a plugin, it doesn’t work.

Why? How can I resolve it?

Solutions Collecting From Web of "Removing user contact methods works from functions.php but not from a plugin"

This could be the matter of adjusting the priority of your filter callback,
since the plugins files are executed before the functions.php theme file. That could explain why it works in your functions.php file, if the priority is the same.

When you add the code snippet to a plugin, it might be executed before the other plugins that use the same filter.

The default priority is 10, so you should try something higher in your plugin code, for example:

add_filter( 'user_contactmethods', 'newfields', 99 );

or even

add_filter( 'user_contactmethods', 'newfields', PHP_INT_MAX );

if you want to be absolutely sure your filter callback is the last one to be executed – assuming the other plugins are not using PHP_INT_MAX as well.

But better yet, just check the priority the user_contactmethods filter callbacks the other plugins are using.