Jump to content

Recommended Posts

I have this Wordpress plugin code (shown below) that pulls definitions from Google. I just call it in my wordpress posts as ~GetDEF(Your_Search_Term)~ and it works great as long as Google has a definition for the term inputed. If Google does not have a definition then it just prints ~GetDEF(Your_Search_Term)~ instead which is kind of ugly.

I am wondering is there a way to make it so it displays nothing if the term is not found?

I dont know much about PHP so if anyone can help please tell me what I should use to stop this and where I should put it in the code. If you need more info please let me know. Thanks in advance!!

Here is the plugin code, its just one php file:

//Server Configuration
$host = "www.google.com";
$searchStr="http://www.google.com/search?ie=utf8&oe=utf8&q=define:";

function getDEF( $def ) {
    global $host,$searchStr,$path;
     
    $fp = fopen ($searchStr.$def, "r");
  if (!$fp) {
      exit;
  }
  $contents ="";
  $article="";
  while (!feof ($fp)) {
      $contents .= fread($fp, 8192);
  }
  if (eregi ("<ul type=\"disc\">(.*)</ul>", $contents, $out)) {
      $article = $out[1];
  }
if (eregi ("<li>(.*)<li>", $contents, $out)) {
      $article = $out[1];
  }
$article=eregi_replace("<a(.*)[/url]","",$article);
$article=strip_tags($article);

fclose($fp);
$article=trim($article);
if($article!=""){
$description=  "</br><span class=\"gdescription\">
This definition of ".$def.' is provided by the best search engine in the world, Google!</span>';
$article = "<div class=\"Gdef\">".$def.":
".$article.$description."</div>";
}
return $article;
}

function def_fy( $text ) {
    $text = preg_replace(
        "#\~GetDEF\((\S*)\)\~#imseU",
        "getDEF('$1')",
        $text
    );

return $text;
}

function def_css() {
    echo "
    <style type='text/css'>
    div.Gdef {
        border: 1px dashed silver;
        background-color: #f0f0f0;
  padding:0 2em 0 2em;
    }
    .gdescription {
        font-size: 80%;
      padding:0 2em 0 2em;
    }
    </style>
    ";
}


add_action('wp_head', 'def_css');
add_filter('the_content', 'def_fy', 2);
add_filter('the_excerpt', 'def_fy', 2);
?>
Link to comment
https://forums.phpfreaks.com/topic/19161-simple-fix/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.