如何在WordPress URL后添加登录用户名?

时间:2020-06-05 作者:Andrea Hugyak

我在这篇文章中找到了一个很好的解决方案:How to add wordpress username after url?

/*
Plugin Name: My Awesome Button
Description: The shortcode [my-awesome-button] will be replaced with the HTML code for a button for logged-in users and for guests it will be replaced with an empty string (so it will be removed). This will work for posts and pages.
Author: Nikolay Nikolov
Version: 1.0.0
*/

add_filter( \'the_content\', \'my_awesome_button_function\', 99999999999 );

function my_awesome_button_function( $content ) {
    if ( strpos( $content, \'[my-awesome-button]\' ) !== false ) {
        if ( is_user_logged_in() ) {
            $current_user = wp_get_current_user();
            $button_html = \'<a href="https://direktoriku.com/shopping/?user=\' . esc_attr( $current_user->user_login ) . \'"><button>Click me</button></a>\';
            $content = str_replace( \'[my-awesome-button]\', $button_html, $content );
        } else {
            $content = str_replace( \'[my-awesome-button]\', \'\', $content );
        }
    }
    return $content;
}
我唯一的问题是,该按钮仅在主页上处于活动状态,而在网站的所有其他页面(例如mydomain.com/documents)上处于非活动状态。

请你帮我把它修好。

非常感谢!

1 个回复
SO网友:Baikare Sandeep

您可以在此插件文件中尝试此短代码,也可以将此方法粘贴到活动主题的底部functions.php. 如果您是WordPress新手,需要帮助,可以访问here 有关在函数中添加代码的步骤。

add_shortcode( \'my-awesome-button\', \'wp_my_awesome_bytton\' );
function wp_my_awesome_bytton( $atts ) {
    $button_html = \'\';
    if ( is_user_logged_in() ) {
            $current_user = wp_get_current_user();
            $button_html = \'<a href="https://direktoriku.com/shopping/?user=\' . esc_attr( $current_user->user_login ) . \'"><button>Click me</button></a>\';

    }
    return $button_html; 
}
//在页面/帖子内容中的任何位置使用此短代码,如下所示:[my-awesome-button] 它会将您的短代码替换为html代码。有关shortcode的详细信息visit here.

相关推荐

How to change a username?

如何更改用户名?我的用户名以作者身份出现在搜索引擎中,但它是一个基于产品和服务的网站(仅使用页面),供客户使用。如果我无法删除作者页面,我需要更改用户名。如果打开,您似乎可以更改用户名wordpress.com 所以这意味着它可能是可能的,但这是一个个人WP安装。