Support Log in

39. Security Considerations

Developer Guide
Prefer video? Watch the Field Forge tutorials 8 short guides covering every feature Watch

Best practices for securing custom field data in Field Forge.

Output Escaping

Always escape field values before output. Field Forge does not auto-escape values returned by get_field().

php
// Text fields -- use esc_html()
echo esc_html( get_field( 'headline' ) );

// URLs -- use esc_url()
echo '<a href="' . esc_url( get_field( 'link' ) ) . '">Click</a>';

// HTML attributes -- use esc_attr()
echo '<div class="' . esc_attr( get_field( 'css_class' ) ) . '">';

// A WYSIWYG value saved from the post editor went through wp_kses_post()
// when it was saved. A value written with update_field() did not, so
// filter it when you print it.
echo wp_kses_post( get_field( 'body_content' ) );

Input Validation

When a simple field is saved from the post editor, Field Forge cleans the value by its field type: sanitize_email() for Email, esc_url_raw() for URL, oEmbed and Page Link, wp_kses_post() for WYSIWYG, sanitize_textarea_field() for Textarea, and sanitize_text_field() for most other single-value types, such as Text and Password. A Number or Range value that is not numeric is saved as an empty string. PUT /fields/{post_id} cleans values the same way, except Repeater, Group, Flexible Content and Clone values.

This is cleaning, not validation: nothing checks min, max or step when a value is saved. The one check is the maxlength (Character limit) of a Text or Textarea field: a text longer than the limit is not saved by the post editor, and PUT /fields/{post_id} answers 400 and saves nothing from that request. update_field() does not check it. The Number and Range inputs carry min, max and step attributes when the field has them, so the browser applies those while someone edits, but a request that does not come from the editor skips them. update_field() stores the value you pass without cleaning it, so check and clean your values first:

php
// Sanitize before update_field()
$email = sanitize_email( $_POST['user_email'] );
if ( is_email( $email ) ) {
    update_field( 'contact_email', $email, $post_id );
}

$price = floatval( $_POST['price'] );
if ( $price >= 0 ) {
    update_field( 'price', $price, $post_id );
}

REST API Security

Mutation endpoints (POST, PUT, DELETE) require the manage_options capability. The field group reads (GET /field-groups and GET /field-groups/{id}), GET /aggregate and GET /options/{page} also require manage_options by default. A site can widen the first two and the aggregate with the filters fieldforge/rest/read_groups_permission and fieldforge/rest/aggregate_permission, for example for a headless frontend. The per-post values endpoint (GET /fields/{post_id}) needs a logged-in user and uses current_user_can('read_post', $post_id), so private posts are gated the same way WordPress core gates post meta. Anonymous requests to these endpoints get 401 unless a site has widened one of those filters. For users without manage_options (possible only after widening), GET /field-groups leaves out field groups marked private (or restricted by location rules to administrators), and GET /field-groups/{id} answers 403 for such a group. The public fieldforge property on core WordPress post endpoints only exposes opt-in fields as described in section 20. See section 20 for the full capability matrix per endpoint.

php
// Opt one field in to the public fieldforge property of the core REST post endpoints
add_filter( 'fieldforge/rest/public_field_allowed', function( $allowed, $field, $post_id ) {
    return ( $field['name'] ?? '' ) === 'public_subtitle' ? true : $allowed;
}, 10, 3 );

Password and Sensitive Fields

The password field type stores values as plain text. For sensitive data, encrypt the value after it is saved and decrypt it when it is loaded. The save hook reads the stored row with get_raw(), because get_value() runs the fieldforge/load_value filter, which in this example decrypts. It unhooks itself while it writes, because update_value() fires fieldforge/value_updated again. The value is encrypted with AES-256-CBC and a new random IV for every save. The IV is stored in front of the cipher text, after the prefix enc1:, so a value that was saved before you added this code is still read as it is:

php
// Encrypt on save
function my_secret_key() {
    // 32 raw bytes derived from a constant, the length AES-256 needs
    return hash( 'sha256', AUTH_KEY, true );
}

function my_encrypt_value( $plain ) {
    $iv     = random_bytes( 16 ); // a new random IV for every value
    $cipher = openssl_encrypt( $plain, 'aes-256-cbc', my_secret_key(), OPENSSL_RAW_DATA, $iv );
    if ( $cipher === false ) {
        return $plain;
    }
    return 'enc1:' . base64_encode( $iv . $cipher ); // the IV is stored in front of the ciphertext
}

function my_decrypt_value( $stored ) {
    if ( ! is_string( $stored ) || strpos( $stored, 'enc1:' ) !== 0 ) {
        return $stored; // not encrypted, for example saved before this code was added
    }
    $bin = base64_decode( substr( $stored, 5 ), true );
    if ( $bin === false || strlen( $bin ) <= 16 ) {
        return $stored;
    }
    $plain = openssl_decrypt( substr( $bin, 16 ), 'aes-256-cbc', my_secret_key(), OPENSSL_RAW_DATA, substr( $bin, 0, 16 ) );
    return $plain === false ? $stored : $plain; // on failure keep the stored value, not an empty one
}

function my_encrypt_api_secret( $post_id, $field_name ) {
    if ( $field_name !== 'api_secret' ) {
        return;
    }
    $values = FIELDFORGE_Field_Values::instance();
    $row    = $values->get_raw( 'api_secret', $post_id ); // the stored row, load_value is not applied
    if ( ! $row || $row->field_value === '' || strpos( $row->field_value, 'enc1:' ) === 0 ) {
        return;
    }
    $field_def = FIELDFORGE_Field_Groups::instance()->find_field( 'api_secret', $post_id );

    // update_value() fires fieldforge/value_updated again, so unhook while writing
    remove_action( 'fieldforge/value_updated', 'my_encrypt_api_secret' );
    $values->update_value( 'api_secret', my_encrypt_value( $row->field_value ), $post_id, $field_def ?: [] );
    add_action( 'fieldforge/value_updated', 'my_encrypt_api_secret', 10, 2 );
}
add_action( 'fieldforge/value_updated', 'my_encrypt_api_secret', 10, 2 );

// Decrypt on load
add_filter( 'fieldforge/load_value', function( $value, $field_name ) {
    return $field_name === 'api_secret' ? my_decrypt_value( $value ) : $value;
}, 10, 2 );

If the Sync to PostMeta setting is on, the editor also copies the cleaned, plain-text value to wp_postmeta after this hook has run. Leave that setting off for a field you encrypt. The decrypted value is also what get_field() returns, and Field Forge keeps it in the object cache for up to 300 seconds, which matters on a site with a persistent object cache such as Redis or Memcached.

—

Forge AI Assistant Online

Hi! I'm the Field Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs