0

so i installed a theme which gives a default value to a meta

the code the theme uses is

      update_user_meta($uid, '_sb_pkg_type', 'free');

i would like to add this code to wordpress core so whenever a user registers it sets a default value of 'free' to _sb_pkg_type instead of nothing..

meaning i would like to know how can i set a default user meta to _sb_pkg_type in WordPress core

sorry if this question is not understood as my English is not my first languge.

1
  • 1
    You shouldn't be modifying WordPress core. You would need to create a plugin that just does the same thing that the theme does. Commented Feb 27, 2020 at 23:58

1 Answer 1

1

You can use the user_register action to run your user meta update when a user registers.

/**
 * Sets the user meta key '_sb_pkg_type' to 'free' when a user registers.
 *
 * @param int $user_id ID for the user who has registered.
 */
function wpse_update_user_meta_pkg_type( $user_id ) {
    update_user_meta( $user_id, '_sb_pkg_type', 'free' );
}
// Fire late to try to ensure this is done after any other function hooked to `user_register`.
add_action( 'user_register','wpse_update_user_meta_pkg_type', PHP_INT_MAX, 1 );

The code above uses the priority PHP_INT_MAX to try and ensure that it's the last thing triggered on the user_register hook, but without knowing how the plugin you're working with is handling the user meta, it's hard to say if this will be sufficient.

I did verify that this code updates the user meta key _sb_pkg_type with a value of free when a new user is registered though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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