Contact Form 7 Plugin Select Field

Example:

  1. <?php
  2. class CF7_Fields_Widget extends WP_Widget {
  3. function __construct() {
  4. // Instantiate the parent object
  5. $widget_ops = array(
  6. 'classname' => 'widget_title_fields_entries',
  7. 'description' => esc_attr__('CF7 Field Widget - Created by deshisoft', 'eye-theme')
  8. );
  9. $control_ops = array(
  10. 'width' => 275
  11. );
  12. parent::__construct( false, 'CF7 Field Widget', $widget_ops, $control_ops );
  13. }
  14. function widget( $args, $instance ) {
  15. extract( $args );
  16. // Widget output
  17. $title = apply_filters('widget_title', $instance['title']);
  18. // Contact Form 7
  19. $wpcf7 = apply_filters('widget_wpcf7', $instance['wpcf7']);
  20. echo $before_widget;
  21. echo '<div style="border:1px solid #ddd; background:#f6f6f6;padding:15px;">';
  22. echo esc_attr($title);
  23. echo do_shortcode($wpcf7);
  24. echo '</div>';
  25. echo $after_widget;
  26. }
  27. function update( $new_instance, $old_instance ) {
  28. // Save widget options
  29. $instance = array();
  30. $instance = $old_instance;
  31. $instance['title'] = strip_tags( $new_instance['title'] );
  32. $instance['wpcf7'] = strip_tags( $new_instance['wpcf7'] );
  33. return $instance;
  34. }
  35. function form( $instance ) {
  36. // Output admin widget options form
  37. $title = $instance['title'];
  38. $wpcf7 = esc_attr($instance['wpcf7']); ?>
  39. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  40. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
  41. <!-- Contact Form7 -->
  42. <?php
  43. $option_query = new WP_Query(array(
  44. 'orderby' => 'name',
  45. 'order' => 'ASC',
  46. 'post_type' => 'wpcf7_contact_form',
  47. 'posts_per_page' => -1
  48. ));
  49. ?>
  50. <p><label><?php _e('Leave a Message Form: <br/><em style="font-size:12px; color:#0073aa;">Make sure the <a target="_blank" href="https://wordpress.org/plugins/contact-form-7/">contact form 7</a> plugin is install.</em><br/>'); ?></label><br/>
  51. <select id="<?php echo $this->get_field_id('wpcf7'); ?>" name="<?php echo $this->get_field_name('wpcf7'); ?>" >
  52. <option value="" <?php echo ($instance['wpcf7'] === '' ? ' selected="selected"' : ''); ?>>Select Form</option>
  53. <?php $forms = array();
  54. if ($option_query->have_posts()) :
  55. while ($option_query->have_posts() ) : $option_query->the_post(); ?>
  56. <option value='

    Error: Contact form not found.

    '
  57. <?php echo ($instance['wpcf7'] === '

    Error: Contact form not found.

    ' ? ' selected="selected"' : ''); ?>><?php echo $forms[get_the_ID()] = get_the_title(); ?></option>
  58. <?php
  59. endwhile;
  60. endif;
  61. wp_reset_query(); ?>
  62. </select></p>
  63. <?php
  64. }
  65. }
  66. function cf7_field_register_widgets() {
  67. register_widget( 'CF7_Fields_Widget' );
  68. }
  69. add_action( 'widgets_init', 'cf7_field_register_widgets' );
  70. ?>

Theme Folder -> function.php
Add this line:

  1. require get_template_directory() . '/inc/contact_form7_widget.php'; // inc folder name depend on your theme.

Download Contact Form 7 Widget Field