webappguides
Create Wordpress Theme From Scratch! Build Your Own

What is admin_url and how it works in WordPress

The function admin_url fetches the administrator directory URL

The final output when the function is called is a path of directory wp-admin

The function offers the ability to access APIs like admin-ajax.php and comment.php

In short, there are 7 folders and 99 WordPress PHP files residing in wp-admin

In this post, you will find examples that demonstrate how admin_url returns the admin URL and its applications

What are the parameters of admin_url?

The function can pass two optional parameter

When you pass an empty parameter, the function returns the default value

The default value of admin_url always is a wp-admin path

For example, if you run a current website on a localhost

Then the output is http://localhost/wp-admin/

But if you plan to use a certain admin API you should pass extra data as perimeter

These arguments are

  1. $path
  2. $scheme

$path

A variable path represents the URL that point out the wp-admin directory

The $path accepts the custom URL for accessing a certain API( PHP file) that stays in the admin folder

Here is an example

<?php echo admin_url(‘edit-tags.php’);?>

Then, the result is http://your-domain.com/wp-admin/edit-tags.php

$scheme

$scheme variable passes the protocols which can be either non-ssl protocol or secured one

This means the output can display a URL with HTTPS or HTTP

If you let the variable value be https, the URL structure become https://yourdomain.com

But if there is no specified $scheme value a function passes admin as a default value

According to the WordPress developer page, the default value recognizes the functions that can enforce SSL if the website is secured with it

What are the applications of admin_url

This function is always used to get the access of the WordPress ajax

The wp-admin directory hosts a PHP file that helps to provide a data to be used by js file

The file that provides data to javascript is admin-ajax.php

So, if you want to access you will be required to pass a string ‘admin-ajax.php’ to admin_url

admin_url('admin-ajax.php');

In the end, the output is http:localhost/wp-admin/admin-ajax.php if the blog is hosted locally

How does it differ from get_admin_url?

get_admin_url is mostly useful for the multisite

As it can take an id of a particular site

While admin_url returns the admin URL of the current site only

In fact, get_admin_url contains three parameters

The additional argument is a variable $blog_id

$blog_id is a variable that passes the blog ID that you want to get its admin URL

This is its parameter structure

get_admin_url($blog_id, $path, $scheme)

But also the two functions work together

If you open a file link-template.php in the wp-includes directory

There is a complete code snippet of  admin_url, which involves the following lines

function admin_url($path='', $scheme='admin'){

return get_admin_url($blog_id=null, $path, $scheme);

}

This means admin_url passes the $path and $scheme values to its counterpart