Skip to content

Instantly share code, notes, and snippets.

@amielucha
Created May 3, 2018 14:10
Show Gist options
  • Select an option

  • Save amielucha/ee1d37da604ffdb1ff5275b568a393c2 to your computer and use it in GitHub Desktop.

Select an option

Save amielucha/ee1d37da604ffdb1ff5275b568a393c2 to your computer and use it in GitHub Desktop.
ACF Pro Widget Custom Fields - WordPress action
<?php
// Replace the values below with the field names configured in ACF.
function cs_dynamic_sidebar_params( $params ) {
// bail early if ACF is not active
if (!function_exists('get_field')) return $params;
// get widget vars
$widget_name = $params[0]['widget_name'];
$widget_id = $params[0]['widget_id'];
// bail early if this widget is not an HTML widget
if( $widget_name != 'Custom HTML' ) return $params;
// add image to before_widget
$image = get_field('widget_image', 'widget_' . $widget_id);
$button = get_field('widget_button', 'widget_' . $widget_id);
if( $button )
$params[0]['after_widget'] = '<a class="btn btn-lg d-block btn-secondary" href="' . $button['page'] . '">' . $button['text'] . '</a>' . $params[0]['after_widget'];
if( $image )
$params[0]['after_widget'] = '<img alt="" src="' . $image['url'] . '">' . $params[0]['after_widget'];
// return
return $params;
}
add_filter('dynamic_sidebar_params', 'cs_dynamic_sidebar_params');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment