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

jquery - Disable button after one click per user per post

programmeradmin0浏览0评论

Can someone suggest some possible solution to how do I achieve the following in WordPress?

I have a custom post type named Deals. On each deal page, I want to have a button which upon clicking will show some coupon code on the popup and then that button will be disabled for that particular user forever.

I know how to create a button and show popup, but what will be the way to disable it for that particular user after that?

Can someone suggest some possible solution to how do I achieve the following in WordPress?

I have a custom post type named Deals. On each deal page, I want to have a button which upon clicking will show some coupon code on the popup and then that button will be disabled for that particular user forever.

I know how to create a button and show popup, but what will be the way to disable it for that particular user after that?

Share Improve this question edited Mar 9, 2020 at 11:34 sleepingpanda 566 bronze badges asked Mar 8, 2020 at 15:10 vipworksvipworks 731 silver badge9 bronze badges 2
  • 1 Is the user logged in? – fuxia Commented Mar 8, 2020 at 15:39
  • Yes, the button is visible and functionable only for logged in users. – vipworks Commented Mar 8, 2020 at 15:40
Add a comment  | 

1 Answer 1

Reset to default 0

Use a user meta entry to store the information that this user has received the coupon already. Then check this user meta before you print the button:

Plugin code:

function coupon_button()
{
    $post_id          = get_the_ID();
    $user_id          = get_current_user_id();
    $coupons_received = (array) get_user_meta( $user_id, 'coupons_received', true );

    if ( in_array( $post_id, $coupons_received ) )
        return '';

    return get_submit_button( 'Get Coupon', '' );
}

function update_coupon_status()
{
    $user_id            = get_current_user_id();
    $coupons_received   = (array) get_user_meta( $user_id, 'coupons_received', true );
    $coupons_received[] = get_the_ID();
    update_user_meta( $user_id, 'coupons_received', $coupons_received );
}

Template code:

print coupon_button();

And when you show the coupon, use the update function:

update_coupon_status();
发布评论

评论列表(0)

  1. 暂无评论