0

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>&nbsp;</p>
<p>&nbsp;</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.com')" value="I Do Not Agree">
            </div>
        </form>
    </div>
</div>
  • You're retrieving a single value, so the third parameter should be true here: get_user_meta($user->ID, 'HAS_READ', false). But the wp_authenticate hook is called before the authentication cookies are set, so you shouldn't be echoing anything from your hook callback. – Sally CJ Jun 30 '19 at 12:59
  • sorry i did not get you. you mean to say that echo do_shortcode( "[wpterms id=2230]" ); wont work here. I have updated my code, please have a look. – Subham Jun 30 '19 at 13:26
  • will add_action('wp_login', 'gdprFlag'); work instead of wp_authentiate? – Subham Jun 30 '19 at 13:48
  • Can you show a sample output of the shortcode - what's the output or the expected output of [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 Jul 2 '19 at 4:44
  • Yes Sally, you are right. The shortcode is just a simple pop up with a button “Accept”. In reality, I will calling updateHasReadFlag() from the onClick of the “Accept” button. So that once the user accepts the the GDPR popup, he will never be able to see the pop up again. Hope I have explained correctly. – Subham Jul 2 '19 at 5:07
0

I 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.

| improve this answer | |
  • Hi, Thanks for your interest. I have hooked my function to wp_footer but somehow the shortcode is not getting rendered. Do you have any idea about that? – Subham Jul 1 '19 at 16:18
  • @Subham, update your question with the code you've attempted in the wp_footer hook. And the theme's footer.php does call wp_footer() like this, right? – Sally CJ Jul 2 '19 at 0:34
  • I have hooked it to the 'init' hook. My theme calls 'init' and moreover I can see that the user meta is getting inserted and updated for a new user. All of the code is running fine except the main part(displaying the shortcode). Little information about my shortcode is that, its a pop up. – Subham Jul 2 '19 at 0:46
  • 1
    @Subham, if your theme does not call wp_footer(), add <?php wp_footer(); ?> to footer.php, before </body>. Secondly, are you sure the popup HTML is not in the page source? Maybe the popup is just being visually hidden via CSS. Check also the console for errors, including missing resources like a JS file/script. – Sally CJ Jul 3 '19 at 8:06
  • @SallyCJ I am facing issue while calling the updateHasReadFlag() from the click of Agree button using AJAX. Can demonstrate or share any link where I can get an idea. Thank you. – Subham Jul 4 '19 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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