Support » Developing with WordPress » Need help displaying data returned from custom widget

  • Please bear with me…I have a convoluted issue that’s not easy to explain…

    I have a very large Multisite installation, and we use an external web application for searching it. I’ve created a widget that is simply an input box/submit button and we use that to search the DB.

    Currently, in the form’s action, it calls a PHP file that sits in the wp-content folder. That’s bugged me for years.

    What I’d like to do is display the results in the inner div of the theme.

    Here is the pertinent code from the widget plugin:

    public function widget( $args, $instance ) {
            global $custom_search_results_page;   //  <---- This is 'https://fqdn.tld/blogs/wp-content/custom_search_page.php
            echo $args['before_widget'];
            echo '<form method="post" class="searchform" action="' . $custom_search_results_page . '" >';
    	// I truncated the rest of the code

    Here’s the basic code structure of the results page

    require_once( '../wp-load.php' );
    $source = "q=source:blogs";
    $term = $_POST['fq'];
    if (isset($_POST['fq'])){
    	$term = $_POST['fq'];
    	$pageNum = 1;
    if (isset($_GET["fq"])) {
    	$term = $_GET["fq"];
    	$start = $_GET['start'];
    	$pageNum = $_GET['pageNum'];
    $term = urlencode($term);
    dhg_return_results( $term, $start, 10, $page_number, $instance );
    //   ^ that function displays the results neat and pretty 

    My question, I guess, is how do I do this the “WordPress way?” It basically works now, but I know it can be done better…just not sure how.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz


    Heya Dan! We don’t see Slack regulars here to often 🙂

    You are correct (naturally), directly requesting PHP files and directly requiring wp-load.php are frowned upon methods, though they can work just fine on specific sites. The problem is they can fail to work when imported to another site that has moved wp-content elsewhere. It seems like an odd thing to do, but there are legitimate reasons and doing so is supported by WP, so our code should accommodate the possibility.

    There’s really only 3 ways to properly request custom code that accommodates moving folders. Ajax and custom page templates are well known and documented. Lesser known and what I would suggest in your situation is sending requests through wp-admin/admin-post.php. It works similarly to Ajax, but no JavaScript/jQuery is required. It’s similar in that it requires an “action” parameter that is used to form a action tag that is fired.

    When you hook that same action, your action callback executes as usual. Like Ajax, the final line should be exit; because your callback constitutes the entire request response. It’s not clear how the external search knows where to send a response. I’m assuming it’s part of the form submit data. Hopefully the needed “action” parameter can be added to that URL.

    Failing the ability to directly specify an action parameter, you could setup an .htaccess rewrite rule that adds in the needed parameter. This rule could even redirect the original request URL to admin-post.php. Also note that even though it’s implied that admin-post.php is expecting a POST request, it works perfectly fine with GET requests because it gets data from the $_REQUEST super-global.

Viewing 1 replies (of 1 total)
  • The topic ‘Need help displaying data returned from custom widget’ is closed to new replies.