• 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!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    ww.wp.xz.cn Admin

    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.

    Thread Starter alternapop

    (@alternapop)

    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?

    Moderator Samuel Wood (Otto)

    (@otto42)

    ww.wp.xz.cn Admin

    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.

    Thread Starter alternapop

    (@alternapop)

    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!!

    Thread Starter alternapop

    (@alternapop)

    still wondering how to do this… thanks

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘calling custom function from non wordpress php file’ is closed to new replies.