php - Display shortcode based on user meta

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 me

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('')" 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>&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')" 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
  • 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 Commented Jun 30, 2019 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 Commented Jun 30, 2019 at 13:26
  • will add_action('wp_login', 'gdprFlag'); work instead of wp_authentiate? – Subham Commented Jun 30, 2019 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 Commented Jul 2, 2019 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 Commented Jul 2, 2019 at 5:07
 |  Show 9 more comments

1 Answer 1

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745350073a4623777.html

相关推荐

  • php - Display shortcode based on user meta

    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 me

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信