2

我在将自定义字段添加到 wordpress 用户配置文件并将输入的字段显示在网站前面时遇到问题。使用下面的教程,我以为我可以正常工作,但注意到实际上它只适用于一个用户,然后所有其他用户都不起作用,这让我感到困惑,因为我习惯了工作或不工作的事情,而这适用于我尝试过的第一个用户,但不适用于其他用户。

我看起来高低尝试不同的东西无济于事。

我这样做的目的是允许多个 wordpress 用户将他们的 Google+ 个人资料添加到具有 rel 属性的特定作者的帖子上的 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 上的这个链接似乎是一个类似的问题,但没有解决......

Wordpress 中的多个 update_user_meta

4

1 回答 1

3

是的,我最终找到了工作代码......

/* 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);
于 2012-07-28T14:16:42.027 回答