wppaste
WordPress

self_admin_url( string $path = '', string $scheme = 'admin' ): string

Since
3.1.0
Source
wp-includes/link-template.php:3898

Builds an admin URL that automatically switches between the network admin, user admin, or regular admin screen based on the current request context. Use it for links that must stay correct whether they render on wp-admin, wp-admin/network, or wp-admin/user pages. On a single site install it behaves exactly like admin_url() since neither is_network_admin() nor is_user_admin() will be true.

Retrieves the URL to the admin area for either the current site or the network depending on context.

Compatibility

WordPress
since 3.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$pathstringoptional
Path relative to the admin URL. Default empty.Default: ''
$schemestringoptional
The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.Default: 'admin'

Return value

string
Admin URL link with optional path appended.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Build a redirect URL after saving a plugin setting

A settings page handler wants a link back into wp-admin that stays correct no matter which admin context the code runs in.

$redirect_url = self_admin_url( 'options-general.php?page=my-plugin&updated=1' );

echo esc_html( $redirect_url );

On this single-site sandbox this resolves the same way admin_url() would, the difference only shows up on multisite network or user admin screens.

An upgrader-style notice wants to guarantee its link uses https even if force_ssl_admin() is off.

$secure_link = self_admin_url( 'plugins.php?activated=true', 'https' );

printf( 'Return to plugins: %s', esc_html( $secure_link ) );

Common problems and fixes · 4

Why does self_admin_url just give me the same thing as admin_url?

The function only differs from admin_url() when is_network_admin() or is_user_admin() returns true. On a normal single-site request neither is true, so it falls straight through to the admin_url() branch.

How do I change the URL self_admin_url produces before it's used?

The final URL, path and scheme are passed through the self_admin_url filter right before the function returns, so you can rewrite it there instead of patching every call site.

Do I need to escape the value before printing it in HTML?

self_admin_url() returns a raw URL string, it does not run esc_url() internally, so printing it directly into markup without escaping risks broken or unsafe output.

Will this pick the wrong admin URL if I call it from an AJAX handler?

The result depends entirely on is_network_admin() and is_user_admin(), both of which check the current request's screen context. Calling the function from a context where those checks don't reflect the page the link will actually be shown on can produce a URL for the wrong admin area.

Alternatives and related functions

admin_url
When you know the link should always point at the current site's regular wp-admin, regardless of network or user admin context.
network_admin_url
When you specifically need a link into the network admin screens on a multisite install.
user_admin_url
When you specifically need a link into the user admin screens (used for personal settings on multisite).
home_url
When the link should point at the public front end of the site rather than any admin screen.

Performance profile

How much work a call to self_admin_url() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
18–21

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 32.

Plugin surface
1 hook

Third-party callbacks on 'self_admin_url' run inside this call, and their cost is not bounded by anything here.

Called by
40

40 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost self_admin_url() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
is_network_admin()18is_network_admin(), network_admin_url(), apply_filters()
!is_network_admin() && !is_user_admin()20is_network_admin(), is_user_admin(), admin_url(), apply_filters()
!is_network_admin() && is_user_admin()21is_network_admin(), is_user_admin(), user_admin_url(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 32 instructions, 18–21 executed per call, 2 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while self_admin_url() runs, in this order:

  1. apply_filters( self_admin_url )filterline 3916 (+18 into the body)

    Filters the admin URL for the current site or network depending on context.

Uses · 6

  • is_network_admin()Determines whether the current request is for the network administrative interface.
  • network_admin_url()Retrieves the URL to the admin area for the network.
  • is_user_admin()Determines whether the current request is for a user admin screen.
  • user_admin_url()Retrieves the URL to the admin area for the current user.
  • admin_url()Retrieves the URL to the admin area for the current site.
  • apply_filters()Calls the callback functions that have been added to a filter hook.

Used by · 40

Show all 40

Source code

function self_admin_url( $path = '', $scheme = 'admin' ) {	if ( is_network_admin() ) {		$url = network_admin_url( $path, $scheme );	} elseif ( is_user_admin() ) {		$url = user_admin_url( $path, $scheme );	} else {		$url = admin_url( $path, $scheme );	} 	/**	 * Filters the admin URL for the current site or network depending on context.	 *	 * @since 4.9.0	 *	 * @param string $url    The complete URL including scheme and path.	 * @param string $path   Path relative to the URL. Blank string if no path is specified.	 * @param string $scheme The scheme to use.	 */	return apply_filters( 'self_admin_url', $url, $path, $scheme );}

Changelog

Introduced in 3.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/link-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.