Skip to main content

Custom fields

This article is useful when you want to display certain data available as a custom field in Eduframe. The plugin adds these values to the data models for Products and Variants only. These values are available for you to show on your website.

Get the value of a custom field

In order to read the value of a custom field, we provide you with a method that is available on models named above. Lets say you have a custom field called My custom field with the slug my-custom-field. You can access this field like this:

// A variant post type, is either a planned course or program edition.
global $variant;
$field = $variant->get_custom( 'my-custom-field' );
echo $field;

// Example for a product custom field.
$product_field = $variant->get_product()->get_custom( 'my-product-field' );
echo $product_field

Hooks

You can change the values returned by the get_custom method using a hook. The hooks are formatted as follows:

// Model name can be either 'product' or 'variant', the key is the slug of the custom field.
'eduframe_{$model_name}_get_custom_{$key}'

You can then add a custom hook that runs when that field is called upon

add_filter( 'eduframe_product_get_custom_course-duration', function ( $value ) {
return $value . ' days';
} );

Assuming the value of course-duration is 10 the value will return 10 days instead.

global $product;
$field = $product->get_custom( 'course-duration' );
echo $field;

'10 days'