How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask Michael Hannigan Your Own Question

Michael Hannigan
Michael Hannigan, Consultant
Category: Programming
Satisfied Customers: 11141
Experience:  25+ Years Experience in Field. Multi Platform Developer.
23511658
Type Your Programming Question Here...
Michael Hannigan is online now
A new question is answered every 9 seconds

How to convert this search.php into a widget on a new theme?

Customer Question

How to convert this search.php into a widget on a new theme?
I'm changing themes from Genesis/Parallax Pro Customized Child theme to Avada/Avada Child theme.
There's a search functionality that searches a local database for zip codes. I'm changing the theme and want to put this functionality into a widget so it can be inserted onto a static page in the new theme.
<?php
/**
* Genesis Framework.
*
* WARNING: This file is part of the core Genesis Framework. DO NOT edit this file under any circumstances.
* Please do all modifications in the form of a child theme.
*
* @package Genesis\Templates
* @author StudioPress
* @license GPL-2.0+
* @link http://my.studiopress.com/themes/genesis/
*/
header('Location: /find-my-zip/?' . getenv('QUERY_STRING'));
exit;
add_action( 'genesis_before_loop', 'genesis_do_search_title' );
/**
* Echo the title with the search term.
*
* @since 1.9.0
*/
function genesis_do_search_title() {
global $wpdb;
$v_zip = $_POST[s];
$v_fname = $_POST[fname];
$v_lname = $_POST[lname];
$v_email = $_POST[email];
$v_isSecond = $_POST[isSecond];
//Convert ZIP code to 5 digit format
$v_zip = intval($v_zip);
for ( $ziplength = strlen($v_zip); $ziplength get_results($wpdb->prepare("SELECT * FROM wp_zipcode where zip=%s", $v_zip));
search_tracking();
if(!$checkZip)
{
echo "
";
echo ""
. "#ziperror { padding-top:20px; display:block !important; }"
. ".site-inner,"
. ".widget-wrap>h4.widget-title.widgettitle { display: none; }"
. "#altformtitle { display: block !important; padding-top: 30px;}"
. "#altformtitle h4.widgettitle:after { content: 'Welcome'; }"
. "";
}
else{
$checkZipReserved = $wpdb->get_results($wpdb->prepare("SELECT * FROM wp_reserved_zip where zip=%s", $v_zip));
$checkIsPremium = $wpdb->get_results($wpdb->prepare("SELECT * FROM wp_premium_zip where zip=%s", $v_zip));
if(!$checkZipReserved && !$checkIsPremium)
{
echo "";
echo ""
. "post('". get_site_url()."/congratulations', {zip: '".$v_zip."', fname: '".$v_fname."', lname: '".$v_lname."', email: '".$v_email."', isSecond: '".$v_isSecond."'});"
. "";
exit();
}
else if($checkZipReserved)
{
echo ""
. ".widget-wrap>h4.widget-title.widgettitle { display: none; }"
. "#formsubtitle { display: none; }"
. "#altformtitle { display: block !important;}"
. "#altformtitle h4.widgettitle:after { content: 'Search for an alternate ZIP code'; }"
. "";
echo "

The ZIP code you requested ".$v_zip." is not available.

";
echo do_shortcode('[gravityform id="3" title="false" description="false" ajax="true" field_values="zipcode='.$v_zip.'"]');
}
else
{
echo "";
echo ""
. "post('". get_site_url()."/premium-zip', {zip: '".$v_zip."', fname: '".$v_fname."', lname: '".$v_lname."', email: '".$v_email."', isSecond: '".$v_isSecond."'});"
. "";
exit();
}
}
}function search_tracking() { ?> jQuery(document).ready(function($) { ga('send', 'event', 'SearchForm', 'Search', ''); }); <?php }genesis();
Submitted: 1 year ago.
Category: Programming
Expert:  Michael Hannigan replied 1 year ago.

Hello. My name is Michael. I can help you with your question.

All you have to do to create a widget, is to put the code into a widget template. Here is an example template:

// Creating the widget
class wpb_widget extends WP_Widget {

function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_widget',

// Widget name will appear in UI
__('WPBeginner Widget', 'wpb_widget_domain'),

// Widget description
array( 'description' => __( 'Sample widget based on WPBeginner Tutorial', 'wpb_widget_domain' ), )
);
}

// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];

// This is where you run the code and display the output
echo __( 'Hello, World!', 'wpb_widget_domain' );
echo $args['after_widget'];
}

// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_widget_domain' );
}
// Widget admin form
?>
<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
}

// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // Class wpb_widget ends here

// Register and load the widget
function wpb_load_widget() {
register_widget( 'wpb_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );

Expert:  Michael Hannigan replied 1 year ago.

You can put it in functions.php or in your site specific widgets file.

Related Programming Questions