CoursePress, General WP Pages: PHP Include use in wordpress

Can I use PHP Include in wordpress pages as shown here?
http://www.w3schools.com/php/php_includes.asp

  • Tyler Postle
    • CGO

    Hey BGF,

    Hope you're doing well today!

    You want to use it in the actual page? The WP page editor won't accept any PHP; however, you can place it in the page template file :slight_smile:

    Usually page.php but you can also have page specific template files too, like:

    page-about.php - if you have a page with the url slug /about then the page-about.php file will be used for that.

    What are you wanting to do here exactly? Perhaps we can help :slight_smile:

    Look forward to hearing back!

    All the best,
    Tyler

  • BGF
    • The Incredible Code Injector

    Hi Tyler,

    Thanks for coming to the rescue!

    I want to
    1) Create this file:
    /_widgets/_GoogleCustomSearch_search_box.php

    2) Include the contents of the above file in page content, where ever I want it. This is easily done with <cfinclude> in ColdFusion. That's my background.

    The objective:
    Avoid pasting the google search box code over and over - if the Google Custom Search code changes I would have to find each instance of it and update it. With the <cfinclude> type method I would just update the /_widgets/_GoogleCustomSearch_search_box.php file

    If anyone can solve this it's you @Tyler Postle !

  • Tyler Postle
    • CGO

    Hey BGF,

    Thanks for the quick reply :slight_smile:

    Yes, so you can do this by adding this into your page.php file:

    <?php include('_widgets/_GoogleCustomSearch_search_box.php'); ?>

    Add that in wherever you would like to display it :slight_smile:

    If you want it into your posts too then you will want to add it to single.php

    If you're not too familiar with the WordPress template hierarchy yet, then the illustration here will help: http://codex.wordpress.org/Template_Hierarchy

    This way you can just update that one file and it will update it for everywhere you have it included.

    Hope this helps! If you need any further help just let us know.

    Cheers,
    Tyler

  • BGF
    • The Incredible Code Injector

    Thank you Tyler!

    I gather that the include will show on all pages.
    1) Is that correct?
    2) Is there a way that I can possible create a shortcode so that I can choose which pages I want to put it on?

  • Tyler Postle
    • CGO

    Hey BGF,

    No problem, happy to help :slight_smile:

    Yes, that would include it on all pages, unless you have any page specific templates.

    2) Is there a way that I can possible create a shortcode so that I can choose which pages I want to put it on?

    Do you have everything in _GoogleCustom_search_box.php stored in a variable? If so then you could do it like this:

    function include_gs_shortcode() {
    $html = '';
    include('_widgets/_GoogleCustomSearch_search_box.php');
    return $html;
    }
    add_shortcode( 'google-search', 'include_gs_shortcode' );

    $html being the variable.

    Then you can place it anywhere you like using this shortcode:

    [google-search]

    Might want to make the shortcode more unique to make sure any plugins you add in the future don't conflict with it. Like [bgf-google-search].

    Hope that helps!

    Cheers,
    Tyler

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.