【发布时间】:2017-09-21 12:47:49
【问题描述】:
到目前为止,我已将代码缩小到此过滤器(在另一个 SO 答案中找到):
//Checkout page editor bootstrap
add_filter('woocommerce_checkout_fields', 'addBootstrapGlyphs' );
function addBootstrapGlyphs($fields) {
foreach ($fields as &$fieldset) {
foreach ($fieldset as &$field) {
// if you want to add the form-group class around the label and the input
$field['class'][] = 'input-group';
// add form-control to the actual input
$field['input_class'][] = 'form-control';
}
}
return $fields;
}
这设置了我的表单和输入元素。现在要添加字形图标,我尝试从 $fields 读取,但我似乎无法很好地处理可以让我在 input 之前添加 span 元素的东西. Glyphicon 需要这个:
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
jQuery 让我这样做:
<script type='text/javascript'>
jQuery(document).ready(function(){
jQuery('#billing_last_name_field').prepend('<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>');
});
</script>
但是必须有一种更好、更高效的方式来构建已经存在的页面。我想在 php 中执行此操作,因此结帐页面加载仍然很快。请帮忙。我来自 Java 世界。
tl;dr:如何在 https://github.com/woocommerce/woocommerce/blob/master/includes/wc-template-functions.php#L1920 的 woocommerce 结帐输入框中添加字形图标
第 2 部分
使用类似这样的方法在上述问题上取得进展:
// define the woocommerce_form_field_<type> callback
function filter_woocommerce_form_field_type( $field, $key, $args, $value ) {
$field = str_replace('<input','<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span><input',$field);
return $field;
};
// add the filter
add_filter( "woocommerce_form_field_email", 'filter_woocommerce_form_field_type', 10, 4 );
我知道,这不是正确的字形。但是,下一个障碍是编辑 html。就像下面评论中提到的 Mithc 一样,我现在将尝试使用某种类型的 DOM 处理代码更优雅地做到这一点。
所以我的后续问题是,如何使用 php 以正确的方式添加 DOM 元素?这一次,我正在寻找类似的东西,
- 将字符串转换为 DOM 进行处理
- 从
<p>或<input>读取一些属性 - 确定我应该使用的字形类型
- 添加我的跨度
- 将 DOM 转换回字符串以供返回
有什么优雅的方法可以做到这一点,或者 str_replace() 是否足够好?
【问题讨论】:
标签: wordpress twitter-bootstrap woocommerce wordpress-theming