如何从个人资料页面获取用户 ID 并以短代码插入 id=



我正在做一个令人沮丧的项目,需要帮助。我需要从正在查看的个人资料页面获取 $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 的简码。

更新:

这是插件中的视频.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;    
}

它的作用是从帖子的作者那里提取好名字,所以当你点击作者的名字时,它会把它添加到这个网址的末尾:https://hairbowtutorials.com/user-videos/NICENAME。 但是,如果您将 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 }
}

所以它可能像这样简单:

//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 代码之前,我的下一次尝试是在add_action语句中的"标签"和"处理程序"中添加_default- 所以add_action( 'um_profile_content_videos_default', 'um_profile_content_videos_default' ) ;以防万一这是这些钩子的要求。

我终于想通了!!这是最终奏效的方法!!

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

最新更新