最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

users - How to add wordpress username after url?

programmeradmin0浏览0评论

maybe u can help to solve this problem. for example i have this link

/?user=

and i want to add username after login, like /?user=admin

I want to make it on button.

<a href="/?user=username">
    <button>Click me</button>
</a>

I want to put this on post / page.

maybe u can help to solve this problem. for example i have this link

https://direktoriku/shopping/?user=

and i want to add username after login, like https://direktoriku/shopping/?user=admin

I want to make it on button.

<a href="https://direktoriku/shopping/?user=username">
    <button>Click me</button>
</a>

I want to put this on post / page.

Share Improve this question edited Apr 17, 2018 at 6:53 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Apr 17, 2018 at 3:20 Angga PradiptaAngga Pradipta 31 silver badge2 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You can do

<?php
      if( get_current_user_id() ): // check if user is loggedin
         $current_user = wp_get_current_user();  //get user       
?>
<a href="https://direktoriku/shopping/?user=<?php echo $current_user->user_login;?>
   <button>Click me</button>
</a>
<?php endif;?>

This is a small plugin I made for you. Put this code in a php file and upload it as a plugin, and activate it. Then put the shortcode [my-awesome-button] in a post or page, and there it will appear the button. If the visitor is not logged-in, nothing will appear.

<?php

/*
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/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;
}

By the way, we can easily modify this to only show the username, not the whole button. This way you can put the username anywhere you want in the post or page. Let me know if you need help with that.

发布评论

评论列表(0)

  1. 暂无评论