如何从个人资料页面获取用户ID,并在短码中插入id=。

问题描述 投票:0回答:1

我正在做一个令人沮丧的项目,需要帮助。我需要从正在浏览的个人资料页面中获取$user_id,并将该值插入一个短码的id = "_" $atts字段中。我正在使用终极会员作为我的会员插件。我需要将$user_id插入的短码如下......

echo do_shortcode( '[aiovg_user_videos id=" '.$user_id.' "]' ); }

该短码将进入一个名为 "视频 "的个人资料标签,这将有用户的视频,他们上传和导入。到目前为止,我想出的办法,虽然没有成功,但如下所示......

add_filter('um_profile_tabs', 'videos_tab', 1000 );
function videos_tab( $tabs ) {
    $tabs['videos'] = array(
        'name' => 'Videos',
        'icon' => 'um-icon-ios-videocam',
        'custom' => true
    );  
    return $tabs;
}

/* Tell the tab what to display */

// If is current user's profile (profile.php)
if ( defined('IS_PROFILE_PAGE') && IS_PROFILE_PAGE ) {
    $user_id = get_current_user_id();
// If is another user's profile page
} elseif (! empty($_GET['user_id']) && is_numeric($_GET['user_id']) ) {
    $user_id = $_GET['user_id'];

echo do_shortcode( "[aiovg_user_videos id=" . $user_id . "]" ); }

我不是一个编码者,但我正在学习,所以我真的不知道我还需要什么来实现这个功能。我已经尝试了很多很多的东西,给我带来了一个又一个的错误。简单的说,我想让短码从正在浏览的用户资料中抓取用户ID,这样我就可以在显示短码时自动填写ID。

更新。

这是插件中videos.php文件的原始代码...。

     * Run the shortcode [aiovg_user_videos].
     *
     * @since 1.0.0
     * @param array $atts An associative array of attributes.
     */
    public function run_shortcode_user_videos( $atts ) {    
        $user_slug = get_query_var( 'aiovg_user' );
        $content   = '';        

        if ( empty( $user_slug ) ) {
            if ( ! empty( $atts['id'] ) ) {
                $user_slug = get_the_author_meta( 'user_nicename', (int) $atts['id'] ); 
            } elseif ( is_user_logged_in() ) {
                $user_slug = get_the_author_meta( 'user_nicename', get_current_user_id() );             
            }
        }

        if ( ! empty( $user_slug ) ) {      
            $attributes = shortcode_atts( $this->get_defaults(), $atts );
            $attributes['user_slug'] = $user_slug;

            $content = $this->get_content( $attributes );       
        }

        if ( empty( $content ) ) {
            $content = aiovg_get_message( 'videos_empty' );
        }

        return $content;    
    }

它的作用是,从帖子的作者中提取nicename,所以当你点击作者的名字时,它会把它添加到这个网址的末尾。https:/hairbowtutorials.comuser-videosNICENAME。. 但如果你把id添加到短码中,你可以把它放在任何页面上,它将显示该人的视频。我想把这个短码添加到个人资料页面,并根据不同的个人资料自动填写ID。我不知道这是否可行,或者我是否需要制作一个全新的短码。但这是开发者给我的... ...

"你只需要使用下面的模板函数,并动态地建立短码。

<?php
$member_user_id = 1;
echo do_shortcode( "[aiovg_user_videos id=" . $member_user_id . "]" );
?>

你只需要找到会员资料的用户id,并将其存储在$member_user_id变量中即可。"

更新2:这里是终极会员用来从这个人的资料中获取帖子的方法。我想我也可以用类似的方式设置...

<?php if ( ! defined( 'ABSPATH' ) ) exit;

if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
    //Only for AJAX loading posts
    if ( ! empty( $posts ) ) {
        foreach ( $posts as $post ) {
            UM()->get_template( 'profile/posts-single.php', '', array( 'post' => $post ), true );
        }
    }
} else {
    if ( ! empty( $posts ) ) { ?>
        <div class="um-ajax-items">

            <?php foreach ( $posts as $post ) {
                UM()->get_template( 'profile/posts-single.php', '', array( 'post' => $post ), true );
            }

            if ( $count_posts > 10 ) { ?>
                <div class="um-load-items">
                    <a href="javascript:void(0);" class="um-ajax-paginate um-button" data-hook="um_load_posts"
                       data-author="<?php echo esc_attr( um_get_requested_user() ); ?>" data-page="1"
                       data-pages="<?php echo esc_attr( ceil( $count_posts / 10 ) ); ?>">
                        <?php _e( 'load more posts', 'ultimate-member' ); ?>
                    </a>
                </div>
            <?php } ?>

        </div>

    <?php } else { ?>

        <div class="um-profile-note">
            <span>
                <?php if ( um_profile_id() == get_current_user_id() ) {
                    _e( 'You have not created any posts.', 'ultimate-member' );
                } else {
                    _e( 'This user has not created any posts.', 'ultimate-member' );
                } ?>
            </span>
        </div>

    <?php }
}
php wordpress shortcode profile user-data
1个回答
0
投票

所以可能就像这样简单。

//very close to your initial code
add_filter( 'um_profile_tabs', 'um_videos_tab', 1000 );

function um_videos_tab( $tabs ) {
    $tabs['videos'] = array(
        'name' => 'Videos',
        'icon' => 'um-icon-ios-videocam',
        'custom' => true
    ) ;  

    return $tabs ;

}

//profile tab content
//based on how it appears UM has set up its action hooks
add_action( 'um_profile_content_videos', 'um_profile_content_videos' ) ;

function um_profile_content_videos() {

   //got to watch the apostrophes vs quotes so things don't get mixed up
   echo do_shortcode( ' [aiovg_user_videos id="' . um_profile_id() . '"] ' ) ; 

}

我的不确定部分是基于这样一个事实,即我没有UM或其他什么东西来制作aiovg_user_videos短码,所以无法测试或调试。鉴于我有点盲目,在详细研究UM代码之前,我的下一个尝试是添加 _default 中的 "标签 "和 "处理人"。add_action 声明--所以 add_action( 'um_profile_content_videos_default', 'um_profile_content_videos_default' ) ; 以防万一,这是对这些钩子的要求。


0
投票

我终于想出了办法!!! 这就是最终成功的方法!!!

/**
 * Add a new Profile tab
 * @param array $tabs
 * @return array
 */
function um_videos_add_tab( $tabs ) {

    $tabs[ 'videos' ] = array(
        'name'   => 'Videos',
        'icon'   => 'um-icon-ios-videocam',
        'custom' => true
    );

    UM()->options()->options[ 'profile_tab_' . 'videos' ] = true;

    return $tabs;
}
add_filter( 'um_profile_tabs', 'um_videos_add_tab', 1000 );

/**
 * Render tab content
 * @param array $args
 */
function um_profile_content_videos_default( $args ) {
    /* START. You can paste your content here, it's just an example. */

    $action = 'videos';

$member_user_id = um_get_requested_user();
echo do_shortcode( "[aiovg_user_videos id=" . $member_user_id . "]" );


    /* END. You can paste your content here, it's just an example. */
}
add_action( 'um_profile_content_videos_default', 'um_profile_content_videos_default' );
© www.soinside.com 2019 - 2024. All rights reserved.