最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

users - how to set default update_user_meta values wordpress

programmeradmin1浏览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.

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
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论