• I’ve installed a plugin that allows me to use PHP without my posts and pages. What I would like to do now is to use some template tags (such as <?php bloginfo(‘url’); ?>) within my pages, so that I don’t have to type out full URLs all over the place and can have everything relative to my WordPress base URL.

    This is probably a very simple question but I was unable to find the answer by searching, possibly because I wasn’t sure what to search for.

    What I need to know is how I can use template tags within PHP pages. For example, I have a script and I would like to use the <?php bloginfo(‘url’); ?> tag without first “leaving” PHP. I tried just using bloginfo(‘url’) on its own at that didn’t work; neither did $bloginfo(‘url’), bloginfo(‘$url’) or {bloginfo(‘url’)}.

    Any ideas? Thanks in advance!

Viewing 15 replies - 1 through 15 (of 16 total)
  • You mean you are using PHP inside WP created pages (for which you would need a plugin)? Or outside WP?

    You’re talking about a plugin, so if you’ve installed it to allow PHP on pages and posts, it may have a special syntax for writing PHP like <phpcode>bloginfo('url');</phpcode>.

    If you mean that you have a PHP file separate from WP where you would like to use WP’s template tags, you need <?php require('path/to/wp-blog-header.php'); ?> somewhere at the top of the file (before any WP tags). Wp-blog-header.php is found in the WP root folder.

    The syntax for using template tags without “leaving” PHP is that without the beginning <?php and ending ?>, that is the bloginfo('url') version you tried.

    Thread Starter Bonnie

    (@bonnie)

    Hi and thanks for the reply. ??

    It’s a plugin that basically allows you to treat pages and posts like regular PHP pages, providing you use the correct <?php … ?> syntax. It doesn’t require any special syntax (such as the suggested <phpcode> tag). It’s the RunPHP plugin. I’m using it within WP pages (not external pages).

    Basically, what I need to know is how to write the variable bloginfo(‘url’) without enclosing it in its own <?php … ?> tags. Not just in my plugin-ified pages, but in templates, too. So, for example, if I wanted to include some PHP in my header template (which I do, also) I would use something like this:

    <?php echo “<img src=\”bloginfo(‘url’)/images/nav_{$nav_id}.png\” width=\”120\” height=\”30\” alt=\”$nav_title\” />”; ?>

    … where bloginfo(‘url’) is the variable for bringing up the URL to my WP installation, and $nav_id and $nav_title are my own pre-defined variables (which do work). Unfortunately, the script seems to break if I “leave” PHP to display the images, so I’d like to know how to echo the variable without its <?php … ?> tags. Simply using bloginfo(‘url’) doesn’t work; it just prints out “bloginfo(‘url’)” rather than the variable’s value.

    You use the tags as you would in a .php page i.e. <?php bloginfo('url'); ?>. Have you followed the usage instructions on the site?:
    In Options -> Writing, turn off the a€?WordPress should correct invalidly nested XHTML automaticallya€? option.
    Write a post like normal. Save or Publish the post, then go back and edit it. Check the a€?eval() contenta€? in the Discussion box and save the post again.

    <?php echo "<img src=\"" . bloginfo('url') . "/images/nav_$nav_id.png\" width=\"120\" height=\"30\" alt=\"{$nav_title}\" />"; ?>

    Or if you don’t want to escape all those double quotes:

    <?php echo '<img src="' . bloginfo('url') . '/images/nav_' . $nav_id . '.png" width="120" height="30" alt="' . $nav_title . '" />'; ?>

    is what I’d write.

    (I hope I got those right…)

    Thread Starter Bonnie

    (@bonnie)

    Yes, I’ve read the usage instructions and I can get the plugin working fine. However, I am trying to use a tag *within* PHP, so enclosing it in <?php … ?> tags won’t work. Similarly, just using it on its own (without the PHP tags) doesn’t work either.

    This applies to templates as well, not just pages and posts.

    Thread Starter Bonnie

    (@bonnie)

    Thanks for the suggestion, Minna.

    Unfortunately, both of those seem to print out the following:

    https://www.burbling.net/wordpress/<img src=”/images/nav_.png” width=”120″ height=”30″ alt=”” />

    It echoes the value of blog(‘url’) before starting the IMG tag. I’m not really sure why though! I tried moving things around but to no avail. *feels a bit stupid*

    Could you bin the bloginfo bit and just use
    <?php echo "<img src=/images/nav_{$nav_id}.png\" width=\"120\" height=\"30\" alt=\"$nav_title\" />"; ?> I think that has the same effect but I am not too good on relative linkiness.

    Thread Starter Bonnie

    (@bonnie)

    Hi Ian –

    I don’t think so, because I need to use this same header for all my pages and some of them are in different folders. ??

    Hmmmm.

    oh yeah, echo smacked the stuff in weird places (or actually, I must’ve been using it wrong all these years ?? ). Try print instead

    Thread Starter Bonnie

    (@bonnie)

    Same problem with ‘print’. Hmm. I think I may just have to re-shuffle my site structure a bit and try to get away with using relative links after all. Thanks for the help, though, Minna and Ian. It’s much appreciated!

    A bit of searching suggests that get_settings('siteurl') or get_settings('home') could work. I haven’t tried them though.

    Ugh, how annoying (and weird)…
    What about splitting it up:
    <?php
    echo "<img src=\"";
    echo bloginfo('url') ;
    echo "/images/nav_$nav_id.png\" width=\"120\" height=\"30\" alt=\"{$nav_title}\" />"; ?>

    ?? That works, I tried.

    Thread Starter Bonnie

    (@bonnie)

    Unfortunately, Ian, I can’t seem to get any tags to work in that way without putting them inside their own PHP tags. ?? It’s a shame because that’d be very convenient!

    Minna – You’re an absolute star. That works. ?? Thank you so much!

    Thank you Ian and Minna for all your help. ?? Now I can finally get on with modifying this script!

    It’s really weird that bloginfo(‘url’) is outputted before everything else… kinda annoying in fact.

    But thanks for the tip guys ??

    The bloginfo function is built to automatically echo its data, so when you call then function it echos and then the rest of what you were trying to echo echos.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Using template tags within PHP pages’ is closed to new replies.