WordPress fatal error: Call to undefined function trailingslashit()
Call to undefined function trailingslashit()Short answer
trailingslashit() lives in wp-includes/formatting.php, which WordPress loads at startup. If PHP says the function is undefined, either the code is running without WordPress being loaded, or the core files are incomplete. Find the file in the stack trace first, then restore core files or stop that script from running directly.
Fix it fast
1Find out which cause you have
The error line names the file that called the function. Check whether that file is a plugin file, and whether the core file that defines the function exists.
ls -l wp-includes/formatting.php grep -rn "function trailingslashit" wp-includes/2Match what you see, then jump to its fix
What does the error look like?
Call to undefined function trailingslashit()
Cause 1 of 2
A plugin file runs on its own, outside WordPress
1Check
WordPress's plugin guide says files with code outside class and function definitions should stop when ABSPATH is not defined, which means WordPress has not been loaded.
head -n 12 wp-content/plugins/plugin-name/file.php2Fix
Add the ABSPATH guard at the top of the file, and start the script through WordPress (a hook, admin page or WP-Cron) rather than by calling the file directly.
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}3Verify
Requesting the file directly now ends quietly, and the plugin works through WordPress.
curl -I https://example.com/wp-content/plugins/plugin-name/file.phpConfirmed by developer.wordpress.org
Cause 2 of 2
Core files are missing or half-updated
1Check
The function is defined in wp-includes/formatting.php. If that file is absent or the grep above finds nothing, the core install is incomplete.
ls -l wp-includes/formatting.php
grep -n "function trailingslashit" wp-includes/formatting.php2Fix
Follow the manual update steps: back up, deactivate plugins, download a fresh WordPress package, replace the old files while keeping wp-config.php and wp-content, then run the upgrade from /wp-admin/.
# keep: wp-config.php, wp-content, .htaccess (custom rules)3Verify
The grep finds the function and wp-admin loads.
grep -n "function trailingslashit" wp-includes/formatting.phpConfirmed by developer.wordpress.org
How do I stop it coming back?
- Back up files and database before every core update.
- Guard every plugin file that runs code at load with the ABSPATH check.
- If one-click updates fail, check file permissions; the update guide names them as the likely reason.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix