WordPress.org

Ready to get started?Download WordPress

Forums

search widget on my non-wordpress home page? (8 posts)

  1. carlabaldwin
    Member
    Posted 1 year ago #

    I have a custom site that has a wordpress blog (in a sub-directory). Is it possible to have the search box (widget) appear on my custom home page? I was hoping for a snippet of code to insert on my home page.
    thank you!

  2. wpismypuppet
    Member
    Posted 1 year ago #

    If you want to use WordPress outside of WordPress you simply need to require() wp-load.php, which is found in the root of the WordPress installation. Since your site is in a sub-directory, you'll want to look in that folder. Then you'll call the search form where ever you want it. So something like this:

    <?php
        require( 'subfolder/wp-load.php' );
        get_search_form();
    ?>

    Things to pay attention to...

    1) The path inside the require() function will change according to where you are relative to where the wp-admin.php file is. Also, replace "subfolder" with your actual folder name :).

    2) The require() function need only be called ONCE per page it's used on. In fact, I use require_once() as it does the same thing, but does a check to make sure the file hasn't already been called. Just a safety measure on my part.

    3) Though the require() function CAN go anywhere, it's generally good practice to place it a the very top of the file. This means you would separate the get_search_form() function from the require() function, as you'll want the search form somewhere appropriate on the page

    Let me know if this makes sense. Good luck.

  3. carlabaldwin
    Member
    Posted 1 year ago #

    I have this in the beginning of my <body>
    <?php
    require( 'wordpress/wp-load.php' );
    ?>

    and this where I want the search widget to appear
    <?php get_search_form(); ?>

    but it is not executing the code. 'wordpress' is my wordpress/blog directory, where the wp-admin directory is and the wp-load.php file is. Not sure if I am missing something. Thank you for your response!

  4. wpismypuppet
    Member
    Posted 1 year ago #

    Ah... you might need the absolute path instead of a relative one. Try this instead:

    <?php require( $_SERVER['DOCUMENT_ROOT'] . '/wordpress/wp-load.php' ); ?>

  5. carlabaldwin
    Member
    Posted 1 year ago #

    still no luck. The site is http://www.knowthyselfwellness.com. The search widget should appear under the mailing list form. when I view source I see the code but not the data it should have pulled in.

  6. wpismypuppet
    Member
    Posted 1 year ago #

    Well, the fact that you seeing the actual PHP code is the problem. It's not being parsed by the PHP compiler. So my guess is the filename does not have a .php extension? Maybe the filename is index.htm or index.html? Renamve the file and give it a .php extension. Then it will be parsed properly and all should work.

  7. carlabaldwin
    Member
    Posted 1 year ago #

    That worked! thank you for your help, it is much appreciated.

  8. wpismypuppet
    Member
    Posted 1 year ago #

    You are welcome. Please mark this post as resolved so other people know. Glad I could help!

Topic Closed

This topic has been closed to new replies.

About this Topic