You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

含woocommerce_before_customer_login_form的短码无法调用覆写模板

问题修复方案

问题根源

  1. 注册短码的模板路径写法错误,导致WooCommerce未优先加载主题中覆写的form-login.php;
  2. 登录短码使用的woocommerce_login_form()函数是直接生成表单HTML,完全不依赖模板文件,所以你的自定义修改不会生效。

具体修复步骤

1. 修复注册短码的模板路径

将注册短码里的模板调用代码,去掉路径开头的斜杠:

// 原代码
$html = wc_get_template_html( '/myaccount/form-login.php' );

// 修改后
$html = wc_get_template_html( 'myaccount/form-login.php' );

WooCommerce模板查找逻辑要求路径为相对路径(相对于woocommerce/templates目录),开头的斜杠会干扰主题模板的优先级判断。

2. 重写登录短码,使其调用自定义模板

替换原登录短码函数,改为从你覆写的form-login.php中提取登录表单部分,和注册短码逻辑保持一致:

function bbloomer_separate_login_form() {
   if ( is_user_logged_in() ) return '<p>You are already logged in</p>'; 
   ob_start();
   do_action( 'woocommerce_before_customer_login_form' );
   // 调用主题覆写的模板文件
   $html = wc_get_template_html( 'myaccount/form-login.php' );
   $dom = new DOMDocument();
   $dom->encoding = 'utf-8';
   // 处理HTML解析时的编码和格式警告
   libxml_use_internal_errors( true );
   $dom->loadHTML( mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' ) );
   libxml_clear_errors();
   $xpath = new DOMXPath( $dom );
   // 定位登录表单节点
   $form = $xpath->query( '//form[contains(@class,"login")]' );
   $form = $form->item( 0 );
   echo $dom->saveXML( $form );
   return ob_get_clean();
}
  • 加入libxml_use_internal_errors(true)是为了避免自定义模板HTML格式不规范时触发DOMDocument警告;
  • 用mb_convert_encoding替代原代码的utf8_decode,能更好处理中文及特殊字符,避免乱码。

内容的提问来源于stack exchange,提问作者EJ Navarro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 14:33:32