Wordpress - Why does WordPress have private functions?

It is quite normal practice for code to not be a part of public API.

But much of WP code is ancient and procedural. There are no technical ways to make a private function.

These are semantically private, that is WP doesn't want you to use them, but it cannot actually forbid you to. There is a long history of "private" WP APIs being actively used in practice by extensions.

The reasons for declaring something private vary from case to case. In this specific case you raised the reason seems to be that "public" version is pluggable, so moving implementation to a "private" version allows original to be replaced more easily / with less issues.


Put simply, developers will choose to make those internal functions 'private' because they don't want to have to provide public support for them. For example, they don't guarantee that any function arguments will be kept consistent in placement or even existence from update to update.

Not that this actually stops plenty of developers from using 'private' functions anyway...


It was not at first absolutely clear to me so I needed to check out deeper. If you take for instance the great wp-includes/user.php file you will not find a single PHP class definition in there.

File: /wp-includes/user.php
2452:  * @since 4.5.0
2453:  * @access private
2454:  *
2455:  * @see wp_get_current_user()
2456:  * @global WP_User $current_user Checks if the current user is set.
2457:  *
2458:  * @return WP_User Current WP_User instance.
2459:  */
2460: function _wp_get_current_user() {

Yet you will find the _wp_get_current_user() function we are interested in. What you see @access private is just a tip for the PHP doc generator.

This also indicates that this function in the future may become private function inside of a class.

The concept of a private function exists only when you have PHP classes and this is with PHP 5 or later (since then PHP is Object Oriented). The concept is called encapsulation.

WordPress gradually improves the PHP code by introducing classes, but the process will not happen over the night.

Tags:

Functions