Check if MB plugin and extension is active
- This topic has 4 replies, 2 voices, and was last updated 2 years, 8 months ago by
[email protected].
-
AuthorPosts
-
August 17, 2022 at 6:25 PM #37547
[email protected]
ParticipantHello,
I use the snippet from here to list the featured image in the backend.
But if the MB Plugin is not active, my website breaks.So my question is now, how can I check if MB and the needed extensions are active?
I tried this with function_exists:/* Add Featured Images to Admin Columns * https://docs.metabox.io/extensions/mb-admin-columns/ * https://support.metabox.io/topic/displaying-featured-image-in-admin-columns/ */ if ( function_exists( 'mb_admin_column_load' ) ) { add_action( 'admin_init', 'prefix_add_custom_columns', 20 ); } function prefix_add_custom_columns() { class Prefix_Custom_Admin_Columns extends \MBAC\Post { public function columns( $columns ) { $columns = parent::columns( $columns ); $position = 'before'; $target = 'title'; $this->add( $columns, 'featured_image', 'Image', $position, $target ); // Add more if you want return $columns; } public function show( $column, $post_id ) { switch ( $column ) { case 'featured_image': the_post_thumbnail( [60, 60] ); break; // More columns } } } new Prefix_Custom_Admin_Columns( 'post', array() ); new Prefix_Custom_Admin_Columns( 'page', array() ); new Prefix_Custom_Admin_Columns( 'review', array() ); }
But this isn't working.
How to do it right, and where can I find the right functions for the plugin itself and all extensions?
thanks
JochenAugust 18, 2022 at 11:22 AM #37559Long Nguyen
ModeratorHi,
Please follow this documentation to check if the Meta Box plugin is activated https://docs.metabox.io/functions/rwmb-meta/#faq
August 18, 2022 at 5:06 PM #37564[email protected]
ParticipantHello Long,
sorry I am not a programmer. I don't know how to use this correctly.My site is still crashing after using this
add_action( 'admin_init', 'prefix_add_custom_columns', 20 ); function prefix_add_custom_columns() { if ( ! function_exists( 'rwmb_meta' ) ) { function rwmb_meta( $key, $args = [], $object_id = null ) { return null; } } class Prefix_Custom_Admin_Columns extends \MBAC\Post { public function columns( $columns ) { $columns = parent::columns( $columns ); $position = 'before'; $target = 'title'; $this->add( $columns, 'featured_image', 'Image', $position, $target ); // Add more if you want return $columns; } public function show( $column, $post_id ) { switch ( $column ) { case 'featured_image': the_post_thumbnail( [60, 60] ); break; // More columns } } } new Prefix_Custom_Admin_Columns( 'post', array() ); new Prefix_Custom_Admin_Columns( 'page', array() ); new Prefix_Custom_Admin_Columns( 'review', array() ); }
Can you please correct it for me?
JochenAugust 19, 2022 at 12:23 PM #37580Long Nguyen
ModeratorHi,
You can check Meta Box is activated before using the hook to register the custom columns.
if ( function_exists( 'rwmb_meta' ) ) { add_action( 'admin_init', 'prefix_add_custom_columns', 20 ); }
For other extensions, if you are using the plugin Meta Box AIO, it is so hard to check if it is available.
August 19, 2022 at 5:40 PM #37943[email protected]
ParticipantThank you Long.
I have added now "mb_admin_column_load" to the if, but it is still crashing if I deactivate AIO.
Is it not easier to check it the two plugins are active?
How can I do that?if ( function_exists( 'rwmb_meta' ) OR function_exists( 'mb_admin_column_load' ) ) { add_action( 'admin_init', 'prefix_add_custom_columns', 20 ); } function prefix_add_custom_columns() { class Prefix_Custom_Admin_Columns extends \MBAC\Post { public function columns( $columns ) { $columns = parent::columns( $columns ); $position = 'before'; $target = 'title'; $this->add( $columns, 'featured_image', 'Image', $position, $target ); // Add more if you want return $columns; } public function show( $column, $post_id ) { switch ( $column ) { case 'featured_image': the_post_thumbnail( [60, 60] ); break; // More columns } } } new Prefix_Custom_Admin_Columns( 'post', array() ); new Prefix_Custom_Admin_Columns( 'page', array() ); new Prefix_Custom_Admin_Columns( 'review', array() ); }
-
AuthorPosts
- You must be logged in to reply to this topic.