admin管理员组文章数量:1289583
Basically I'm looking for something like this:
<?php if ( is_page( array( 'home' ) ) ): ?>
update_user_meta( $user_id, 'pagehome', '1' );
<?php else: ?>
update_user_meta( $user_id, 'pagehome', '0' );
<?php endif; ?>
I want to change usermeta data for current user depending on which page they visited.
So I need to find a simple code to fire update_user_meta( $user_id, 'pagehome', '1' );
and record into database.
Any help would be appreciated.
Basically I'm looking for something like this:
<?php if ( is_page( array( 'home' ) ) ): ?>
update_user_meta( $user_id, 'pagehome', '1' );
<?php else: ?>
update_user_meta( $user_id, 'pagehome', '0' );
<?php endif; ?>
I want to change usermeta data for current user depending on which page they visited.
So I need to find a simple code to fire update_user_meta( $user_id, 'pagehome', '1' );
and record into database.
Any help would be appreciated.
Share Improve this question asked Jul 14, 2021 at 11:23 robert0robert0 2032 silver badges11 bronze badges1 Answer
Reset to default 2There are lots of actions you can hook into to do this, I think the best once to use would be wp
or template_redirect
.
Using either of those actions the code would be like this, this code goes into the functions.php
add_action('wp', 'bt_update_user_homepage_meta');
function bt_update_user_homepage_meta () {
// get user id, if user is not logged in then it will be 0
$user_id = get_current_user_id();
// now we check if we are in front page or not and we update the user meta accordingly
// if user is not logged in this code will try to update the meta for user 0,
// because this user doesn't exist, nothing will happen
if (is_front_page()) update_user_meta($user_id, 'pagehome', '1');
else update_user_meta($user_id, 'pagehome', '0');
}
本文标签: phpHow to update and save user metadata on page visits
版权声明:本文标题:php - How to update and save user metadata on page visits? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741443108a2379032.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论