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.
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.
Share Improve this question edited Feb 29, 2020 at 12:12 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Feb 27, 2020 at 23:56 Yahya LutfYahya Lutf 1 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. – Jacob Peattie Commented Feb 27, 2020 at 23:58
1 Answer
Reset to default 1You 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.