WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] PHP Variable for current URL (12 posts)

  1. jzacsh
    Member
    Posted 5 years ago #

    hello, I feel like this is uber-simple, but I'm not too familiar with PHP (I know/think I've seen it before in the code making up a theme I've used):
    I'm trying to figure out what the variable is for the current URL (for instance, if I could call that variable here, you'd see "wordpress.org", because that's the URL of this page)

    important: would I be able to use this right inside of a post? (I see there's only a WYSIWYG and an HTML editor for posting, no input for PHP, would I need to use the "custom fields" or something?)

    thanks in advance for the help!

  2. esmi
    Forum Moderator
    Posted 5 years ago #

    Could you explain what it is you're trying to do?

  3. jzacsh
    Member
    Posted 4 years ago #

    hey sorry, must've had email notifications turned off for this forum.

    um, i'm trying to figure out how I can dynamically display the URL of my website from inside of a post. specifically, I don't want to manually type into my post <a href="www.site.com">...

    because I may change my domain, at which point the link ^ above would no longer be correct. I know that one solution would be to have a relative link like <a href="/">... which would be like my pointing to the root of the site, but that won't solve my problem this time.

    thanks again for any help

  4. jzacsh
    Member
    Posted 4 years ago #

    Hi, I'm trying to figure out how to reference "base_url" or my 'wordpress site' when i link to things. For instance, lets say I want to write a link in a blog post that's:

    http://mysite.com/tags/tech

    if i change my domain to newsite.com, that link^ will break

    a solution (i hoped) would be:

    <?php $BASE_URL$ php?>/tags/tech

    [where $BASE_URL$ is simply a variable refencing the wordpress configuration setting that holds the invormation to "blog_url" and "site_url" in the settings pane]

    thanks in advance!! :)

  5. esmi
    Forum Moderator
    Posted 4 years ago #

    You can't use PHP within the post/page content area unless you first install a plugin like PHP Execution. And if you're going to do that, why not use <?php bloginfo('url'); ?>/tags/tech?

  6. Use these, from http://codex.wordpress.org/Template_Tags/bloginfo

    <?php bloginfo('template_directory'); ?> returns the active template directory
    
    <?php bloginfo('url'); ?>  returns URL of web root
    
    <?php bloginfo('title'); ?> returns blog name

    And use with a plugin that enables PHP in posts and pages, like Exec-PHP

  7. jzacsh
    Member
    Posted 4 years ago #

    aah, a bloginfo function! thank you, this is exactly what i meant, I'll test this out tomorrow,

    Thank you both so much!

  8. WatchCount.com
    Member
    Posted 4 years ago #

    Just wondering if anyone knows of an easy way to take this 1 step further: the full URL of the page that is currently being displayed.

    Thanks!

    -WCC

  9. acafourek
    Member
    Posted 4 years ago #

    To get the current URL, you can use a pretty simple PHP snippet (explained here: http://www.webcheatsheet.com/PHP/get_current_page_url.php )

  10. WatchCount.com
    Member
    Posted 4 years ago #

    Thanks for that acafourek.

    Just wondering, is there a built-in WP function that does it for WP-specific URLs? I built and used something similar to that WebCheatSheet one, but sometimes it returns WP URLs that end in .ico, .gif, etc. and not the main page URL.

    -WCC

  11. gauthamns
    Member
    Posted 4 years ago #

    You can use get_permalink() function

  12. megakozak
    Member
    Posted 4 years ago #

    It doesn't solve major problem here.
    If you are using 4 exmpl. google translator and you wanna keep your links accurate even on translated pages you need to generate links from your template... Pure php method will get google link after translation and you wont be able to use this function again.

    Anyone know's a way to determine current page permalink (whenever it's a category, page, post, or search page teplate)?

Topic Closed

This topic has been closed to new replies.

About this Topic