Admin Columns does not show when I dynamically include (PHP) a Meta Box
Support › MB Admin Columns › Admin Columns does not show when I dynamically include (PHP) a Meta BoxResolved
- This topic has 4 replies, 2 voices, and was last updated 4 years, 5 months ago by
Teia Local Studio.
-
AuthorPosts
-
November 6, 2020 at 5:52 AM #22728
Teia Local Studio
ParticipantHej guys!
If I call my metaboxes using a condition like this one, the ADMIN COLUMNS does not show up on the posts list. I am trying to do this for the post_type PAGE.
// Here I get the ID fo the current page being edited $post_id = null; if ( isset( $_GET['post'] ) ) { $post_id = intval( $_GET['post'] ); } elseif ( isset( $_POST['post_ID'] ) ) { $post_id = intval( $_POST['post_ID'] ); } $post_type = get_post_type($post_id); // Then I have a function that search in my theme // options (Settings Page Extension) what MB should be included $mbs_to_be_included = check_mbs($post_id); // If it finds MBs to include to the current post being edited... foreach ($mbs_to_be_included as $extra_metabox) { include(TEMPLATE_PATH.'/metaboxes/'.$post_type.'/'.$extra_metabox.'/mb-foo-'.$post_id.'.php'); ?> }
All metaboxes load fine, they save fine, all is good... But if I set
'admin_columns' => true
, the columns display is completely ignored.If I bypass my PHP code and write/hardcode the metabox call manually, the very same, the columns are shown correctly in the pages list. But I can't do that, all MBs in the pages are loaded accordingly to specific configurations in the settings pages.
And yes, I did call the feature correctly and the extension is turned on.
To go further I manually forced my desired column to display via custom code (functions.php, not related to MB.IO at all) and it worked. So, it seems the problem is completely related to the "dynamic inclusion" via the current post being edited.
Any ideas?
Thank you!!!November 6, 2020 at 2:16 PM #22730Long Nguyen
ModeratorHi,
Could you please share the code of the function
check_mbs()
?I think it relates to this topic https://support.metabox.io/topic/code-not-working-with-updated-version/.
Instead of using the helper function to get the settings page value, you can use the function get_option() of WP core.
November 6, 2020 at 7:03 PM #22732Teia Local Studio
ParticipantHello Long!
Thanks for the return. I already get the options manually, never use the helper function for anything, a silly habit of mine... =)
But let's go — there is a lot of code in the middle, but mostly I do the following, simplifying things:
- On top of my functions.php I retrieve the related options and store inside a global variable:
global $mod; $mod = get_option( 'my-theme-custom-mbs' );
Every time I need this information, I bring up the $mod variable:
global $mod;
So, when I start the meta box registering, I simply use my function
check_mbs()
to read what meta box do I need to include based on the current$post_id
of the running edit screen. My function simplified:function check_mbs($post_id) { // Here I retrieve the settings options global $mod; if (is_array($mod['custom-mbs-for-post-'.$post_id])) { foreach ($mod['custom-mbs-for-post-'.$post_id] as $key => $val) { $my_mb_file = $key; $my_mb_dir = $val; } $custom_mb = array($my_mb_dir, $my_mb_file); return $custom_mb; } }
Then I just call for the meta box inclusion. I have this on a separate file for easy management. It is on a file called
custom_mbs_assembler.php
.$post_id = null; if ( isset( $_GET['post'] ) ) { $post_id = intval( $_GET['post'] ); } elseif ( isset( $_POST['post_ID'] ) ) { $post_id = intval( $_POST['post_ID'] ); } $post_tipo = get_post_type($post_id); $mbs = check_mbs($post_id); foreach ($mbs as $my_mb) { include(TEMPLATE_PATH.'/custom_mbs/'.$my_mb[0].'/custom-'.$my_mb[1].'-metabox.php'); }
All the code above run inside the "official" registering meta boxes method as instructed on the documentation. In my functions.php I have:
function my_theme_register_meta_boxes() { . . . include('static_mbs/general.php'); // A bunch of simple metabox all static, admin_colums OK! include('static_mbs/foo.php'); // Another bunch of simple metabox all static, admin_coluns OK! . . . include('custom_mbs/custom_mbs_assembler.php'); // The file mentioned above, no columns oh my =( . . . return $meta_boxes; } add_filter( 'rwmb_meta_boxes', 'my_theme_register_meta_boxes' );
By the metabox registering, data saving and retrieving, both on admin and template, all is working flawlessly. Furthermore, I even have conditional logic for INCLUDE/EXCLUDE (using the extension) to prevent some meta boxes to not being loaded/registered on several specific
$post_ids
, managed in the settings options.All fine so far...
Just admin_colums does not load for them.I understand that, without seeing the complete and original code, it will not help much, but anyway, if you have any other ideas of what could be wrong, I do appreciate. Otherwise, never mind, don't worry. I did manage to create my custom columns by hand, directly on the function.php.
But it is a pity to lose such great MB.IO functionality.
Thaaaanks!
Giovani.November 8, 2020 at 3:43 PM #22743Long Nguyen
ModeratorHi,
I think the problem comes from this code:
$mbs = check_mbs($post_id); foreach ($mbs as $my_mb) { include(TEMPLATE_PATH.'/custom_mbs/'.$my_mb[0].'/custom-'.$my_mb[1].'-metabox.php'); }
That means the meta box only shows when the function
check_mbs( $post_id )
has the input $post_id and returns an array while in the table listing posts, it does not have the $post_id and the meta box cannot be registered to show the admin columns.November 8, 2020 at 6:57 PM #22747Teia Local Studio
ParticipantOh, you solved the mystery, my friend Long Nguyen.
I am happy it is just a technical matter and not a mal-formed PHP code by my part. So, now I can live with it, no problems. Always thank you for the awesomeness of MetaBox!
Giovani.
-
AuthorPosts
- You must be logged in to reply to this topic.