Skip to content

Fix: WP Widget hooks support closes #5844#5849

Merged
KingYes merged 1 commit intoelementor:release/2.3.0from
bainternet:fix/i-5844
Oct 4, 2018
Merged

Fix: WP Widget hooks support closes #5844#5849
KingYes merged 1 commit intoelementor:release/2.3.0from
bainternet:fix/i-5844

Conversation

@bainternet
Copy link
Copy Markdown
Contributor

PR Checklist

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

Summary

Added native WordPress Widget hooks support

Description

Added native WordPress Widget hooks support

Hooks added are:

  • in_widget_form - to support adding extra widget form fields
  • widget_update_callback - to support saving extra widget form fields

Test instructions

This PR can be tested by following these steps:

  • use the following snippets to add a field and save it.
/*
 * add custom field to WP widget form
 */
add_action('in_widget_form', function ( $widget, $return, $instance ) {
	echo 'This Is visible in Elementor too.';
	$my_custom_field = isset( $instance['my_custom_field'] ) ? $instance['my_custom_field'] : '';
    ?>
	<p>
		<label for="<?php echo $widget->get_field_id( 'my_custom_field' ); ?>"><?php _e( 'My custom field:' ) ?></label>
		<input type="text" class="widefat" id="<?php echo $widget->get_field_id( 'my_custom_field' ); ?>" name="<?php echo $widget->get_field_name( 'my_custom_field' ); ?>" value="<?php echo esc_attr( $my_custom_field ); ?>"/>
	</p>
    <?php
}, 10, 3 );

/**
 * save custom field of WP widget form
 */
add_filter( 'widget_update_callback', function ($instance, $new_instance, $old_instance, $widget ){
	$instance['my_custom_field'] = isset( $new_instance['my_custom_field'] ) ? sanitize_text_field( $new_instance['my_custom_field'] ) : '';
	return $instance;
}, 12, 4 );

Quality assurance

  • I have tested this code to the best of my abilities
  • I have added unittests to verify the code works as intended
  • Docs have been added / updated (for bug fixes / features)

Fixes #5844

@KingYes KingYes changed the base branch from release/2.2.5 to release/2.3.0 October 4, 2018 10:55
@KingYes
Copy link
Copy Markdown
Member

KingYes commented Oct 4, 2018

Please fix your PR.

@KingYes KingYes merged commit ca7a2b6 into elementor:release/2.3.0 Oct 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants