0

I am learning meta boxes in Wordpress theme development. In a tutorial I watched, tutor wrote a function for saving meta box values. As an argument he add $post_id and $post.

I know $post is a global variable but what about $post_id, I wonder where is it coming from.

A little bit of research i did before asking here, I found this: https://wordpress.stackexchange.com/a/299134/172202

One of the answers in the link, It says:

Another way you might get a post ID is in a hook callback. For example, in the post_thumbnail_size hook the callback receives a post ID as the 2nd argument

And gives this example

function wpse_299132_post_thumbnail_size( $size, $post_id ) { return $size; } add_filter( 'wpse_299132_post_thumbnail_size', 'wpse_299132_post_thumbnail_size', 10, 2 );

The Question I Have
In the filter hook I know 10 is priority and 2 is number of arguments as this question's anwers says: Which hook callback has priority if both plugin and theme use the same hook?

But since that 2 parameter is the number of arguments, it is not the n th argument, because the id parameter inserted as the second parameter, I first thought it is the n th parameter but with the question's answer above, i found out that it is actually number of arguments. I thought if you name the parameter as $post_id, the hook automatically adds the post id value, but that's not the case?

And first question link's answer also says this:

But that's just the name used in the documentation to make it clear what the variable contains. You can call it anything you like. This is also valid, for example:

function wpse_299132_post_thumbnail_size( $size, $myPostId ) { return $size; } add_filter( 'wpse_299132_post_thumbnail_size', 'wpse_299132_post_thumbnail_size', 10, 2 );

$myPostId is the 2nd argument, so will contain a post ID. But what you call it doesn't matter.

If 2 in the hook parameter would be n th argument, if we would add $myPostId as the first parameter, it wouldn't work, but it is actually number of arguments.

How the hooks know if it should give post id value to the parameter?

1 Answer 1

0

I thought if you name the parameter as $post_id, the hook automatically adds the post id value, but that's not the case?

Correct, that's not the case. What you name it has no effect on what value WP passes through that argument. If you check out the reference for the post_thumbnail_size hook, you can see there that they will always pass the thumbnail size in the first argument, and the post ID in the second argument.

You could even swap the names, and you'd still get the size values in the first and the post ID in the second:

function wpse_299132_post_thumbnail_size( $post_id, $size ) {
    echo "Size: ";
    print_r($post_id);
    echo "Post ID: $size";
}
add_filter( 'wpse_299132_post_thumbnail_size', 'wpse_299132_post_thumbnail_size', 10, 2 );

Outputs (for example):

Size: Array(
    [0] => 120
    [1] => 120
)
Post ID: 1647

How the hooks know if it should give post id value to the parameter?

Each hook has a predetermined set of arguments; for any given hook, you can check the code reference to see exactly which argument will be passing what value.

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.