需要在我的wp网站的按钮上设置登录控件。 Button的代码如下所示:
$enable_redirect = get_field('redirect_to_offer') ? get_field('redirect_to_offer') : array('');
echo'<a href="' . esc_url(get_field('url')) . '" target="_blank" class="btn btn-color btn-deal show-coupon-code" data-target="#discount-modal" data-clipboard-text="' . esc_attr(get_field('coupon_code')) . '" data-redirect="' . $enable_redirect[0] . '">' . fw_ssd_get_option('show-code-text') . '</a>';
}
我的目标是检查点击该按钮的用户是否已登录:
if ( function_exists('um_profile_id') && !um_profile_id() && get_field('registered_members_only') ) {
wp_redirect(home_url('login/'));
exit;
}
但我无法弄清楚应该在哪里放置控制器代码。请F1。
由于您的按钮URL是外部的,因此从一开始就更改按钮URL似乎是一个更好的选择(如评论中所讨论的):
$enable_redirect = get_field( 'redirect_to_offer' ) ? get_field('redirect_to_offer') : array( '' );
if ( function_exists( 'um_profile_id' ) && ! um_profile_id() && get_field( 'registered_members_only' ) ) {
$button_url = home_url( 'login/' );
} else {
$button_url = get_field( 'url' );
}
echo sprintf( '<a href="%1$s" target="_blank" class="btn btn-color btn-deal show-coupon-code" data-target="#discount-modal" data-clipboard-text="%2$s" data-redirect="%3$s">%4$s</a>'
esc_url( $button_url ),
esc_attr( get_field( 'coupon_code' ) ),
esc_attr( $enable_redirect[0] ),
fw_ssd_get_option( 'show-code-text' )
);
登录状态定义最终按钮URL。
注意:我在其他地方用sprintf()
替换了占位符,以便于阅读。