// Aanmaken widget
class Custom_Widget extends WP_Widget {
function __construct() {
parent::__construct(
'custom_widget', // Base ID
__('Tekst met button', 'text_domain'), // Name
array( 'description' => __( 'Een widget met tekst en een button', 'text_domain' ), ) // Args
);
}
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( !empty($instance['title']) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
}
$tekst = get_field('tekst', 'widget_' . $args['widget_id']);
$button_tekst = get_field('button_tekst', 'widget_' . $args['widget_id']);
$button_link = get_field('button_link', 'widget_' . $args['widget_id']);
echo $tekst;
echo "<a href='". $button_link ."' class='btn'>". $button_tekst ."</a>";
echo $args['after_widget'];
}
public function form( $instance ) {
if ( isset($instance['title']) ) {
$title = $instance['title'];
}
else {
$title = __( 'New title', 'text_domain' );
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
add_action( 'widgets_init', function(){
register_widget( 'Custom_Widget' );
});
// Aanmaken groep voor velden
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array (
'key' => 'group_5b3e1ead3169c',
'title' => 'Button tekst widget',
'fields' => array (
array (
'key' => 'field_5b3e22fc00b1d',
'label' => 'Tekst',
'name' => 'tekst',
'type' => 'wysiwyg',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'tabs' => 'all',
'toolbar' => 'full',
'media_upload' => 1,
'delay' => 0,
),
array (
'key' => 'field_5b3e1ebc23fa7',
'label' => 'Button tekst',
'name' => 'button_tekst',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array (
'key' => 'field_5b3e1ec323fa8',
'label' => 'Button link',
'name' => 'button_link',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
),
'location' => array (
array (
array (
'param' => 'widget',
'operator' => '==',
'value' => 'custom_widget',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
));
endif;
Vervolgens ACF groep aanmaken met de volgende
#acf #php #widgets