TAGS :Viewed: 15 - Published at: a few seconds ago

[ How to allow PHP to find functions ]

I am new to PHP. I have a function that is in a folder. PHP can't seem to find this function. What can I do to allow PHP to find/see the file that contains the function I am trying to use?

EDIT:

Here is the code I am using

<?php

include './blog/wp-includes/post.php';

$recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent )
{
echo '<li><a href="' 
        . get_permalink($recent["ID"]) 
        . '" title="Look '
        .esc_attr($recent["post_title"])
        .'" >' 
        .   $recent["post_title"]
        .'</a> </li> ';
    }

but when I execute the code, I get:

Fatal error: Call to undefined function add_action() in /home/hilan1/public_html/site/blog/wp-includes/post.php on line 144

Answer 1


Also look into autoloading if you're using classes:

edit

You will need to include the file that contains the WordPress function add_action() before using another function that uses it.

Answer 2


I will make you an example:

Suppose you have the following structure in your root:

  • index.php
  • important (folder) / functions.php (inside folder)

Example of the "functions.php" file:

<?php

  function HelloWorld($name){
    return "Hello ".$name;
  }  

?>

In your index.php file do the following:

<?php
include('important/functions.php');

$name = "Oscar";
$result = HelloWorld($name);
echo $result;

?>

That's a simple way to invoke functions in other files.

Hope this helps.