admin管理员组文章数量:1418088
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:
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?
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/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?
Share Improve this question asked Aug 25, 2019 at 2:42 NoobDevNoobDev 132 bronze badges1 Answer
Reset to default 0I 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.
本文标签: plugin developmentWhat is the source of the postid in a hook argument
版权声明:本文标题:plugin development - What is the source of the $post_id in a hook argument? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745217407a2648237.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论