我是Wordpress的新手,我正在寻找一种方法来添加自定义字段并显示它们(没有插件)。
我在网上找到了a great example。作者通过将以下函数添加到fuctions.php
文件中来添加许多自定义字段。
function modify_contact_methods($profile_fields) {
// Add new fields
$profile_fields['linkedin'] = 'LinkedIn URL';
$profile_fields['telephone'] = 'Telephone';
return $profile_fields;
}
add_filter('user_contactmethods', 'modify_contact_methods');
我已成功将此类字段添加到用户注册表单的联系信息部分。我一直在尝试将自定义字段添加到其他部分,例如作者信息部分(Bio所在的部分),但没有成功。
我认为我要更改user_contactmethods
函数中的值add_filter(...)
,但我找不到任何内容。
我甚至不知道这是否是纠正这种方法的方法,但它到目前为止还有效 -
答案 0 :(得分:2)
由于您不熟悉wordpress,因此您不了解filter
和action
。如果您浏览filter list,则会发现user_contactmethods
here。
正如您在作者和用户过滤器中看到的那样,作者和用户只有4个过滤器。我们不能使用它们来实现您想要的输出。
但不知何故,我们可以通过在关于用户下添加另一个字段来完成此操作,例如作者信息。
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Author Information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="author"><?php _e("Author Information"); ?></label></th>
<td>
<textarea name="author" id="author" rows="5" cols="10" ><?php echo esc_attr( get_the_author_meta( 'author', $user->ID ) ); ?></textarea><br />
<span class="description"><?php _e("Please enter Author's Information."); ?></span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_user_meta( $user_id, 'author', $_POST['author'] );
}
因此,您可以根据需要添加任意数量的字段。