Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call WordPress functions in a custom PHP script

Tags:

php

blogs

wpmu

I have a PHP script I want to use for creating a new blog in WPMU. I am having trouble calling WordPress functions like wpmu_create_user and wpmu_create_blog.

My hope is to get this script running as a cron job from the command line and pick up new blog creation requests from an external db, create a new blog using the WordPress functions and update the database with the new blog information.

like image 257
sid.sri Avatar asked Jan 19 '10 06:01

sid.sri


People also ask

How do you add a function in a custom .php file in WordPress?

You just need to include the file wp-load. php in your external file. The wp-load. php file is located in root of your WordPress installation.

How do I access the functions php file in WordPress?

Go to Appearance > Editor. On the right-hand side of the screen, you can see a list of all files contained in the theme. These differ depending on which theme you use, but one of the top options should be Theme Functions (functions. php).

How do you call a function php in WordPress?

Look for the file functions. php, which appears under the name of Theme Functions. By clicking it, you will be redirected to functions. php code editor, where you can manually enter the commands you want.

How do you call a function in WordPress?

To do this, call add_action() in the global execution space of your plugin file: add_action ( 'hook_name', 'your_function_name', [priority], [accepted_args] ); where: hook_name The name of an action hook provided by WordPress, that tells what event your function should be associated with.


2 Answers

include wp-load.php file (in the root of your wordpress installation) in your php script file like so,

require_once("/path/to/wordpress/wp-load.php"); 

you will have to provide the abspath of the wp-load file, now you can use all the functions of wordpress in your php script

like image 198
Pragati Sureka Avatar answered Oct 01 '22 20:10

Pragati Sureka


I've got a universal solution that will work in any PHP file inside the wp-content folder without any adjustments or needing to know what is mysterious 'path/to/wordpress'

require_once(explode("wp-content", __FILE__)[0] . "wp-load.php"); 

It just automatically goes up to root of WordPress and loads wp-load.php.

You can just paste it anywhere, no matter if it's a plugin or theme file, and it will work.

I think stuff like ../../../.. looks extremely bad and when you modify the structure of your folders of a theme or plugin you can get crazy.


Note: This solution assumes you didn't rename your wp-content folder.

like image 31
Adam Pietrasiak Avatar answered Oct 01 '22 18:10

Adam Pietrasiak