小部件重定向到主页

时间:2013-04-29 作者:Hybride

我正在尝试创建一个搜索名称的小部件。搜索和其他一切都很好,但当我点击提交时,我又回到了头版。我试图搞乱action=”,但无论我将其更改为什么,小部件仍会重定向到主页。

我确信这很简单,我只是不知道我做错了什么。感谢您的帮助!

    <?php
    require_once(\'include/functions.php\'); 

   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
    ?>
   <p>
        <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); ?>" />
    </label></p>
    <?php
    }

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()
{
     delete_option(\'HNDomainSearchWidget\');
    }


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

    if(isset($search_domain))
    {
        // 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.
                \'&responsetype=xml&uid=\'.$username.\'&w=\'.$password;

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

        if(isset($xml->errors)) 
        {
            // print all errors
            function recursive_print($item, $key)
            {
                echo \'<div id="domain-notavail">\'.$item.\'</div><br />\';
            }
            array_walk_recursive($xml->errors, \'recursive_print\');
        }
        else
        {
            // 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>\';
                    break;
                case 211:
                    echo \'<div id="domain-notavail">\'.$xml->DomainName.\' is not available</div>\';
                    break;
                default:
                    echo \'<div id="domain-default">Code: \'. $rrpCode . \' \' . $rrpText . \'</div>\';
                    break;
            }
        }       
        //$this->hobinom_redirect($referrer);
    }
}

function hobinom_redirect($permalink) 
{
    wp_redirect($permalink);
}
    }

 add_action(\'widgets_init\', array($HNDomainSearchWidget, \'hobinom_redirect\'));
?>

2 个回复
SO网友:s_ha_dum

有点猜测,但如果搜索成功,但返回到主页,听起来像get_permalink( $post->ID ) 没有为您提供正确的值。查看页面源以确认。这意味着$post 未设置或不可访问,因此放置global $post; 在您尝试使用该变量之前。我建议您在函数的顶部执行以下操作:

global $post;
$pid = (isset($post->ID)) ? get_permalink($post->ID) : \'\';
然后使用$pid 而不是$post->ID 在那之后。

我可以测试这个小部件(它调用一个我没有的类,而实际域搜索的URL只是部分的。)它的运行基本符合预期。这

  $p = get_queried_object();
  $pid = (isset($p->ID)) ? get_permalink($p->ID) : home_url();
。。。将获得比前一个块更好的结果,但实际上您正在重定向到$referer-- $referrer = $_SERVER[\'HTTP_REFERER\'];-- 不管怎么说,都不是为了那个永久性的。在上下文中,您到底想做什么有点让人困惑。

我不明白你是如何“注册”小部件的。我犯了一个错误,直到我将最后几行改为:

function register_HND_widget() {
  register_widget(\'HNDomainSearchWidget\');
}

add_action(\'widgets_init\', \'register_HND_widget\');
还有,你真的应该debugging enabled. 该代码生成了相当数量的Notices

SO网友:Hybride

我切换了方法=“post”和操作=”(而不是获取/和url)。出于某种原因,这解决了它。我不知道为什么“get”会遇到如此困难,因为如果我切换回该方法,它仍然会继续重定向。

无论如何,感谢s\\u ha\\u dum的帮助。

结束