Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: How Can I Use a Include File With This Functions.php Code Snippet?

  1. #1
    Edwin is offline Hello World
    Join Date
    Nov 2009
    Posts
    47

    Default How Can I Use a Include File With This Functions.php Code Snippet?

    I was wondering how to have the Adsense code in this example being pulled via include function.

    Example regular code:

    PHP Code:
    function showads() {
        return 
    '<script type="text/javascript"><!--
    google_ad_client = "pub-xxxx0000xxxx0000";
    google_ad_slot = "111111111";
    google_ad_width = 468;
    google_ad_height = 60;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    '
    ;
    }

    add_shortcode('adsense''showads'); 
    Instead of the above, I would like to have something like this:




    PHP Code:
    function showads() {
        return '<?php include ('http://www.domain.com/adsense.php'); ?>
    ';
    }

    add_shortcode('adsense', 'showads');
    I'm not sure where to begin what the "return" part should be replaced with, if that even needs to be replaced, or what use as an alternative for the include file code.

  2. #2
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,997

    Default

    Is www.domain.com the same domain, or is that an external include?

    If it's internal, it would just be:
    PHP Code:
    function showads() {
        include (
    'FILEPATH/adsense.php');
    }

    add_shortcode('adsense''showads'); 
    (You'd need to know what FILEPATH is, but what you use there will depend on where adsense.php is. If it's in a plugin, you'd maybe build FILEPATH with WP_PLUGIN_DIR; if it's in your Theme, you'd build FILEPATH with e.g. get_template_directory().)

    However, if it's an external domain:
    PHP Code:
    function showads() {
        include (
    'http://www.domain.com/adsense.php');
    }

    add_shortcode('adsense''showads'); 
    (And you'd need to ensure that your PHP settings allow for such external URL includes.)
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  3. #3
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,801

    Default

    Quote Originally Posted by chipbennett View Post
    However, if it's an external domain:
    PHP Code:
    function showads() {
        include (
    'http://www.domain.com/adsense.php');
    }

    add_shortcode('adsense''showads'); 
    Nope. Includes don't work via http. You are best off using the WordPress http API for that - which WP Tavern member Jacob Santos wrote.

  4. #4
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,997

    Default

    Quote Originally Posted by Ryan View Post
    Nope. Includes don't work via http. You are best off using the WordPress http API for that - which WP Tavern member Jacob Santos wrote.
    Isn't that what the allow_url_include php.ini setting is for?

    Or am I completely missing something?
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  5. #5
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,801

    Default

    Quote Originally Posted by chipbennett View Post
    Isn't that what the allow_url_include php.ini setting is for?
    Probably. I hadn't heard of that before. Thanks for point it out :)

    http API would be the solution when you can't guarantee that setting is in place I guess then.

    I think readfile() is the standard why of doing it with raw PHP though as it doesn't require a setting change. That does apparently still have issues on some setups though and supposedly curl works even better. http API I believe checks if curl is in place and if not, defaults to readfile() (I think) and failing that I think it tries something else (I haven't read up on it in detail so am not sure of the details), so it should hopefully work on most server setups.

  6. #6
    Edwin is offline Hello World
    Join Date
    Nov 2009
    Posts
    47

    Default

    Quote Originally Posted by chipbennett View Post
    Is www.domain.com the same domain, or is that an external include?

    If it's internal, it would just be:
    PHP Code:
    function showads() {
        include (
    'FILEPATH/adsense.php');
    }

    add_shortcode('adsense''showads'); 
    (You'd need to know what FILEPATH is, but what you use there will depend on where adsense.php is. If it's in a plugin, you'd maybe build FILEPATH with WP_PLUGIN_DIR; if it's in your Theme, you'd build FILEPATH with e.g. get_template_directory().)

    However, if it's an external domain:
    PHP Code:
    function showads() {
        include (
    'http://www.domain.com/adsense.php');
    }

    add_shortcode('adsense''showads'); 
    (And you'd need to ensure that your PHP settings allow for such external URL includes.)
    Thank you so much Chip!
    Very helpful, it will be an external domain request, so your example will probably do the trick.

    You are best off using the WordPress http API for that - which WP Tavern member Jacob Santos wrote.
    Ryan,

    Does this mean the code would look like this?


    PHP Code:
    function showads() {
        
    wp_remote_get'http://www.domain.com/adsense.php' );
    }

    add_shortcode('adsense''showads'); 
    By the way, is this suggested forum feature going to be put in place?

    *Forum enhancement suggestion feature: Add personal PayPal donate buttons to forum member's signatures, sort of like an Add Reputation button. *
    You guys rock! - and if I can donate some beer starbucks $$ I would feel a lot better then providing a Thanks merely in writing.

  7. #7
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,801

    Default

    Quote Originally Posted by Edwin View Post
    Ryan,

    Does this mean the code would look like this?


    PHP Code:
    function showads() {
        return 
    wp_remote_get'http://www.domain.com/adsense.php' );
    }

    add_shortcode('adsense''showads'); 
    By the way, is this suggested forum feature going to be put in place?

    I think it would be something like the following (not 100% sure though as haven't used the WP http API in quite a while):
    PHP Code:
    <?php
    function showads() {
    wp_remote_retrieve_body'http://www.domain.com/adsense.php' );
    }
    add_shortcode'adsense''showads' );
    ?>

  8. #8
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,801

    Default

    Quote Originally Posted by Edwin View Post
    You guys rock! - and if I can donate some beer starbucks $$ I would feel a lot better then providing a Thanks merely in writing.
    Thanks :) There's a food chain. Otto (and others) help us, then we help you :)

    Perhaps you could feed the top of the chain as Otto has a beer money form on his site :) ... http://ottopress.com/

  9. #9
    Edwin is offline Hello World
    Join Date
    Nov 2009
    Posts
    47

    Default

    Thanks Ryan, I'll give that code a try.

    Quote Originally Posted by Ryan View Post
    Thanks :) There's a food chain. Otto (and others) help us, then we help you :)

    Perhaps you could feed the top of the chain as Otto has a beer money form on his site :) ... http://ottopress.com/
    Just made a donation. :)

    Beer on me!

  10. #10
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    865

    Default

    Wait, what? How did I get involved in this transaction? ;)

    Actually, you can include from a domain like that, but the trick is that the http request has to return PHP code, not PHP code that has been run. So this makes allow_url_include somewhat useless for most cases.

    Also, I didn't even know about wp_remote_retrieve_body. Seriously.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •