Finding Function in WordPress core files

The easiest way to learn how a specific WordPress function works then looking up a function in the core is the quickest way. Here you can see the exactly what parameters are passing or allowed to be sent to the function, and also that what the function actually does and what the values it is return. Before to start confirm that you have downloaded the latest version of WordPress locally to your computer or workstation. Now you will search these files as a reference for WordPress cms. Then open up any text editor it can search files (Textmate for Mac and TextPad for Windows are recommended). While searching for a function, then want to remove calls to that function from your search. It will do by including the word of “function” at the start of your search, wp_head like this function. In WordPress not everything is a function but this is a good place to start. When you don’t find any matches of your functions, remove “function” from the beginning of your search that you have searched. An important thing remember to set your text editor to search all files like that (*.*), not just .txt files if you set only text file it search in only text files.

Now look at the is_super_admin() function. What this functions do? is_super_admin() function is used to check that is a super admin in WordPress Multisite. Remember that before to use the function you should need to know exactly what values the function expects. Then Open your text editor or development toll and search for function is_super_admin in all the files of WordPress. Search should be produce that file is the file path wp-includes/capabilities.php: and fuction name is

function is_super_admin( $user_id = false ) {

Here, you notice that one parameter that can be sent to this function: and it is $user_id where $user_id it is the user id that is auto generated in database when a super admin profile created. Have you Notice that the inline documentation listed directly above the function mentioned. Here, the is_super_admin() documentation as seen below:

/**

* Determine if user is a site admin.

*

* @since 3.0.0

*

* In this parameter get the user id @param int $user_id (Optional) The ID of a user. It is by default the current user id.

* It has responsibility if use is true if retun true and it return false this comment in wordpress represent as and bool value either true or false @return bool True if the user is a site admin in not then it return false.

*/

Here is the lot of information and very easy to learn all the things in wordpress. Now see in the comments has a short description about what the function does and what are requirements, first comment you can see In this comments also notes when the function was added in the wordpress version (since version 3.0.0) and Information about that it pass the single parameter only, It telling about the parameter type also. In the comments also the details that what the return values will be. If a user is a site admin the function will return True if it is not then it return False.

This information to understand how this function works and what it perform, but let’s see into the code for a better understanding of function. Function few lines here:

if ( $user_id )

$user = new WP_User( $user_id );

else

$user = wp_get_current_user();

You know the $user_id parameter is optional it is mentioned in the comments of this function, IF $user_id is not passed in the function so what happens. Then if statement checks if the $user_id variable contains a value of $user_id then the WordPress User class is WP_User it called to retrieve the user data for that user ID and get all info about the user. If the $user_id variable has not value or it is empty, then the wp_get_current_user() function is called to get the user data or user info for the currently logged in user. After that the function checks that the $user data actually exists before to fetch the data against this id, if not then it will return false.

if ( ! $user->exists() )

return false;

Now that you know the $user data exists in database then you need to check that if a user is actually a super admin then:

if ( is_multisite() ) {

$super_admins = get_super_admins();

if ( is_array( $super_admins ) && in_array( $user->user_login,

$super_admins ) )

return true;

} else {

if ( $user->has_cap(‘delete_users’) )

return true;

}

Now break down and this if statement a bit:

if ( is_multisite() ) {

What this if statement does? This if statement checks by calling the is_multisite() function it check Multisite is actually running in WordPress. If the Multisite feature of WordPress has beenenabled then the Super admins will exists only.

After this process of function WordPress has determined that Multisite is running then the function calls get_super_admins() to retrieve or get an array of all super admins in WordPress using the following code is mentionded below:

$super_admins = get_super_admins();

Now the $super_admins variable is an array of all super admin login usernames. Next line is the most important line in this function it is the code of actually checks that a user is a super admin in WordPress exist:

$super_admins = get_super_admins();

if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )

return true;

Remember before working with an array, you shoud to verify always that the variable is an actual array using the is_array() PHP function. Other part of this line of code uses the in_array() PHP function it use to check the login user’s if exists in the super admin array if exist the user is a super admin and the function returns true.

is_multisite() if this function heck covered earlier returns false then the function will execute the following else code and what is in else code lets see:

} else {

if ( $user->has_cap(‘delete_users’) )

return true;

}

Now this preceding code checks if the user has the delete_users capability. This capability is assigned to regular administrator accounts in WordPress and it is assigned By defauld. In WordPress the Multisite is disable, but you are an administrator, code will return true after calling the is_super_admin() function. The final and last line of code in the function is:

return false;

If any of the checks or condition in the is_super_admin() function fail it will return false. For this more safety measure to be certain a true or false value is always returned by this function. Exploring this example, then it should be more apparent how useful the WordPress core code can be, and very easy to understand the code.

You have learned exactly how this function works by exploring the source code and how the wordpress is writing a code or function. So all the answers to your questions exist within the core file so it’s essential to have a good understanding of how to utilize the advantage of the core file and you can also made the your own functions after read this article and also make the changes in core file mostly web Design Company London use the wordpress function after changing.