use wordpress functions in external php file

To use WordPress functions in an external PHP file, you need to follow these steps:

  1. Include the WordPress core files: To access WordPress functions, you need to include the necessary core files. Add the following code at the beginning of your external PHP file:
<?php
require_once('/path/to/your/wordpress/wp-load.php');

Make sure to replace /path/to/your/wordpress/ with the actual path to your WordPress installation directory.

  1. Initialize WordPress: After including the core files, you need to initialize WordPress. This step is essential for accessing the WordPress functions and global variables. Add the following code after including the core files:
<?php
wp();
  1. Use WordPress functions: Once WordPress is initialized, you can use its functions in your external PHP file. For example, let's say you want to retrieve the title of the current post. You can use the get_the_title() function like this:
<?php
$title = get_the_title();
echo $title;

This code will output the title of the current post.

Remember to save your external PHP file with a .php extension and place it in the same directory as your WordPress installation or in a child theme's directory. This ensures that the file can access the necessary WordPress files.

That's it! By following these steps, you can use WordPress functions in your external PHP file.