Ga naar inhoud

Post meta box hooks

Deze inhoud is nog niet vertaald.

CampaignSuite Core adds a “CampaignSuite” meta box to the edit screen of pages and posts. Out of the box it holds one text field, the campaign code (cs_campaign_code). With the hooks on this page you can add fields, show the meta box on other post types and handle field types other than text.

Each field is an array keyed by its meta key, with these options:

Option Type Description
title string The label of the field.
type string The field type. text is rendered and saved by Core; any other type is handled by your own hooks (see the {type} hooks below).
show_public bool Output the value as a <meta> tag in the page head. The meta name is the meta key with underscores replaced by dashes.
show_in_list bool Add a column with the value to the post list table.
sortable bool Make that column sortable (needs show_in_list).

Filter · includes/class-meta-boxes.php

Filters the fields of the CampaignSuite meta box. Use it to add, change or remove fields. If the filtered array is empty, the meta box is not added.

Parameter Type Description
$fields array The fields keyed by meta key. Default: one cs_campaign_code field of type text that is public, shown in the list and sortable.

Returns: array — the fields keyed by meta key.

add_filter('cs_meta_box_fields', function ($fields) {
$fields['cs_partner_code'] = [
'title' => __('Partner code', 'my-plugin'),
'type' => 'text',
'show_public' => false,
'show_in_list' => true,
'sortable' => false,
];
return $fields;
});

Filter · includes/class-meta-boxes.php

Filters the post types (screens) that get the CampaignSuite meta box, the list table columns and the public meta tags.

Parameter Type Description
$screens string[] The post type names. Default: ['page', 'post'].

Returns: string[] — the post type names.

add_filter('cs_meta_box_screens', function ($screens) {
$screens[] = 'landing_page';
return $screens;
});

Action · includes/class-meta-boxes.php

Renders a field whose type is not text. The dynamic part {type} is the field’s type option, for example cs_meta_render_select_field for a field with 'type' => 'select'. Core renders text fields itself, so there is no hook for that type. The action runs inside the field wrapper <div>, so just echo your markup. Use the meta key as the input name, because that is the key read when the post is saved.

Parameter Type Description
$key string The meta key of the field.
$field array The field definition from cs_meta_box_fields.
$value mixed The saved meta value, or an empty string when there is none.
add_action('cs_meta_render_select_field', function ($key, $field, $value) {
printf('<label for="%s">%s</label>', esc_attr($key), esc_html($field['title']));
printf('<select id="%1$s" name="%1$s">', esc_attr($key));
foreach ($field['options'] as $option => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($option), selected($value, $option, false), esc_html($label));
}
echo '</select>';
}, 10, 3);

Filter · includes/class-meta-boxes.php

Filters the value of a field whose type is not text before it is saved. Every submitted value is first sanitized with sanitize_text_field(). Use this filter for types that need different sanitization, such as HTML or arrays. The dynamic part {type} is the field’s type option.

Parameter Type Description
$value string The value sanitized with sanitize_text_field().
$key string The meta key.
$post_id int The ID of the post that is saved.
$raw_value mixed The submitted value, unslashed but not sanitized.

Returns: string — the value to save.

add_filter('cs_meta_sanitize_textarea_value', function ($value, $key, $post_id, $raw_value) {
return sanitize_textarea_field($raw_value);
}, 10, 4);

Filter · includes/class-meta-boxes.php

Filters whether the current user may save the meta box fields for a post type other than page or post (those use the edit_page and edit_post capabilities). The dynamic part {post_type} is the post type of the saved post. The default is the result of current_user_can('edit_post', $post_id). The save still requires a valid nonce.

Parameter Type Description
$permission bool Whether the user may save. Default: the result of current_user_can('edit_post', $post_id).
$post_id int The ID of the post that is saved.

Returns: bool — true to let the fields be saved.

add_filter('cs_meta_save_landing_page_permission', function ($permission, $post_id) {
return $permission && current_user_can('publish_pages');
}, 10, 2);

Filter · includes/class-meta-boxes.php

Filters a field value before it is printed as a <meta> tag in the page head. Only fields with show_public and a non-empty value are printed, and only on a queried post (singular pages and a static posts page). The dynamic part {type} is the field’s type option, so cs_meta_text_value also applies to the built-in text fields. The result is escaped with esc_attr() when printed.

Parameter Type Description
$value mixed The saved meta value.
$post_id int The ID of the queried post.

Returns: string — the value to print as the content of the meta tag.

add_filter('cs_meta_text_value', function ($value, $post_id) {
return strtoupper($value);
}, 10, 2);