Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i have a file i am including across site to index.php, single.php etc.

This file behaves really strage and acts like it not part of
wordpress Enviroment meaning:

i get errors on get_bloginfo('template_url') and it won't
get the values from my options page.

Any idea what this might happen?

Here is the include code i have in my index.php (somewhere mid page):

<?php include(get_bloginfo('template_url') .'/extras/floater.php'); ?>

(it includes the file so problem aint there)

Here is the actuall "floater.php" code... :

<?php
    function establishParms() {
    // get yes/no values and urls..
    $facebook = get_option('sg_show_facebookfloater');
    $twitter = get_option('sg_show_twitterfloater');
    $mail = get_option('sg_show_mailfloater');

        // facabook
        if($facebook == "Yes") {
            $facebook = '<li><a href="'. get_option('sg_url_facebookfloater') .'"><img src="'. get_bloginfo('template_url') .'/images/facebook.png" alt="'.__('Visit us on Facebook', 'sagive').'" /></a></li>';
        } 

        // twitter
        if($twitter == "Yes") {
            $twitter = '<li><a href="'. get_option('sg_url_twitterfloater') .'"><img src="'. get_bloginfo('template_url') .'/images/twitter.png" alt="'.__('Visit us on Twitter', 'sagive').'" /></a></li>';
        } 

        // mail
        if($mail == "Yes") {
            $mail = '<li><a href="'. get_option('sg_url_mailfloater') .'"><img src="'. get_bloginfo('template_url') .'/images/mail.png" alt="'.__('Contact us', 'sagive').'" /></a></li>';
        } 
    }
?>

<div class="floatingBar" >
    <ul class="gotop">
        <?php echo $facebook; ?>
        <?php echo $twitter; ?>
        <li><a href="#top"><?php echo '<img src="'. get_bloginfo('template_url') .'/images/mail.png" alt="" />'; ?></a></li>                                    
        <li><a href="#top"><?php echo '<img src="'. get_bloginfo('template_url') .'/images/up.png" alt="" />'; ?></a></li>
    </ul>
</div>  

.
Nothing Special there also so.. i am left with no answer to think of.

Also... had a nother weird problem with this site (which works great on localhost)
i have a function that did a simple thing inside function.php file:

function setPaths() {
    $templateUrl = get_bloginfo('template_url');
    $imagesUrl = get_bloginfo('template_url')."/images/";
}

But when trying to use $templateUrl in index.php file it gave me nothing..

Would love for your help :)
Cheers, Sagive

REVISION:

Here is an example of an error i got:

Fatal error: Call to undefined function get_bloginfo() in /home/workspac/public_html/sagive/wp-content/themes/sagive-spaceJunkie/extras/floater.php on line 29
share|improve this question
    
What is the error that you get? Can you copy that text into your question? –  tollmanz Feb 4 '12 at 22:37
    
Added to my question... i am starting to think its might be a bad wordpress installation cuz' this shouldnt happen :( –  Sagive SEO Feb 5 '12 at 0:16

2 Answers 2

up vote 1 down vote accepted

The error indicates that your file "floater.php" is being called outside of a WordPress generated page. Add this to the top of the file to be able to use WordPress functions.

EDIT: See Brian Fegter response on using the server path for your include.

if ( !function_exists( 'get_bloginfo' ) )
    require( '../../../wp-blog-header.php' ); // check path leads to root of your WordPress install

Regarding your setPaths function, you can either set those as global variables or use constants. Depending on what you are using these for, you can likely define these directly in your functions.php file instead of calling an additional function.

function setPaths() {
    // option one
    global $templateUrl, $imagesUrl;
    $templateUrl = get_bloginfo( 'template_url' );
    $imagesUrl = $templateUrl . "/images/"; // no need to call get_bloginfo again

    // option two preferred assuming these values are not changing.
    define( 'TEMPLATE_URL', get_bloginfo( 'template_url' ) );
    define( 'IMAGES_URL', TEMPLATE_URL . '/images/' );
}
/*
setPaths();
global $templateUrl, $imagesUrl;
echo $templateUrl;
echo $imagesUrl;
echo TEMPLATE_URL;
echo IMAGES_URL;
*/
share|improve this answer
    
You essentially are creating double the DB load per page load by loading WordPress into a template file. –  Brian Fegter Feb 5 '12 at 7:31
    
Based on the error message, the file is being included outside of WordPress. The conditional check ensures WordPress isn't being loaded twice (which would cause fatal errors not double load). –  aaronwaggs Feb 5 '12 at 17:28
    
ah, didn't see he was using a URL for the include. That would essentially load WP twice since they would be separate requests. –  aaronwaggs Feb 5 '12 at 17:39
    
Evantually this: wp-blog-header.php - did the trick... dont know why i needed that but after such a long search "I DONT CARE!" :) thanks a lot man –  Sagive SEO Feb 7 '12 at 13:08
    
Just beware that this is a huge performance issue. If your site gets any formidible traffic, this essentially could shut your site down. –  Brian Fegter Feb 7 '12 at 13:36

You should include using the server path rather than the URL. This will give you access to WordPress functionality.

include('extras/floater.php');

Update: The following is to further explain why it's a bad practice to include the file via URL. Here's what's happening:

  1. There is an innate inheritance when you include a file via server path. It has access to the functions, classes, variables that have been loaded prior to the include.
  2. When you include a file via URL, you get none of that inheritance because the file is loaded externally. When you do this, floater.php is loaded WITHOUT WordPress functionality such as get_option() and get_bloginfo(). Including URLs is as though you are doing a simple screen scrape.

If a relative server path isn't working, you can use a native WordPress constant to determine the path to floater.php.

include(WP_CONTENT_DIR.'/themes/yourtheme-name/extras/floater.php');
share|improve this answer
    
Why Should that work (doesent).. i dont get this weird problem.. an include into index.php in a template folder should work.. By the way.. changed (to check) a wordpress installation.. still not working online but works on localhost –  Sagive SEO Feb 6 '12 at 1:00
    
Ps.. the include works.. the file is being loaded.. its the WP commands inside that file that wont work. –  Sagive SEO Feb 6 '12 at 1:01
    
The include works OUTSIDE your loop, you need to include the server path so it's one continuous script. What you're doing now, is loading externally from the loop and basically loading WordPress two times per page load. With the server path include, you will load WordPress one time and get the desired effect. –  Brian Fegter Feb 6 '12 at 4:47
    
well... i guess i dont understand :) - my apologies... i tried using the include this way but i get an include error.. "cant find file" and so on.. –  Sagive SEO Feb 7 '12 at 0:27
    
See my further explanation above. :) –  Brian Fegter Feb 7 '12 at 2:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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