在wordpress中添加自定义文件到作者信息
问题描述:
我是新来的Wordpress,我正在寻找一种方法来添加自定义字段并显示它们(不带插件)。 我在网上找到a great example。作者通过将以下函数添加到fuctions.php
文件中添加了一些自定义字段。在wordpress中添加自定义文件到作者信息
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');
我已经能够TU等领域成功添加到我的用户登记表联系信息部分。我一直在尝试将自定义字段添加到其他部分,例如作者信息部分(其中Bio是),但没有成功。 我认为我必须在add_filter(...)
函数中更改值user_contactmethods
,但我一直未能找到任何东西。
我甚至不知道这是不是这样做的correect方式,但它的工作这么远
答
如你是新来的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']);
}
所以用这种方法你可以添加任意数量的字段。
哇,这绝对解决了我的问题。现在是时候获得一些有关文件员和行动的知识吧! – INElutTabile 2014-10-04 11:38:32
通过练习,您将自动获得知识。 快乐编码! – 2014-10-04 11:41:40