Description
This filter is used to change the values for Conductor Widget instances (settings) on the front-end.
Note: To view a list of Conductor Widget instance (settings) keys/values, see conductor_widget_defaults
.
This filter is executed when the Conductor_Widget::widget()
function is called (when a Conductor Widget is displayed).
Technical Details
Type: Filter
Parameters:
$instance (array): Conductor Widget instance (settings)
$args (array): Conductor widget arguments (before_widget, after_widget, before_title, after_title)
$widget (Conductor_Widget): Conductor_Widget instance
Return Value: array
Resources
- View on GitHub
Class: Conductor_Widget
conductor_widget_defaults
conductor_widget_update
conductor_widget_instance
conductor_widget_admin_localize
conductor_widget_query_callback_prefix
Examples
The following example demonstrates how to add a custom setting to Conductor Widget instances based on a CSS class.
Note: This example is nearly identical to the conductor_widget_update
example. The difference between the two is that conductor_widget_instance
is only executed on the front-end at run-time and conductor_widget_update
is executed in the admin upon updating/saving of a Conductor Widget (stores data in the database).
<?php | |
/** | |
* This function adds a custom setting to Conductor Widget instances based on a CSS class. | |
*/ | |
add_filter( 'conductor_widget_instance', 'my_conductor_widget_instance', 10, 3 ); | |
function my_conductor_widget_instance( $instance, $args, $widget ) { | |
// Bail if the 'my-custom-css-class' doesn't exist in the CSS class value | |
if ( strpos( $instance['css_class'], 'my-custom-css-class' ) === false ) | |
return $instance; | |
// Add 'my-custom-setting' to the Conductor Widget instance | |
if ( ! isset( $instance['my-custom-setting'] ) ) | |
$instance['my-custom-setting'] = true; | |
return $instance; | |
} |
The following example demonstrates how to create a custom callback function for a single output element (a custom field).
<?php | |
/** | |
* These functions will adjust the callback function for a custom field named "my_custom_field" on Conductor Widgets. | |
* Please customize the following code to fit your needs. | |
*/ | |
/** | |
* This function adjusts the the callback function on the "my_custom_field" custom field for a Conductor Widget instance (settings). | |
*/ | |
add_filter( 'conductor_widget_instance', 'my_conductor_widget_instance', 20, 3 ); | |
function my_conductor_widget_instance( $instance, $args, $widget ) { | |
// Adjust the callback on the my_custom_field custom field | |
foreach ( $instance['output'] as $priority => &$element ) | |
if ( $element['type'] === 'custom_field' && $element['id'] === 'my_custom_field' ) | |
// Adjust callback | |
$element['callback'] = 'my_custom_field_callback'; | |
return $instance; | |
} | |
/** | |
* This function is the callback function for the "my_custom_field" custom field used on Conductor Widget output. This callback | |
* is passed 4 parameters, the current $post object, the widget $instance (settings), the $widget instance (reference to the Conductor_Widget class), | |
* and the $query used in the Conductor Widget. | |
* | |
* Currently, the logic below matches the Conductor Custom Fields Add-On but you can adjust the logic to match your specific needs. | |
*/ | |
function my_custom_field_callback( $post, $instance, $widget, $query ) { | |
$custom_field_object = array(); | |
// Find the custom field object | |
foreach ( $instance['output'] as $priority => $element ) | |
// If this output element matches "my_custom_field" | |
if ( $element['type'] === 'custom_field' && $element['id'] === 'my_custom_field' ) { | |
// Store a reference to this output element | |
$custom_field_object = $element; | |
break; | |
} | |
// Fetch the custom field value | |
$custom_field = get_post_meta( $post->ID, $custom_field_object['id'], true ); | |
do_action( 'conductor_widget_custom_field_before', $post, $instance, $custom_field_object ); | |
?> | |
<p class="custom-field custom-field-<?php echo esc_attr( $custom_field_object['id'] ); ?>"> | |
<?php if ( $custom_field_object['label'] !== $custom_field_object['id'] ) : // Custom Field Label ?> | |
<span class="custom-field-label"><?php echo $custom_field_object['label']; ?></span> | |
<?php endif; ?> | |
<?php if ( ! empty( $custom_field ) ) : // Custom Field Value ?> | |
<span class="custom-field-val"><?php echo ( is_array( $custom_field ) ) ? implode( ', ', $custom_field ) : $custom_field; ?></span> | |
<?php endif; ?> | |
</p> | |
<?php | |
do_action( 'conductor_widget_custom_field_after', $post, $instance, $custom_field_object ); | |
} |