clubmate.fi

A good[ish] website

Web development blog, loads of UI and JavaScript topics

Add custom fields with the WPAlchemy MetaBox PHP Class

Filed under: WordPress— Tagged with: plugins

Using custom fields in WordPress is imperative, the built in custom field UI has much to wish for. Helper Class to rescue.

There are a ton of custom meta box plugins. This is not a plugin, but a PHP class, giving you all the power how things are presented. Download and get to know it here.

Sometimes (almost always) when adding something to page via custom field it is good to have a if and else in case the field is left empty.

$custom_metabox->the_meta();
$custom_metabox->the_field('_material');
$my_custom_value = $custom_metabox->get_the_value();

// If the $my_custom_value is not empty
if (!empty($my_custom_value)) {
  echo $custom_metabox->get_the_value();
} else {
  // If it is empty
  echo '<span class="dehighlight">--</span>';
}

Comments would go here, but the commenting system isn’t ready yet, sorry. Tweet me @hiljaa if you want to make a correction etc.

  • © 2021 Antti Hiljá
  • About
  • Follow me in Twatter → @hiljaa
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • console.log('Smash the patriarchy!')
  • I love u!