Support Forum
Hello,
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
Jochen
Hi,
Please follow this documentation to check if the Meta Box plugin is activated https://docs.metabox.io/functions/rwmb-meta/#faq
Hello 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?
Jochen
Hi,
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.
Thank 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() );
}