realbig / rbm-field-helpers

0 stars 1 forks source link

rbm_get_field() Default Option #24

Closed d4mation closed 6 years ago

d4mation commented 7 years ago

While get_post_meta() does not work in this way (and therefore may not be appropriate here), it is one thing that I've always appreciated about get_theme_mod(). You can provide a default value as a parameter and it will be used in the case that no data is found.

Very low priority as you can always just create your own conditional for using a preset default if it does not exist. It is neat though.

d4mation commented 6 years ago

We've got documented ways to set a default in the Wiki now, so I'll consider this good. Using helper functions like this for creating fields and such is how RBM FH will be interacted with in most cases anyhow I'd assume.

/**
 * Gets a meta field helpers field.
 *
 * @since {{VERSION}}
 *
 * @param string $name Field name.
 * @param string|int $post_ID Optional post ID.
 * @param mixed $default Default value if none is retrieved.
 * @param array $args
 *
 * @return mixed Field value
 */
function your_rbm_fh_prefix_get_field( $name, $post_ID = false, $default = '', $args = array() ) {
    $value = your_rbm_fh_prefix_fieldhelpers()->fields->get_meta_field( $name, $post_ID, $args );
    return $value !== false ? $value : $default;
}

/**
 * Gets a option field helpers field.
 *
 * @since {{VERSION}}
 *
 * @param string $name Field name.
 * @param mixed $default Default value if none is retrieved.
 * @param array $args
 *
 * @return mixed Field value
 */
function your_rbm_fh_prefix_get_option_field( $name, $default = '', $args = array() ) {
    $value = your_rbm_fh_prefix_fieldhelpers()->fields->get_option_field( $name, $args );
    return $value !== false ? $value : $default;
}