wordpress - Wordpress 添加额外用户配置文件字段的问题

标签 wordpress google-plus

我在将自定义字段添加到 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 上的这个链接似乎是一个类似的问题但是没有解决...

Multiple update_user_meta in Wordpress

最佳答案

对了,我终于找到了工作代码......

/* 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);

关于wordpress - Wordpress 添加额外用户配置文件字段的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11669817/

相关文章:

c# - Google+ 'share' 未检测到语言设置

php - 使用 AM/PM 将字符串转换为 mysql 日期时间

wordpress - 拒绝文件请求并使用 .htaccess 重定向到 .php 页面

PHP 和 HTML CSS 标签

java - fragment 内的谷歌加登录按钮显示空指针异常 onclick

javascript - Google Plus 使用 Javascript 发帖?

javascript - WordPress javascript 加载顺序

WordPress - 自定义评论 - 父/嵌套回复

ios - 在 iOS 中执行 Google+ 时出错

java - 如何在 Google Cloud Console 的同意屏幕上更改电子邮件帐户?