Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

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

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 < 5; $ziplength++){
$v_zip = '0' . $v_zip;
}

$checkZip = $wpdb->get_results($wpdb->prepare("SELECT * FROM wp_zipcode where zip=%s", $v_zip));

search_tracking();
if(!$checkZip)
{
echo "<div id='zipNotFound'>&nbsp;</div>";
echo "<style type='text/css'>"
. "#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'; }"
. "</style>";
}
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 "<script type='text/javascript' src='". get_site_url()."/wp-content/themes/parallax-pro-zipcode/js/post.js'></script>";
echo "<script type='text/javascript'>"
. "post('". get_site_url()."/congratulations', {zip: '".$v_zip."', fname: '".$v_fname."', lname: '".$v_lname."', email: '".$v_email."', isSecond: '".$v_isSecond."'});"
. "</script>";
exit();
}
else if($checkZipReserved)
{
echo "<style type='text/css'>"
. ".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'; }"
. "</style>";
echo "<div id='zipNotFound'><h4>The ZIP code you requested <span class='zipnumber'>".$v_zip."</span> is not available.</h4>";
echo do_shortcode('[gravityform id="3" title="false" description="false" ajax="true" field_values="zipcode='.$v_zip.'"]');
}
else
{
echo "<script type='text/javascript' src='". get_site_url()."/wp-content/themes/parallax-pro-zipcode/js/post.js'></script>";
echo "<script type='text/javascript'>"
. "post('". get_site_url()."/premium-zip', {zip: '".$v_zip."', fname: '".$v_fname."', lname: '".$v_lname."', email: '".$v_email."', isSecond: '".$v_isSecond."'});"
. "</script>";
exit();
}
}
}function search_tracking() { ?> <script type="text/javascript"> jQuery(document).ready(function($) { ga('send', 'event', 'SearchForm', 'Search', '<?php echo $_POST[s]; ?>'); }); </script><?php }genesis();

Answers (2)

2015-09-17

Arnav Joy answers:

Hi ,

This will not quick to extract code and create it as widget to be used in another project.

I will need access to check how it is implemented ..

so can you add me on skype : arnav.joy

2015-09-17

dimadin answers:

You don't mention that you need your form converted so I just placed these function for results that you shared inside widget's result. I haven't modified your code, but notice that can be very improved.



/**
* Adds MD_Search_Title_Widget widget.
*/
class MD_Search_Title_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'md_search_title_widget',
__( 'Search Term Title', 'text_domain' ),
array( 'description' => __( 'Widget for title with the search term', 'text_domain' ), )
);
}

/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
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'];
}

$this->do_search_title();

echo $args['after_widget'];
}

/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['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
}

/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';

return $instance;
}

/**
* Echo the title with the search term.
*
* @since 1.9.0
*/
public function 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 < 5; $ziplength++){
$v_zip = '0' . $v_zip;
}

$checkZip = $wpdb->get_results($wpdb->prepare("SELECT * FROM wp_zipcode where zip=%s", $v_zip));

$this->search_tracking();

if(!$checkZip)
{
echo "<div id='zipNotFound'>&nbsp;</div>";
echo "<style type='text/css'>"
. "#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'; }"
. "</style>";
}
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 "<script type='text/javascript' src='". get_site_url()."/wp-content/themes/parallax-pro-zipcode/js/post.js'></script>";
echo "<script type='text/javascript'>"
. "post('". get_site_url()."/congratulations', {zip: '".$v_zip."', fname: '".$v_fname."', lname: '".$v_lname."', email: '".$v_email."', isSecond: '".$v_isSecond."'});"
. "</script>";
exit();
}
else if($checkZipReserved)
{
echo "<style type='text/css'>"
. ".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'; }"
. "</style>";
echo "<div id='zipNotFound'><h4>The ZIP code you requested <span class='zipnumber'>".$v_zip."</span> is not available.</h4>";
echo do_shortcode('[gravityform id="3" title="false" description="false" ajax="true" field_values="zipcode='.$v_zip.'"]');
}
else
{
echo "<script type='text/javascript' src='". get_site_url()."/wp-content/themes/parallax-pro-zipcode/js/post.js'></script>";
echo "<script type='text/javascript'>"
. "post('". get_site_url()."/premium-zip', {zip: '".$v_zip."', fname: '".$v_fname."', lname: '".$v_lname."', email: '".$v_email."', isSecond: '".$v_isSecond."'});"
. "</script>";
exit();
}
}
}

public function search_tracking() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
ga('send', 'event', 'SearchForm', 'Search', '<?php echo $_POST[s]; ?>');
});
</script>
<?php
}
}

/**
* Register MD_Search_Title_Widget widget.
*/
function register_md_search_title_widget() {
register_widget( 'MD_Search_Title_Widget' );
}
add_action( 'widgets_init', 'register_md_search_title_widget' );