I have been working on a project where I want to display a shortcode based on the user meta data. I have added a user meta while creating a new user. The default value of the user meta is set to false. Now, my logic is to check whether the user meta is 'false' or not at the time of login. I will be displaying the shortcode if the value is 'false' and after that again updating the user meta to 'true'. But, this is not working. The new created user when logs in, is not able to see any shortcode. Can find my mistake?
This code adds user meta for all the newly created users
add_filter( 'insert_user_meta', 'new_user_meta', 20, 3);
function new_user_meta( $meta, $user, $update )
{
if ( $update )
return $meta;
$meta['GDPR_TERMS_READ'] = false;
return $meta;
}
This piece of code checks whether the user meta is false or not to Display the shortcode
add_action( 'init', 'gdpr_flag' );
function gdpr_flag() {
$current_user = get_current_user_id();
if ( ! $current_user ) {
// user not logged in
return;
}
if ( get_user_meta( $current_user, 'GDPR_TERMS_READ', true ) ) {
// user meta set already
return;
}
// render some html
echo do_shortcode( '[wpterms id="2230"]' );
// update user meta
updateHasReadFlag( $current_user );
}
function updateHasReadFlag($user) {
// I added support for using this function either with user ID or user object
if ( $user && is_int( $user ) ) {
$user_id = $user;
} else if ( ! empty( $user->ID ) ) {
$user_id = $user->ID;
} else {
return;
}
return update_user_meta( $user_id, 'GDPR_TERMS_READ', true );
}
This is the code generated for pop up from the plugin
<div id="tlight" class="tbrightcontent">
<div class="termspopupcontainer">
<h3 class="termstitle">GDPR</h3>
<div class="termscontentwrapper">
<p>This is a GDPR pop up.</p>
<p> </p>
<p> </p>
<form><input name="accept" type="checkbox" value="Gdpr">I accept the GDPR</form>
</div>
<form method="post">
<div class="tthebutton">
<input class="termsagree" name="wptp_agree" type="submit" value="Accept">
<input class="termsdecline" type="button" onclick="window.location.replace('')" value="I Do Not Agree">
</div>
</form>
</div>
</div>
I have been working on a project where I want to display a shortcode based on the user meta data. I have added a user meta while creating a new user. The default value of the user meta is set to false. Now, my logic is to check whether the user meta is 'false' or not at the time of login. I will be displaying the shortcode if the value is 'false' and after that again updating the user meta to 'true'. But, this is not working. The new created user when logs in, is not able to see any shortcode. Can find my mistake?
This code adds user meta for all the newly created users
add_filter( 'insert_user_meta', 'new_user_meta', 20, 3);
function new_user_meta( $meta, $user, $update )
{
if ( $update )
return $meta;
$meta['GDPR_TERMS_READ'] = false;
return $meta;
}
This piece of code checks whether the user meta is false or not to Display the shortcode
add_action( 'init', 'gdpr_flag' );
function gdpr_flag() {
$current_user = get_current_user_id();
if ( ! $current_user ) {
// user not logged in
return;
}
if ( get_user_meta( $current_user, 'GDPR_TERMS_READ', true ) ) {
// user meta set already
return;
}
// render some html
echo do_shortcode( '[wpterms id="2230"]' );
// update user meta
updateHasReadFlag( $current_user );
}
function updateHasReadFlag($user) {
// I added support for using this function either with user ID or user object
if ( $user && is_int( $user ) ) {
$user_id = $user;
} else if ( ! empty( $user->ID ) ) {
$user_id = $user->ID;
} else {
return;
}
return update_user_meta( $user_id, 'GDPR_TERMS_READ', true );
}
This is the code generated for pop up from the plugin
<div id="tlight" class="tbrightcontent">
<div class="termspopupcontainer">
<h3 class="termstitle">GDPR</h3>
<div class="termscontentwrapper">
<p>This is a GDPR pop up.</p>
<p> </p>
<p> </p>
<form><input name="accept" type="checkbox" value="Gdpr">I accept the GDPR</form>
</div>
<form method="post">
<div class="tthebutton">
<input class="termsagree" name="wptp_agree" type="submit" value="Accept">
<input class="termsdecline" type="button" onclick="window.location.replace('https://google')" value="I Do Not Agree">
</div>
</form>
</div>
</div>
Share
Improve this question
edited Jul 3, 2019 at 1:34
Subham
asked Jun 30, 2019 at 7:45
SubhamSubham
271 gold badge2 silver badges7 bronze badges
14
|
Show 9 more comments
1 Answer
Reset to default 0I assume the shortcode you're using renders some html. If this is the case, then you could consider hooking your function to some later action, maybe one in your theme, so that the shortcode content gets rendered along with all the other html. You could even consider using wp_footer
, if such late hook is ok.
You could then maybe write your functions something like this,
add_action( 'some_theme_hook', 'gdpr_flag' );
function gdpr_flag() {
$current_user = get_current_user_id();
if ( ! $current_user ) {
// user not logged in
return;
}
if ( get_user_meta( $current_user, 'HAS_READ', true ) ) {
// user meta set already
return;
}
// render some html
echo do_shortcode( "[wpterms id=2230]" );
// update user meta
updateHasReadFlag( $current_user );
}
function updateHasReadFlag($user) {
// I added support for using this function either with user ID or user object
if ( $user && is_int( $user ) ) {
$user_id = $user
} else if ( ! empty( $user->ID ) ) {
$user_id = $user->ID;
} else {
return;
}
return update_user_meta( $user_id, 'HAS_READ', true );
}
P.S. You might want to consider renaming the meta key, as "HAS_READ" doesn't tell much about what the meta data is for. Especially if some other developer happens to have a look at your code or works with the user meta some time later. Perhaps using something like "gdpr_term_read" could be more explicit.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745350073a4623777.html
true
here:get_user_meta($user->ID, 'HAS_READ', false)
. But thewp_authenticate
hook is called before the authentication cookies are set, so you shouldn't be echoing anything from your hook callback. – Sally CJ Commented Jun 30, 2019 at 12:59[wpterms id="2230"]
? "Little information about my shortcode is that, its a pop up." - are you trying to show a popup once per login/user session? I.e. It's shown each time after the user logs in? – Sally CJ Commented Jul 2, 2019 at 4:44