Home>Support>Get the Page title in a variable

Get the Page title in a variable

Hi everyone,

I’m customizing the header of my website in Vantage child-theme.
I see that “the_title();” call the function able to create the page title in the header.php, but the problem is when I try to save the name in a PHP variable just like:

$name = the_title();

… doesn’t work! Because that command is going to execute a piece of code I suppose.

So, how can I get the page name in a variable?
Thank you guys!

This is our free support forum. Replies can take several days. If you need fast email support, please purchase a SiteOrigin Premium license.

  1. 7 years, 8 months ago Max G. Khappa

    Hi please can I have a help?
    Cheers guys

  2. 7 years, 8 months ago Max G. Khappa

    Sorry guys I know my questions are always strange, but your help is gold as always.
    I just need to put the page title in other places and im working on the “header.php” file.
    If I call this function:
    the_title();
    its gonna create a new header, instead is there a way to get the the “page title” and save it in a variable?
    Thanks again guys

  3. 7 years, 8 months ago Alex S
    Hi, I Work Here

    Hi Maxx,

    You’re after get_the_title(). the_title() will automatically output the title while get_the_title will simply “get” the title (thus allowing you to do stuff with it).

    You could also use the_title( null, null, false ) but there’s no reason to do that that over get_the_title().

  4. 7 years, 8 months ago Max G. Khappa

    Hi Alex,

    that’s great! Thank you very much man, now it’s working and I see this is a WordPress function. In combination with some other PHP code ($full_path = “http://”.$_SERVER[‘HTTP_HOST’].$_SERVER[‘REQUEST_URI’];) I got custom header for every user profile.

    In this case I can also extend the capabilities of the “header.php” a lot, I will publish the code here in the forum if this can be useful for somebody else :)

    best!

  5. 7 years, 8 months ago Alex S
    Hi, I Work Here

    Hi Max,

    You need to escape those as what you’re currently doing allows for XSS. Please refer to esc_url for more information.

  6. 7 years, 8 months ago Max G. Khappa

    Hi Alex,

    sorry I just read your reply right now.
    Thanks a lot for your advice man, but is that necessary? Did you find something wrong in my additional code?

  7. 7 years, 8 months ago Alex S
    Hi, I Work Here

    Hi Max,

    Yes. This:

    $full_path = “http://”.$_SERVER[‘HTTP_HOST’].$_SERVER[‘REQUEST_URI’];

    Is vulnerable to XSS and it’s 100% necessary to escape those strings.

  8. 7 years, 7 months ago Max G. Khappa

    Sorry again for my late answer… im planning new medialist for my newsletter
    now imworking again to khappa.com
    wooooow, your’re right Alex and thanks for the advice but how can i get same thing in other way?

Replies on this thread are closed. Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More