php - 如何使用 add_action 向所有 Woocommerce 属性术语添加自定义字段
问题描述
在 Woocommerce 产品属性页面上,我对一个 Woocommerce 属性(称为myattribute)进行了以下操作。以下将自定义字段添加到myattribute条款的添加/编辑页面:
add_action( 'myattribute_add_form_fields', array( $this, 'add_new_field' ), 10, 2 );
add_action( 'created_myattribute', array( $this, 'save_new_field' ), 10, 2 );
add_action( 'myattribute_edit_form_fields', array( $this, 'update_new_field' ), 10, 2 );
add_action( 'edited_myattribute', array( $this, 'updated_new_field' ), 10, 2 );
有没有办法对所有属性做同样的事情,而不需要在 add_action 中命名它们?
原因是我有许多属性,并希望将自定义字段添加到与这些属性相关的所有术语中。
提前致谢。
解决方案
这可能不是最好的解决方案,但它对我有用:
if (isset($_GET['taxonomy']) && !empty($_GET['taxonomy'])) {
$taxonomy_name = $_GET['taxonomy'];
if (strpos($taxonomy_name, 'pa_') !== false) {
add_action( $taxonomy_name.'_add_form_fields', array ( $this, 'add_category_image' ), 10, 2 );
add_action( 'created_'.$taxonomy_name, array ( $this, 'save_category_image' ), 10, 2 );
add_action( $taxonomy_name.'_edit_form_fields', array ( $this, 'update_category_image' ), 10, 2 );
add_action( 'edited_'.$taxonomy_name, array ( $this, 'updated_category_image' ), 10, 2 );
}
}
我一直在寻找相同的解决方案,到目前为止,这个快速代码帮助我在任何产品属性编辑/添加视图上实现了自定义字段。
推荐阅读
- html - Rails & FontAwesome - 跨度内的链接不起作用
- python - keras 神经网络中的多种输入类型
- sql - 查询返回我为它定义的更多结果
- c# - 如何将用户控件对象传递给另一个用户控件的内容?
- amazon-cloudformation - 设置 IAM 角色描述
- javascript - 动态创建的表中的粗体文本 - javascript
- null - Null Channel 最大尺寸 - Spring 集成流程
- java - 如何将 ID 属性添加到蜡染中的 g 元素?
- android - ImageButton 之间的两条水平线
- javascript - 设置默认输入值 symfony 3