WordPress add_meta_box() function kodu

WordPress add_meta_box() function kodu

Bir veya daha fazla ekrana bir meta kutusu ekler.

function add_meta_box( $id, $title, $callback, $screen = null, $context = 'advanced', $priority = 'default', $callback_args = null ) {
    global $wp_meta_boxes;
 
    if ( empty( $screen ) ) {
        $screen = get_current_screen();
    } elseif ( is_string( $screen ) ) {
        $screen = convert_to_screen( $screen );
    } elseif ( is_array( $screen ) ) {
        foreach ( $screen as $single_screen ) {
            add_meta_box( $id, $title, $callback, $single_screen, $context, $priority, $callback_args );
        }
    }
 
    if ( ! isset( $screen->id ) ) {
        return;
    }
 
    $page = $screen->id;
 
    if ( ! isset( $wp_meta_boxes ) ) {
        $wp_meta_boxes = array();
    }
    if ( ! isset( $wp_meta_boxes[ $page ] ) ) {
        $wp_meta_boxes[ $page ] = array();
    }
    if ( ! isset( $wp_meta_boxes[ $page ][ $context ] ) ) {
        $wp_meta_boxes[ $page ][ $context ] = array();
    }
 
    foreach ( array_keys( $wp_meta_boxes[ $page ] ) as $a_context ) {
        foreach ( array( 'high', 'core', 'default', 'low' ) as $a_priority ) {
            if ( ! isset( $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ] ) ) {
                continue;
            }
 
            // If a core box was previously added or removed by a plugin, don't add.
            if ( 'core' == $priority ) {
                // If core box previously deleted, don't add
                if ( false === $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ] ) {
                    return;
                }
 
                /*
                 * If box was added with default priority, give it core priority to
                 * maintain sort order.
                 */
                if ( 'default' == $a_priority ) {
                    $wp_meta_boxes[ $page ][ $a_context ]['core'][ $id ] = $wp_meta_boxes[ $page ][ $a_context ]['default'][ $id ];
                    unset( $wp_meta_boxes[ $page ][ $a_context ]['default'][ $id ] );
                }
                return;
            }
            // If no priority given and id already present, use existing priority.
            if ( empty( $priority ) ) {
                $priority = $a_priority;
                /*
                * Else, if we're adding to the sorted priority, we don't know the title
                * or callback. Grab them from the previously added context/priority.
                */
            } elseif ( 'sorted' == $priority ) {
                $title         = $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ]['title'];
                $callback      = $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ]['callback'];
                $callback_args = $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ]['args'];
            }
            // An id can be in only one priority and one context.
            if ( $priority != $a_priority || $context != $a_context ) {
                unset( $wp_meta_boxes[ $page ][ $a_context ][ $a_priority ][ $id ] );
            }
        }
    }
 
    if ( empty( $priority ) ) {
        $priority = 'low';
    }
 
    if ( ! isset( $wp_meta_boxes[ $page ][ $context ][ $priority ] ) ) {
        $wp_meta_boxes[ $page ][ $context ][ $priority ] = array();
    }
 
    $wp_meta_boxes[ $page ][ $context ][ $priority ][ $id ] = array(
        'id'       => $id,
        'title'    => $title,
        'callback' => $callback,
        'args'     => $callback_args,
    );
}

Benzer İçerikler

Bir cevap yazın

E-posta hesabınız yayımlanmayacak.