skip to Main Content

I am converting a static page to wordpress and in my index.php I use: <?= get_template_directory_uri() ?>
to create links, example:

<a href="<?= get_template_directory_uri() ?>/pages/login.php" class="btn btn-success btn-lg">Login</a>

and that works.

But I have a <a href="<?= get_template_directory_uri() ?>/pages/login.php" class="btn btn-success btn-lg">Login</a>that sends me to /pages/login.php successfully.

But inside login.php i have a <link href="<?= get_template_directory_uri() ?>/assets/css/bootstrap.min.css" rel="stylesheet" type="text/css" media="all"> and this give a error:
PHP Fatal error: Uncaught Error: Call to undefined function get_template_directory_uri() in...

I’m inexperienced with php, maybe I’m leaving something out?

2

Answers


  1. I’m inexperienced with php, maybe I’m leaving something out?

    Certainly. As the error message already tells:

    PHP Fatal error: Uncaught Error: Call to undefined function get_template_directory_uri() in …

    For the PHP engine that function is undefined. This means, PHP on its own does not have this function (also not any of the enabled extensions) and the PHP script that defines that function was not loaded.

    In that realm WordPress has a common set of functions, however it also has different entry-points (in your error case, the entry-point is the login.php script).

    Therefore some parts/sections of the application have more functions than others. This is an optimization technique, there is no need to load everything if only a fraction is needed to do the job, however it also means you can not expect that all functions are everywhere available.

    Especially not when you work with try-and-error and then getting the error message.

    When you look closer, this probably already makes sense to you: The login page (login.php) is independent to the website theme. It must always work, regardless the theme (e.g. to change it via the admin interface). Therefore no theme related functions need to be loaded and therefore — as that function is not available — you get the error.

    Luckily WordPress is available in it’s source code form. That is the preferred form to make changes to it. This comes with the benefit that you have all source code to read and study (reverse engineer from the point of error towards the point of definition via the entry-point where it works for you) and you should be able to finally find out if you still can get it to work (proof of concept).

    Then, review if this really makes sense and only if the function then can be called is actually solving your problem or if not having that function is a good default in the first place.

    I’d do the experiment nevertheless it may turn out afterwards it’s not the real deal. You’ll master any other upcoming problem, also with PHP in general, much better.

    Read, understand, plan, write and execute.

    Login or Signup to reply.
  2. If you want to use wordpress functions in a custom php script you must load wordpress first.

    You can achive this by adding this code in first line of your php script

    <?php
    require_once(Your_Path_To.'/wp-load.php');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search