【问题标题】:How to get user ID from the profile page and plug into id= in shortcode如何从个人资料页面获取用户 ID 并在短代码中插入 id=
【发布时间】:2020-10-02 22:54:28
【问题描述】:

我正在处理一个令人沮丧的项目,需要帮助。我需要从正在查看的个人资料页面获取 $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,所以当你点击作者的名字时,它会将它添加到这个 url 的末尾: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: 这是 Ultimate Member 用来从个人资料中获取帖子的方法。我想我可以用类似的方式设置它...

<?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 }
}

【问题讨论】:

  • 代码似乎没有在任何地方执行短代码。它使用过滤器设置一个选项卡(或尝试)返回它,但不清楚应该如何将短代码添加到页面内容(在选项卡下)。所以,大概有或将会有某种页面模板或功能在某处可能发生。
  • (我想我会先查看 UM 文档。)。
  • 我昨天开始考虑这个问题。我将尝试将代码放入 videos.php 模板中。这就是这个简码模板所在的位置。我会做更多的研究,但我想知道的最重要的事情是如果简码仅在个人资料页面上,如何自动化 ID。如果此人已登录并且这是他们的个人资料,则应该显示他们自己的视频。如果是其他人的个人资料,它应该从该个人资料中获取 id。
  • 对不起,我有点困惑:你说你遇到了“错误”。什么样的错误?如果函数实际上并没有放在目标内容中,无论如何,它们将如何获得 - 或 GET - 正确的 $user_id?你怎么知道它是否有效?
  • 如果我向您展示原始代码并更好地解释它,也许会有所帮助。我会将其添加为对此问题的编辑...

标签: php wordpress shortcode profile user-data


【解决方案1】:

我终于想通了!!!这就是最后的工作!

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

【讨论】:

  • 恭喜。毕竟它确实很简单。
【解决方案2】:

所以可能就这么简单:

//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' ) ; 只是如果这是这些钩子的要求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-19
    • 2017-06-12
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多