How to Get a part of URL and put in shortcode?

admin2025-06-05  2

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/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 /

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/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/plugins/badgearoo/

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Oct 16, 2016 at 11:37 StefanStefan 211 silver badge4 bronze badges 1
  • Please edit your question mentioning the shortcode plugin you are using. – Max Yudin Commented Oct 16, 2016 at 12:23
Add a comment  | 

2 Answers 2

Reset to default 1

This should work:

function get_name() {
    $url = "http://domain/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/author/peter
$author_name = get_query_var('author_name'); // peter
  1. Using get_queried_object()
// http://example/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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749113463a316476.html

最新回复(0)