You get that error, because the $_GET
and $_POST
are not filled on every admin page with that data. Also, your code should never rely on globals, they can be changed by any other code, so they are not reliable. So don’t use global $post;
, $_POST
or $_GET
when you can get the information from better sources like here.
Use the action add_meta_boxes
to … well … add metaboxes. If you want to restrict the metabox to just one post type use the action add_meta_boxes_$post_type
.
Here is how you register a metabox for the post type page
, and how to access the WP_Post
object in your callback function:
add_action( 'add_meta_boxes_page', function( \WP_Post $post ) {
if ( 62 === (int) $post->ID ) {
add_meta_box( 'contact_meta11','Social icons','social_icon','page','normal','high' );
}
});
function social_icon( \WP_Post $post ) {
}
Note: In my example, I write \WP_Post
, not just WP_Post
, because this class exists in the global namespace. I hope you are using your own namespace for your code. In that case, you have to write it like I did, because other wise PHP would try – and fail – to find the class in your namespace.