admin管理员组

文章数量:1128710

For exercise I am working through a PHP class to add meta boxes I found on GitHub. I just copied the code and now I am playing around with it to understand it.

It works like this: The file containing the class is included on init. Inside that file, but outside the class, an empty array $meta_boxes is initialized.
After that, a custom action is executed, using apply_filters. My guess is apply_filters is used instead of do_action because the latter does not return anything – $meta_boxes = apply_filters( 'cmb_meta_boxes', $meta_boxes ).

Into that action, a function is hooked which adds to the existing array, which is returned at the end.

That function gets $meta_boxes passed as an argument.

TLDR;
An empty array is created, a function which adds to that array and returns it is fired. The function fires because it is hooked to a custom hook which is executed using apply_filters, because that way the function's return value can be assigned to a variable.

To me, that seems really to be a really complicated task for a pretty simple thing. Is this 'the WordPress way' of doing things or can the same result achieved with less code?

Why does the function that is hooked into the custom filter/action needs the array as an argument. Isn't it passed automatically to that function because it is added to the hook?

Maybe I haven't yet fully understood the way variables are handled inside WordPress. If you think this is the case, any links to explanations would be also gladly accepted.

For exercise I am working through a PHP class to add meta boxes I found on GitHub. I just copied the code and now I am playing around with it to understand it.

It works like this: The file containing the class is included on init. Inside that file, but outside the class, an empty array $meta_boxes is initialized.
After that, a custom action is executed, using apply_filters. My guess is apply_filters is used instead of do_action because the latter does not return anything – $meta_boxes = apply_filters( 'cmb_meta_boxes', $meta_boxes ).

Into that action, a function is hooked which adds to the existing array, which is returned at the end.

That function gets $meta_boxes passed as an argument.

TLDR;
An empty array is created, a function which adds to that array and returns it is fired. The function fires because it is hooked to a custom hook which is executed using apply_filters, because that way the function's return value can be assigned to a variable.

To me, that seems really to be a really complicated task for a pretty simple thing. Is this 'the WordPress way' of doing things or can the same result achieved with less code?

Why does the function that is hooked into the custom filter/action needs the array as an argument. Isn't it passed automatically to that function because it is added to the hook?

Maybe I haven't yet fully understood the way variables are handled inside WordPress. If you think this is the case, any links to explanations would be also gladly accepted.

Share Improve this question asked Sep 7, 2014 at 16:56 SvenSven 3711 gold badge6 silver badges18 bronze badges 1
  • There are actually a lot of good Q&A's about that topic on WordPress Development you just have to search them. – Nicolai Grossherr Commented Sep 7, 2014 at 17:03
Add a comment  | 

1 Answer 1

Reset to default 0

The basic premise for creating meta boxes from scratch is to first register a metabox, which calls a display callback that you then create and output fields in. And then of course you need to handle sanitizing, saving, and displaying that data yourself.

The class you're using isn't really the "WordPress-way" so much as a framework that serves to make creating meta boxes and sanitizing various types of fields easier.

If you're interested in learning how to do it "from scratch", here's a very simplified example for adding a new meta box:

/**
 * Register meta box(es).
 */
function wpdocs_register_meta_boxes() {
    add_meta_box( 'meta-box-id', __( 'My Meta Box', 'textdomain' ), 'wpdocs_my_display_callback', 'post' );
}
add_action( 'add_meta_boxes', 'wpdocs_register_meta_boxes' );

/**
 * Meta box display callback.
 *
 * @param WP_Post $post Current post object.
 */
function wpdocs_my_display_callback( $post ) {
    // Display code/markup goes here. Don't forget to include nonces!
}

/**
 * Save meta box content.
 *
 * @param int $post_id Post ID
 */
function wpdocs_save_meta_box( $post_id ) {
    // Save logic goes here. Don't forget to include nonce checks!
}
add_action( 'save_post', 'wpdocs_save_meta_box' );

Of course, there's also good information on the the add_meta_box() page in the Code Reference.

本文标签: filtersAdding to an array amp passing it through doactionapplyfilters