I have this shortcode

[broo_user_badges username=""]

and I have to put username between " " But, username is in URL: /author/peter. So, when page domain.com/author/peter was loaded, on that page this shortcode should be generated:

[broo_user_badges username="peter"]

I found this

add_shortcode('name', 'get_name');
function get_name() {
   return $_GET['name'];

here How to get URL param to shortcode? but I do not understand how to use that on my case (/author/peter url structure).

Edit: plugin https://wordpress.org/plugins/badgearoo/

  • Please edit your question mentioning the shortcode plugin you are using.
    – Max Yudin
    Oct 16, 2016 at 12:23

2 Answers 2


This should work:

function get_name() {
    $url = "http://domain.com/author/peter";

    if (preg_match("/author/", $url )) {
        $lastSlash = strrpos( $url, "/");
        return substr( $url, $lastSlash, strlen($url));

This will return everything after the last slash.


Two ways to get author name from url

  1. Using get_query_var()
// http://example.com/author/peter
$author_name = get_query_var('author_name'); // peter
  1. Using get_queried_object()
// http://example.com/author/peter
$author_info = get_queried_object();
$author_name = $author_info->user_login; // peter

// print_r($author_info); // to see more information about Author

For more information read Custom Author Information

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.