7

Note: I am talking about _wp_get_current_user(); not wp_get_current_user().

If you check the function _wp_get_current_user(); you can see the following statement:

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness. Use wp_get_current_user() instead.

Why does WordPress have these types of private functions? Why doesn't WordPress allow to use these types of functions for WordPress plugin or theme developers?

2
  • 1
    This matters less in a language like PHP or open source project where you can look at the code for "private" functions anyway.
    – wtmwrer
    Commented Nov 17, 2016 at 20:13
  • 1
    happens to be the excellent question @Ranuka. I clicked the upwote 101 times but it only registered a single upwote. :(
    – prosti
    Commented Nov 29, 2016 at 14:21

3 Answers 3

18

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.

1
  • 2
    In essence, they're saying "these could change or go away without warning, don't go depending on them".
    – ceejayoz
    Commented Nov 18, 2016 at 0:05
6

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

1
  • like the last statement :) Commented Nov 18, 2016 at 5:08
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.