Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

WordPress fatal error: Call to undefined function trailingslashit()

ErrorCall 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.

  • Applies to WordPress 7.1, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/
  2. 2Match what you see, then jump to its fix

    1. 1the path in the error is under wp-content/plugins and was opened by URL, cron or the command lineA plugin file runs on its own, outside WordPressFix
    2. 2ls shows no wp-includes/formatting.php, or the error appears on every page after a failed updateCore files are missing or half-updatedFix

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.php

2Fix

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.php

Confirmed by developer.wordpress.org

Did this fix it?

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.php

2Fix

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.php

Confirmed by developer.wordpress.org

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.