Jump to content


Photo

Simple Problem?


  • This topic is locked This topic is locked
1 reply to this topic

#1 mnybud

mnybud
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 30 August 2006 - 03:19 PM

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.co...tf8&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);
?>

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 30 August 2006 - 03:25 PM

Stop double-posting.

If our forum rules are too hard to follow, perhaps you would feel happier elsewhere.
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users