代码区项目交易流程

Why Use CMB2 Over ACF for Custom Fields in WordPress


If you develop WordPress sites regularly, you know that using a custom field manager plugin is a must. Achieving a complex behavior and handle mixed fields is possible in WP but by default, not the most advanced and easy-to-use system.


Why Use CMB2 Over ACF for Custom Fields in WordPress

When you start to search for a custom option/meta solution, you will fastly find the Advanced Custom Field plugin developed by Elliot Condon. The ACF is one of the most popular extensions in the WordPress ecosystem although it is a partly paid one and mostly useful for the devs.

You can almost do any metadata related task with ACF, there are a ton of 3rd party extensions for it, and the support/documentation is fantastic.

I’m using ACF for ages now, and I’m satisfied with it, but this doesn’t mean that there aren’t any good other solution. For me finding another extension for metadata handling came when I started to develop a freely available theme; for this, I couldn’t redistribute the plugin (ACF), and I didn’t want to expect the users to buy it. At the point, CMB2 came into the picture.

What Can The CMB2 Plugin Do

WebDevStudios develop the CMB2 , so the support and the quality are guaranteed.

Using CMB2, you can create anything in some way that you can create in ACF. Of course, these are two different pieces of software, so they are works differently.

Two main difference is that: CMB2 using the WordPress Core methods when it comes to store or retrieve data. So to get something you can use get_post_meta() function. In CMB2 you don’t get an admin UI to register your fields, you have to set them up in your code; this is not just a disadvantage but an advantage too.

For the field registration here is an example where you first create a section (box):

add_action( 'cmb2_admin_init', 'pine_alpha_register_theme_options_metabox' ); function pine_alpha_register_theme_options_metabox() { $prefix = '_pine_alpha_'; $cmb_post = new_cmb2_box( array( 'id' => $prefix . 'post_fields', 'title' => esc_html__( 'Theme Specific Settings', 'pine-alpha' ), 'object_types' => array( 'post' ) ) ); $cmb_post->add_field( array( 'name' => esc_html__( 'Show this in slider?', 'pine-alpha' ), 'desc' => esc_html__( 'If you check this field, this post will be shown in the slider section in the header if it is enabled.', 'pine-alpha' ), 'id' => $prefix . 'show_in_slider', 'type' => 'checkbox', 'column' => true ) ); }

For more information about the API check out the documentation !

In my opinion, the WebDevStudios’s extension is intuitive, works well in a lot of cases, and has proper documentation. If you worked custom meta before there won’t be any problem to switching. Sure your code and some UI element will look a little different, but after the first project, this is not a problem.

As in the case of ACF there a lot of 3rd party extension to help create any different solution.

If you develop a theme, you can easily integrate it into your theme files but the best method to use TGM Plugin Activation and set up as a requirement.

Is it Worth to Switch To CMB2?

I think you don’t have to switch. For me, it is not a problem to use them in parallel. For the open-source project I use CMB2, and for the client jobs, I use ACF.

In WordPress there is always another often better solution which worth our time. We can learn a lot from these experiences so if you feel so give it a try!

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

点击收藏

LAST How to develop a website with user and site security as a top priority New in Symfony 4.3: Always Include Route Default Values NEXT