WordPress弹窗注册表单链接target属性失效,无法打开新标签页
解决方案
问题根源在于WordPress的wp_kses函数会过滤掉未被明确允许的HTML属性。你当前的代码里,只给<a>标签开放了href属性,target和onclick都被默认过滤了,所以前端渲染出的链接根本没有这些属性,自然无法在新标签页打开。
正确修改方式
将wp_kses的标签属性规则里,给<a>标签添加target属性的允许权限:
<div class="form-group"> <label for="register-terms-and-conditions"> <input type="checkbox" name="terms_and_conditions" value="on" id="register-terms-and-conditions" oninvalid="setCustomValidity('Please accept the terms and conditions before proceeding')" oninput="setCustomValidity('')" required> <?php echo sprintf(wp_kses(__('I accept the <a href="%s" target="_blank">terms and conditions*</a>'), array('a' => array('href' => array(), 'target' => array())) ), esc_url($page_url)); ?> </label> </div>
为什么之前的尝试无效
- 更换
target属性值无效:因为wp_kses直接把整个target属性过滤掉了,前端看不到这个属性,任何值都不会生效。 - 添加
onclick事件无效:同样,你没在wp_kses的规则里允许onclick属性,这个事件代码会被过滤,不会出现在最终的HTML里。
如果一定要用onclick实现,需要把onclick也加入<a>标签的允许属性列表,但优先使用target="_blank"更符合HTML标准,也更简洁。
内容的提问来源于stack exchange,提问作者Laphattan
相关产品推荐
相关产品推荐

