我遇到的问题是在wordpress用户个人资料中添加自定义字段,并在网站的正面显示输入的字段。使用下面的教程,我认为我有它的工作,但是注意到它实际上它只适用于一个用户,然后所有其他用户都不起作用,这让我感到困惑,因为我习惯了工作或不工作的事情,这适用于我尝试过的第一个用户,但没有其他用户。
我看起来高低不同尝试不同的事情无济于事。
我的目的是允许多个wordpress用户将其Google+个人资料添加到具有rel属性的特定作者的帖子BY上的wordpress头部。
这是我用过的教程
http://bavotasan.com/2009/adding-extra-fields-to-the-wordpress-user-profile/
这是我用于函数的代码
1 - 这会将字段添加到用户个人资料
/* Add Additional Fields to Author Profile */
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) { ?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="u-profs">Profile Info</label></th>
<td>
<input type="text" name="u-profs" id="u-profs" value="<?php echo esc_attr( get_the_author_meta( 'u-profs', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Enter Your Profile Info.</span>
</td>
<td>
<input type="text" name="u-profs2" id="u-profs2" value="<?php echo esc_attr( get_the_author_meta( 'u-profs2', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Enter Your Google+ Url.</span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
foreach($user_ids as $user_id){
update_user_meta( $user_id, 'u-profs2', $_POST['u-profs2'] );
}
function my_save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
/* Copy and paste this line for additional fields. Make sure to change 'u-profs' to the field ID. */
update_user_meta( $user_id, 'u-profs', $_POST['u-profs'] );
update_user_meta( $user_id, 'u-profs2', $_POST['u-profs2'] );
}
2 - 获取字段内容并将其添加到wordpress的wp_head
/* Google Rel Author */
function google_rel_author_in_document_head() {
echo '<link rel="author" href="' . get_the_author_meta('u-profs2') . '"/>';
}
add_action('wp_head', 'google_rel_author_in_document_head',99);
StackOverflow上的这个链接似乎是一个类似的问题,但尚未解决......
答案 0 :(得分:2)
对,我最终找到了工作代码......
/* Add Additional Fields to Author Profile */
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) { ?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="u-profs">Profile Info</label></th>
<td>
<input type="text" name="u-profs2" id="u-profs2" value="<?php echo esc_attr( get_the_author_meta( 'u-profs2', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Enter Your Google+ Url.</span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
/* Copy and paste this line for additional fields. Make sure to change 'u-profs' to the field ID. */
update_user_meta( $user_id, 'u-profs2', $_POST['u-profs2'] );
}
/* Google Rel Author */
function google_rel_author_in_document_head() {
global $post;
$author_id=$post->post_author;
?>
<link rel="author" href="<?echo get_user_meta($author_id, 'u-profs2', true);?>"/>
<?
}
add_action('wp_head', 'google_rel_author_in_document_head',99);