Forums

calling custom function from non wordpress php file (6 posts)

  1. alternapop
    Member
    Posted 5 years ago #

    i'm learning php and created my own php file. if i call the main function within that php file and load it, it works. such as:

    mainfunction('var', 1);

    it successfully writes the expected text to the browser window.
    if i remove that main function call from that php file itself and add it to a .html page in the same directory like this:

    <body>
    <?php include("filename.php"); ?>
    <?php mainfunction('var', 1); ?>
    </body>

    and then load that .html file, nothing happens... no errors, no text is written to screen, etc. changing "include" to "require" does nothing different.

    if i add the comments to the top of my php file to turn it into a wp-plugin and activate it, then call the function the same way, it works.

    what am i missing?

    thanks!

  2. Otto
    Tech Ninja
    Posted 5 years ago #

    What you're missing is that only *.PHP files go through the PHP processor. So putting PHP code into an *.HTML file is rather pointless. You'd have to change your server setup to make it process *.HTML files as if they were *.PHP files.

    Extensions of files matter.

  3. alternapop
    Member
    Posted 5 years ago #

    hmm... ah....

    if i put this same .php file in my main wp directory or server root directory and include it the same way with an absolute path link, then call the function, in theory, should that work?

  4. Otto
    Tech Ninja
    Posted 5 years ago #

    Err... What?

    Includes only work in PHP files. You can't do includes from HTML files either. So, no.

    PHP code must be inside PHP files and made using server calls to PHP files. You can't make an HTML file with PHP code in it and expect it to work.

  5. alternapop
    Member
    Posted 5 years ago #

    that's what i meant. copy the php file to my server directory, somewhere. then within my wordpress template, where ever i want that code called, put those two lines (include and function call)...

    that should work, right?

    thanks!!

  6. alternapop
    Member
    Posted 5 years ago #

    still wondering how to do this... thanks

Topic Closed

This topic has been closed to new replies.

About this Topic