WordPress-动态添加用户ID到URL的末尾

发布于 2025-01-28 09:18:19 字数 525 浏览 5 评论 0原文

我正在寻找一种将当前登录的用户名添加到链接末尾的方法。

example: www.mywebsite.com/custom-post-type/author-post-title

“作者 - 命名”将被用户名中的当前登录所取代

此链接将导致一个私人帖子,该帖子将自动使用重力表格创建。帖子标题与已登录的人的用户名相同。我希望此链接事先动态地到位。每个作者仅限于一个帖子,并且已经必须登录用户才能查看NAV菜单中的链接。

我认为有一种方法可以在其中编写作者名称是占位符,并且被当前登录的用户ID代替,但我无法弄清楚。类似...但是,这是我新来的PHP。

<?php

function replace_text($text) {
    $text = str_replace('author-post-title', '$user_id', $text);
    $user_id = get_current_user_id();
    return $text;
}

?>

I'm looking for a way to have the current logged in username added to the end of a link.

example: www.mywebsite.com/custom-post-type/author-post-title

"author-post-title" would be replaced with the current logged in username

This link will lead to a private post that is automatically created using Gravity Forms. The post title is the same as the logged-in person's username. I would like this link to be in place dynamically beforehand. Each author is limited to only one post and users already have to be logged in to see the link in nav menu.

I'm thinking there is a way to code this where the author name is a placeholder and is replaced by the currently logged in user ID, but I have not been able to figure it out. Something like... but this is way off as I am new to php.

<?php

function replace_text($text) {
    $text = str_replace('author-post-title', '$user_id', $text);
    $user_id = get_current_user_id();
    return $text;
}

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春夜浅 2025-02-04 09:18:20

我建议您制作为所有用户具有相同URL的通用页面,并为特定用户修改其内容。将此链接附加到导航菜单会更容易。

但是,如果您确实需要创建此类链接,则可以使用函数创建它:

function get_user_specific_url() {
    if ( is_user_logged_in() ) {
        return sprintf(
            '%s/custom-post-type/%s',
            get_site_url(),
            wp_get_current_user()->user_nicename
        );
    }

    // Redirect home in case if not logged in
    return get_site_url();
}

那么您可能需要为此链接结构创建自定义重写规则,以显示用户的特定页面:

add_action( 'init', function() {
       add_rewrite_tag("%user_nicename%", '([^/]*)');
       add_rewrite_rule(
           '^custom-post-type/([^/]*)/?',
           'index.php?page_id=123&user_nicename=$matches[1]',
           'top'
       );
} );

I would recommend you to make generic page which has same URL for all users and modify it's content for specific user. It will be easier to attach this link to nav menu.

But, if you really need to create this kind of link you may create it using function:

function get_user_specific_url() {
    if ( is_user_logged_in() ) {
        return sprintf(
            '%s/custom-post-type/%s',
            get_site_url(),
            wp_get_current_user()->user_nicename
        );
    }

    // Redirect home in case if not logged in
    return get_site_url();
}

Then you may want to create custom rewrite rule for this link structure to display specific page for user:

add_action( 'init', function() {
       add_rewrite_tag("%user_nicename%", '([^/]*)');
       add_rewrite_rule(
           '^custom-post-type/([^/]*)/?',
           'index.php?page_id=123&user_nicename=$matches[1]',
           'top'
       );
} );
绾颜 2025-02-04 09:18:20

您可以尝试将以下短代码添加到您的页面:
[repleast_link_w_user link =“ 在这里添加您的链接,而无需用户名“ button =” 在此处添加按钮文本”]

然后将以下内容添加到您的functions.php:php:

add_shortcode( 'replace_link_w_user', 'rl_w_user' );
function rl_w_user($atts = array()) {
    $details = shortcode_atts( array(
        "link"=>'',
        "button_text"=>'Search'
    ), $atts );
    if ( is_user_logged_in() ) {
        $button='
        <form action="'.$details['link'].'/'.wp_get_current_user()->user_nicename.'">
            <input type="submit" value="'.$details['button_text'].'" />
        </form>';
        return $button;
    } else {
       return;
    }
}

You could try adding the following shortcode to your page:
[replace_link_w_user link="add your link here without username" button="Add button text here"]

Then add the following to your functions.php:

add_shortcode( 'replace_link_w_user', 'rl_w_user' );
function rl_w_user($atts = array()) {
    $details = shortcode_atts( array(
        "link"=>'',
        "button_text"=>'Search'
    ), $atts );
    if ( is_user_logged_in() ) {
        $button='
        <form action="'.$details['link'].'/'.wp_get_current_user()->user_nicename.'">
            <input type="submit" value="'.$details['button_text'].'" />
        </form>';
        return $button;
    } else {
       return;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文