Redeclaring a plugin function within a class

In the code below, a user defined add_meta_box() function is hooked to add_meta_boxes action. How does wordpress know that the add_meta_box() function used inside the user-defined add_meta_box( $post_type ) function is a plugin function and not a recursive one?

class someClass {
 
    /**
     * Hook into the appropriate actions when the class is constructed.
     */
    public function __construct() {
        add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
        add_action( 'save_post',      array( $this, 'save'         ) );
    }
 
    /**
     * Adds the meta box container.
     */
    public function add_meta_box( $post_type ) {
        // Limit meta box to certain post types.
        $post_types = array( 'post', 'page' );
 
        if ( in_array( $post_type, $post_types ) ) {
            add_meta_box(
                'some_meta_box_name',
                __( 'Some Meta Box Headline', 'textdomain' ),
                array( $this, 'render_meta_box_content' ),
                $post_type,
                'advanced',
                'high'
            );
        }
    }
 
    /**
     * Save the meta when the post is saved.
     *
     * @param int $post_id The ID of the post being saved.
     */
    public function save( $post_id ) {
 
        /*
         * We need to verify this came from the our screen and with proper authorization,
         * because save_post can be triggered at other times.
         */
 
        // Check if our nonce is set.
        if ( ! isset( $_POST['myplugin_inner_custom_box_nonce'] ) ) {
            return $post_id;
        }
 
        $nonce = $_POST['myplugin_inner_custom_box_nonce'];
 
        // Verify that the nonce is valid.
        if ( ! wp_verify_nonce( $nonce, 'myplugin_inner_custom_box' ) ) {
            return $post_id;
        }
 
        /*
         * If this is an autosave, our form has not been submitted,
         * so we don't want to do anything.
         */
        if ( defined( 'DOING_AUTOSAVE' )  DOING_AUTOSAVE ) {
            return $post_id;
        }
 
        // Check the user's permissions.
        if ( 'page' == $_POST['post_type'] ) {
            if ( ! current_user_can( 'edit_page', $post_id ) ) {
                return $post_id;
            }
        } else {
            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                return $post_id;
            }
        }
 
        /* OK, it's safe for us to save the data now. */
 
        // Sanitize the user input.
        $mydata = sanitize_text_field( $_POST['myplugin_new_field'] );
 
        // Update the meta field.
        update_post_meta( $post_id, '_my_meta_value_key', $mydata );
    }
 
 
    /**
     * Render Meta Box content.
     *
     * @param WP_Post $post The post object.
     */
    public function render_meta_box_content( $post ) {
 
        // Add an nonce field so we can check for it later.
        wp_nonce_field( 'myplugin_inner_custom_box', 'myplugin_inner_custom_box_nonce' );
 
        // Use get_post_meta to retrieve an existing value from the database.
        $value = get_post_meta( $post-ID, '_my_meta_value_key', true );
 
        // Display the form, using the current value.
        ?
        label for=myplugin_new_field
            ?php _e( 'Description for this field', 'textdomain' ); ?
        /label
        input type=text id=myplugin_new_field name=myplugin_new_field value=?php echo esc_attr( $value ); ? size=25 /
        ?php
    }
}

Topic plugin-development Wordpress

Category Web


How does wordpress know that the add_meta_box() function used inside the user-defined add_meta_box( $post_type ) function is a plugin function and not a recursive one?

It doesn't, WordPress just calls it, so you can indeed create infinite loops.

Instead what I think you're asking is how PHP knows to call that class method and not the function with the same name. Remember, WordPress is an application written in PHP, it's not a programming language itself, and you're writing PHP code that calls WordPress functions/APIs, not WordPress code.

The answer, is that the parameter expects a callable value, something PHP recognises as something it can call, e.g. the name of a function, an anonymous function, etc.

The answer becomes clearer when you compare the following:

  • 'bar' is equivalent to bar();
  • array( $this, 'bar' ), is equivalent to $this->bar();
  • array( 'Foo, 'bar' ), is equivalent to Foo::bar()
  • etc...

As you can see, the code in your question used an array, where the entry is the object, and the second entry is the name of the method to call.

It's also helpful to refer to these by their proper names, e.g. a function inside a class is a class method, sometimes known as a class function. This is different from a normal function.

For more information on what callables are and how they work:

https://www.php.net/manual/en/language.types.callable.php

Callables are a PHP language feature, and one that WordPress relies on for specifying callbacks for actions/hooks/filters and other APIs.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.