自动连接-AutoLinker

http://www.huddledmasses.org/ 制作发布。

使用:

  1. if( empty($autolinker_array) ) {  
  2.       $autolinker_array = array(

下照模式添加需要加自动连接的地址。

  1. < ?php  
  2. /*
  3. Plugin Name: AutoLinker  
  4. Version: 1.3  
  5. Plugin URI: http://www.huddledmasses.org/  
  6. Description: Makes links from predefined text  
  7. Author: Joel Bennett  
  8. Author URI: http://www.HuddledMasses.org  
  9. Copyright (c) 2003  
  10. Released under the GPL license  
  11. http://www.gnu.org/licenses/gpl.txt  
  12.     This file is part of WordPress.  
  13.     WordPress is free software; you can redistribute it and/or modify  
  14.     it under the terms of the GNU General Public License as published by  
  15.     the Free Software Foundation; either version 2 of the License, or  
  16.     (at your option) any later version.  
  17.     This program is distributed in the hope that it will be useful,  
  18.     but WITHOUT ANY WARRANTY; without even the implied warranty of  
  19.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
  20.     GNU General Public License for more details.  
  21.     You should have received a copy of the GNU General Public License  
  22.     along with this program; if not, write to the Free Software  
  23.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  
  24. */  
  25.  
  26.  
  27.  
  28.  
  29.  
  30. function autolinker($text) {  
  31.    // First, we define all the things we're going to replace:  
  32.    // and we name it weird so nobody will mistakenly use it  
  33.    // each definition is in the form: // "text" => "url",  
  34.    // ESPECIALLY note that they all end with commas EXCEPT the last one  
  35.    global $autolinker_array;  
  36.  
  37.    if( empty($autolinker_array) ) {  
  38.       $autolinker_array = array(  
  39.          "my site" => "http://www.antonio.hostonnet.com/",  
  40.          "Geo Docs" => "http://docs.GeoShell.org/",  
  41.          "GeoShell" => "http://www.GeoShell.org/",  
  42.          "W3C" => "http://www.w3.org",
  43.           "google" => "http://www.google.com",
  44.         "sohu" => "http://www.sohu.com",
  45.       );  
  46.  
  47.    }  
  48.  
  49.    $text = " $text ";  
  50.    foreach($autolinker_array as $match => $url) {  
  51.    /*    For advanced users, there are several possible regular expressions here....  
  52.    *    The safest, "default" one is at the top ...  
  53.    *    You (or I) may choose to use one of the others!  
  54.    *    Pick whichever you want, and make SURE there is only one that isn't preceded by slashes: //  
  55.    */  
  56.    // DEFAULT  
  57.  
  58.  $text = preg_replace("|([^./]b)$match(b[^:])|msU" , "$1<a href="$url">$match</a>$2" , $text);  
  59.  $text = preg_replace("|(< [A-Za-z]* [^>]*)<a href="$url">$match</a>([^< ]*>)|msU" , "$1$match$2" , $text);  
  60.  
  61.    // SAFER ALTERNATIVE: requires square brackets which will be stripped off matched text  
  62.  
  63.    // SAFER ALTERNATIVE: [GeoShell] would become <a href="http://www.geoshell.org/">GeoShell</a>  
  64.  
  65.    //    $text = preg_replace("|[$match]|msU" , "<a href="$url">$match</a>" , $text);  
  66.  
  67.    }  
  68.  
  69. return trim($text);  
  70.  
  71. }
  72.  
  73. add_filter('the_content', 'autolinker', 7);  
  74.  
  75. add_filter('comment_text', 'autolinker', 7);  
  76.  
  77. ?>

发表评论

电子邮件地址不会被公开。 必填项已用*标注