• I’ve made one page-contact.php in which I’ve included script.php file where I’ve kept the contact form sending script. I’ve included this page on the end of form. When I try to submit a blank contact form then I see that it goes to my script.php where a pop-up message appears as “FILL THE BLANK FIELDS”. By this it’s clear that I’ve placed the right path. But when I fill the form and click to submit then it shows message as “Oops page not found”. Why is it not working?
    I’ve included file as: <?php include_once('script.php'); ?> I just want to make my contact form work without using plugins.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Relative include paths are not always reliable, you should strive to use absolute paths whenever possible. For example, if your file is in the WP installation folder (not a good choice, it’s just an example), do something like this:
    <?php include_once( get_home_path() . 'script.php'); ?>

    When you say you don’t want to use plugins, if you mean third party plugins, I totally get it. But there’s nothing wrong with a custom, site specific plugin of your own making that contains all of your site’s custom code. Not only is it a convenient platform to implement hooks, it’s a good, safe place to store your various files. Similar things are achieved by using a child theme, assuming your current theme hasn’t taken up that slot already. (Framework based themes, like those from ThemeForest et.al., are usually installed as a child theme)

    Thread Starter jklyn

    (@jklyn)

    I pasted the above code exactly to my file and it doesn’t go through to the included file. Do I need to change the code I mean replace get_home_path by my site address or other? Please clear me.

    Moderator bcworkz

    (@bcworkz)

    Sorry for the slow reply.

    Where exactly does the file to be included reside? My example would only work if it were in the WP installation root, which is not the right place for such a file. The point was you want a fully qualified path, not an URL or URI. Something like /home/content/01/9990001/html and not https://www.example.com. get_home_path() will return something like the first example. Once you have the full home path, you’ll need to add whatever other folders are involved to reach the file. For example, a full path to a theme’s sub-folder file might be /home/content/01/9990001/html/wp-includes/themes/my-theme/includes/script.php

    You can also get the complete path to the folder of the current .php file with dirname(__FILE__), so if both files are in the same folder, this should work for you:
    <?php include_once( dirname(__FILE__) . '/script.php'); ?>

    BTW, if the existence of script.php is absolutely needed for the proper functioning of your script, consider using require_once() instead of include_once(), which will throw a fatal error if the file is not found instead of just a warning. Then it’s clear where the problem lies.

    Thread Starter jklyn

    (@jklyn)

    Actually I need to include this file to my page-contact.php. I didn’t use the plugin to send contact form. I’ve included script to make it work. When I click submit to blank form then it goes to the script and says that “FILL THE REQUIRED FIELDS” that means it goes to the correct path that I’d mentioned i.e. script.php. But surprisingly when I fill the form and click on submit then it shows “oops! the page is not found.” I’ve placed the script to send contact form fields on my mail to script.php which I’ve used while working on my php files and it used to work well but I can’t make it work on wp.

    Moderator bcworkz

    (@bcworkz)

    You know what? It just occurred to me. The “Oops” message is not something that would be generated by a bad include path. All that should happen with a bad include path is a PHP warning would be entered in the error log. The oops message is generated by WP when it can’t find a post matching the request. When a file cannot be found by the file system, the request is forwarded to WP for it to figure out. This is how random permalinks without matching files end up in WP. Erroneous file requests get the same treatment.

    It sounds to me like a bad HTML file request somewhere, not a bad include path. Are you sure the form’s action path is correct? Check it in the form’s source view of your browser.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to include external php file on wp?’ is closed to new replies.