Thursday, March 1, 2018

How to find anchor tag from content and replace it and remove anchor tag from images in wordpress | Wordpress.

<?php 
    $phrase = get_the_content();
    // This is where wordpress filters the content text and adds paragraphs
    $phrase = apply_filters('the_content', $phrase);
   preg_match_all('/href="(http:\/\/[^\/"]+\/?)?([^"]*)"*/',$phrase, $matches);
   //echo '<pre>';
   //print_r($matches[2]);
    $i=0;
   $string="";
   foreach($matches[2] as $val){
    $value =substr($val,strpos($val,'/')+1);
     $slug=rtrim($value, "/");
     $getpostid = $wpdb->get_results( "SELECT ID FROM wp_posts
                       WHERE post_status= 'publish' AND post_name like '%$slug%'" );
                  $id=$getpostid[0]->ID;
 $ids[]="href=http://blog.saginfotech.com/app-single-post?id=".$id;
 }
 $newphrase = str_replace($matches[0],$ids, $phrase);
$content =str_replace('"','',$newphrase); 
  $content =
         preg_replace(array('{<a[^>]*><img}','{/></a>}'), array('<img','/>'), $content);
    echo  $content;
 ?>

No comments:

Post a Comment