Advertisement
shaashwato1308

PHP - Wordpress - ALT tag (custom)

May 2nd, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.37 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  
  5. // Create connection
  6. $url = "http://localhost/AUSSIEWEB/phildoring/00102022015/wp-content/uploads/2014/05/insurance.jpg";
  7. // no need to copy
  8. $servername = "localhost";
  9. $username = "root";
  10. $password = "";
  11.  
  12. $conn = mysql_connect("localhost", "root", "");
  13. mysql_select_db("aussieweb_phildoring", $conn);
  14. // Check connection
  15. if($conn){
  16.     echo "Server Connection Success";
  17. }else{
  18.     echo "Server Connection Failed";
  19. }
  20. echo "<br>";
  21. // no need to copy ends
  22.  
  23.  
  24.  
  25.  
  26. $al = alt_text($url);
  27. echo $al;
  28. function alt_text($url){
  29.     $imageUrlRegEx = wp_url_tokenize_sc($url);
  30.     echo "image url regex: ".$imageUrlRegEx."<br>";
  31.  
  32. // Finding out ID
  33.     $id = 0;
  34.     $q = "SELECT ID from pdwp_posts WHERE guid REGEXP '$imageUrlRegEx'";
  35.     $r = mysql_query($q);
  36.     if($r){
  37.         $a = mysql_fetch_array($r);
  38.         $id = $a[0];
  39.         //echo "id: ".$id."<br>";
  40.     }
  41.  
  42.     //echo $id."<br>";
  43.  
  44.         // Finding alt tag
  45.     $alt = "It's ALT tag";
  46.     $q = "SELECT meta_value FROM pdwp_postmeta WHERE meta_key='_wp_attachment_image_alt' AND post_id='$id'";
  47.     $r = mysql_query($q);
  48.     if($r){
  49.         $a = mysql_fetch_array($r);
  50.         //print_r($a);
  51.         $alt = $a[0];
  52.     }else{
  53.         //echo "unsuccessfull";
  54.     }
  55. //echo "alt tag: ".$alt."<br>";
  56.     return $alt;
  57. }
  58.  
  59. function wp_url_tokenize_sc($string){
  60.     $c = 0;
  61.     $collection = array();
  62.     $break = "0";
  63.     $toReturn = "";
  64.     $token = strtok($string, "/");
  65.     while ($token !== false)
  66.     {
  67.         $toReturn = $token;
  68.         $collection[$c] = $toReturn;
  69.         $c++;
  70.         $token = strtok("/");
  71.     }
  72.     $token = strtok($toReturn, ".");
  73.     while ($token !== false && $break=="0")
  74.     {
  75.         $toReturn = $token;
  76.         $break = "1";
  77.     }
  78.     // print_r($collection);
  79.     // echo "<br>";
  80.     $collectionSize = sizeof($collection);
  81.     $collectionSizeStarts = $collectionSize-5;
  82.     $lin = "";
  83.     // echo "collection size: ".$collectionSize."<br>";
  84.     for($cz=0; $cz<5; $cz++){
  85.         if($cz==4){
  86.             $lin = $lin.$collection[$collectionSizeStarts];
  87.         }else{
  88.             $lin = $lin.$collection[$collectionSizeStarts]."\\\\/";
  89.         }
  90.         $collectionSizeStarts++;
  91.     }
  92.     $lin = addBreakerBeforeDot($lin);
  93.     // echo "lin: ".$lin."<br>";
  94.     return $lin;
  95. }
  96. function addBreakerBeforeDot($string){
  97.     $len = strlen($string);
  98.     $toReturn = "";
  99.     for($c = 0; $c < $len; $c++){
  100.         if($string[$c] == "."){
  101.             // echo "found<br>";
  102.             $toReturn = $toReturn."\\\\". $string[$c];
  103.            
  104.         }else{
  105.             // echo "not found<br>";
  106.             $toReturn .= $string[$c];
  107.         }
  108.     }
  109.     return $toReturn;
  110. }
  111. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement