WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Am trying to create a widget that searches for names. The searching and all that is fine, but when I hit submit, I get returned to the front page. I tried messing with the action="", but no matter what I change it to, the widget still redirects to the home page.

Am sure it's simple, I just can't figure out what am doing wrong. Any help is appreciated!


   if( !defined( 'ABSPATH' ) ) { exit;}

   class HNDomainSearchWidget extends WP_Widget 
function HNDomainSearchWidget() 
    $widget_ops = array(
        'classname' => 'HNDomainSearchWidget',
        'description' => 'Search for domains on your site.'
    $this->WP_Widget('HNDomainSearchWidget', 'HobiNom Domain Search', $widget_ops);

// admin form, in the widget page
function form($instance)
    global $wpdb;
    (isset($wpdb->base_prefix) ? $_prefix = $wpdb->base_prefix : $_prefix = $wpdb->prefix); 

$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
$title = $instance['title'];
    // allow for updating the widget name
        <label for="<?php echo $this->get_field_id('title'); ?>">Title: 
        <input class="wide" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" />

function widget($args, $instance) 
    global $wpdb;
    global $post;

    $referrer = $_SERVER['HTTP_REFERER'];

    // widget sidebar output
    extract( $args );
    // these are the widget options
    $title = apply_filters('widget_title', $instance['title']);
    $text = $instance['text'];
    echo $before_widget;

    // Display the widget
    echo '<div class="widget-text wp_widget_plugin_box">';

    // Check if title is set
    if ( $title ) { echo $before_title . $title . $after_title;}

    // the actual search get_permalink( $post->ID ); 
    $pid = (isset($post->ID)) ? get_permalink($post->ID) : '';
    //echo '<form role="search" method="get" id="domainsearch" action="' . esc_url( home_url( '/' ) ) . '?search_for_domain" >';
    echo '<form role="search" method="get" id="domainsearch" action="'. $pid .'" >';
    echo '<input type="text" name="domain" value="Domain.com" onfocus="this.value = \'\';" />';
    echo '<input type="submit" value="' . __( 'Search' ) . '" name="search_domain" /><br />';
    echo '</div></form>';

    $this->search_for_domain($_GET['domain'], get_permalink( $post->ID ));

    echo $after_widget;

// update widget options
function update($new_instance, $old_instance) 
    $instance = $old_instance;
    // Fields
    $instance['title'] = strip_tags($new_instance['title']);
    return $instance;

function deactivate()

// process the widget input and search for domain using
function search_for_domain($search_domain, $permalink)
    $referrer = $_SERVER['HTTP_REFERER'];

        // get details to access
        $hobinom = new hobinom_db(); 

        $current_user_id = get_current_user_id();
        $details = $hobinom->get_details($current_user_id);

        $username = $details['username'];  
        $password = $details['password']; 

        // separate the data from domain and tld
        // explode makes each subsequent . a new array, so domain.co.uk is [0],[1],[2] while domain.co is [0][1]
        $root_domain = explode(".", $search_domain);
        $domain = $root_domain[0]; 
        $tld = substr($search_domain, strrpos($search_domain, ".")+1);

        // access enom api
        $url =  '/interface.asp?command=check&sld='.$domain.'&tld='.$tld.

        // Load the API results into a SimpleXML object
        $xml = simplexml_load_file($url);

            // print all errors
            function recursive_print($item, $key)
                echo '<div id="domain-notavail">'.$item.'</div><br />';
            array_walk_recursive($xml->errors, 'recursive_print');
            // Read the results
            $rrpCode = $xml->RRPCode;
            $rrpText = $xml->RRPText;

            // Perform actions based on results
            switch ($rrpCode) 
                case 210:
                    echo '<div id="domain-avail">'. $xml->DomainName.' is available</div>';
                case 211:
                    echo '<div id="domain-notavail">'.$xml->DomainName.' is not available</div>';
                    echo '<div id="domain-default">Code: '. $rrpCode . ' ' . $rrpText . '</div>';

function hobinom_redirect($permalink) 

 add_action('widgets_init', array($HNDomainSearchWidget, 'hobinom_redirect'));
share|improve this question

A bit of a guess but if the search works but returns you to the home page it sounds like get_permalink( $post->ID ) is not giving you the right values. Look at the page source to confirm. That implies that $post is not set, or not accessible, so put global $post; in your function before you try to use that variable. I'd suggest something like this right at the top of your function:

global $post;
$pid = (isset($post->ID)) ? get_permalink($post->ID) : '';

Then use $pid instead of $post->ID after that.

To the extent that I can test that widget (It calls a class I don't have and the URL to the actual domain search is only partial.) it is operating mostly as expected. This...

  $p = get_queried_object();
  $pid = (isset($p->ID)) ? get_permalink($p->ID) : home_url();

... will get you a little bit better results than the previous block but you are actually redirecting to $referer-- $referrer = $_SERVER['HTTP_REFERER'];-- not to that permalink anyway. In context, it is a bit confusing what you are trying to do at all.

I don't understand the way you have the widget "registered". I got an error until I altered the last few lines to :

function register_HND_widget() {

add_action('widgets_init', 'register_HND_widget');

Also, you should really have debugging enabled. That code generates a fair number of Notices.

share|improve this answer
I changed to just $pid, and the URL becomes: localhost/wordpress/3.5.1/1?search (which is a 404) If I change it to get_permalink($pid), then it again redirects to the front page. – Hybride Apr 29 '13 at 20:57
Was having edit problems. Kept pressing enter for a new line, instead it kept saving while I was still typing. :) – Hybride Apr 29 '13 at 21:00
@Hybride : See the edit. If $post is set correctly and get_permalink is giving you the wrong URL, something is broken. – s_ha_dum Apr 29 '13 at 21:08
Your edit did not work, it sent me back to the homepage. – Hybride Apr 29 '13 at 21:08
Post all of your widget. – s_ha_dum Apr 29 '13 at 21:14

I switched the method="post" and the action="" (rather than get/and a url). For some reason, that fixed it. Am not sure why "get" was having such a hard time, as it still continued to redirect if I switched back the method.

Regardless, thank you to s_ha_dum for trying to help.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.