This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.
Each form style will have its own unique class name. When this form class name is added to the form, it will allow control for which styling settings the form uses.
This hook will allow you to select a different style than the one already selected in the form settings.
Usage
add_filter( 'frm_add_form_style_class', 'add_style_class', 1 );
Parameters
- $class (string)
- $args (array): Includes 'form'.
Examples
Replace with RTL style
This example checks the WPML language and uses styling settings for an RTL style instead of the regular style for your form.
add_filter( 'frm_add_form_style_class', 'add_style_class', 1 );
function add_style_class( $class ) {
$lang = apply_filters( 'wpml_current_language', null );
if ( $lang == 'ar' ) { // change ar to your language code
remove_filter( 'frm_add_form_style_class', 'FrmStylesController::get_form_style_class', 10, 2 );
$class .= 'frm_my_style_class frm_rtl'; // replace frm_my_style_class with your rtl style class
}
return $class;
}
Set Style in form shortcode
This example adds a use_style param to the form shortcode. If you'd like to display the form using a different Style than the form's default Style in a particular location, set the use_style param to the name of the different Style.
Use lowercase words with dashes in between, the way the Style appears in the Style class. There's no need to include "frm_style_" at the beginning, as this is added by the code example.
Usage:
Say you've created a Busy Bee Style that you want to use for your form on your home page but not on other pages. On the home page, your form shortcode could look like this:
[formidable id=100 use_style="busy-bee"]
add_filter( 'frm_add_form_style_class', 'frm_add_style_attribute_to_form_shortcode', 1 );
function frm_add_style_attribute_to_form_shortcode( $class ) {
if ( ! isset( $_GET['use_style'] ) ) {
return $class;
}
$new_style = $_GET['use_style'];
$new_style = utf8_decode( urldecode( $new_style ) );
$new_style = esc_html( $new_style );
remove_filter( 'frm_add_form_style_class', 'FrmStylesController::get_form_style_class', 10, 2 );
$class .= ' frm_style_' . $new_style . ' ';
return $class;
}
Add custom class to a form
This code example adds a class "my_custom_class" to form ID 439. $form in this case is an array of form options that includes an array of fields.
add_filter( 'frm_add_form_style_class', 'add_style_class', 10, 3 );
function add_style_class( $class, $style, $args = array() ) {
$target_form_id = 439; // change 439 to your form ID.
if ( ! empty( $args['form'] ) && ! empty( $args['form']['fields'] ) ) {
$field = reset( $args['form']['fields'] );
$form_id = (int) $field['form_id'];
if ( $target_form_id === $form_id ) {
$class .= ' my_custom_class'; // change my_custom_class to your class
}
}
return $class;
}