0

I have tried best to create tag Plugin, but am stock here. My goal is that I want to Exclude tag links from html tags like and so on. I have think of many idea but it never seem to work. it should just skip this those html part. Please I need assistance Please.

$replace_times=3;
add_action('admin_menu','tag2link_setting_options');
add_filter('the_content','tag2link');
init_tag2link();
$tags;
function init_tag2link(){
    add_option('tag2link_times');
    add_option('tag2link_use');
}
function getPostTags(){
global $post_ID;
}
function tag2Link($s){
    global $id;
    global $replace_times;
    global $wp_rewrite;
    $te=get_option('tag2link_use');
    if($te=='te')   $useTe=true;
    else $useTe=false;
    $us=get_option('tag2link_times');
    $replace_times=intval($us);
    if($replace_times==0) $replace_times=3;
    /*
        Get tag permalink structure
    */
    //$structure=$this->get_tag_permastruct();
    $ss=$wp_rewrite->get_tag_permastruct();
    $structure="";
    if($ss==false) {
        $structure=get_option('siteurl').'/?tag=%tag%';
    }else $structure=get_option('siteurl').$ss;
    /*
        Get post tags
    */
    $tags=wp_get_post_tags($id);
    $p=$s;
    if($tags==null) return $p;
    /*
        Start replace
    */
    $count=count($tags);
    usort($tags,cmp);
    $temp=$structure;
    foreach($tags as $value){

        if($useTe){
            $pattern='/(?<=[^a-zA-Z])'.$value->name.'(?!.*<\/a>)/';
            $replace='<a href="http://localhost/wordpress/tag/'.$value->slug.'">'.$value->name.'</a>';
            $p=preg_replace($pattern,$replace,$p,$replace_times);
        }
        else{
                $structure=str_replace('%tag%',$value->slug,$temp);
            if($ss==false){
                $pattern='/(?<=[^a-zA-Z])'.$value->name.'(?!.*<\/a>)/';
                //$pattern='/(?<!\/\?)(?<!\w)'.$value->name.'(?!\w)(?!(\s|\w)*<\/a>)/';
            }else{
                //$pattern='/(?<!\/)(?<!\w)'.$value->name.'(?!\w)(?!(\s|\w)*<\/a>)/';
                $pattern='/(?<=[^a-zA-Z])'.$value->name.'(?!.*<\/a>)/';
            }
            $tag_permalink = get_tag_link($value->term_id);
            $replace='<a href="'. $tag_permalink .'">'.$value->name.'</a>';
            $p=preg_replace($pattern,$replace,$p,$replace_times);
        }
     }      
    return $p;
}
function cmp($a,$b){
    return strlen($a->name)-strlen($b->name);
}
function tag2link_setting_options(){
        add_options_page('Tag to Links', 'Tag to Links', 5, 'automatic-tag-link/options.php');

Looking at the code I don't want the tag link to be in all h2, h2, h1, h4, html text. including , tag etc. Please I really need this Please. I have tried my best but no way out.

3
  • Have you tried wp_strip_all_tags()? codex.wordpress.org/Function_Reference/wp_strip_all_tags
    – WebElaine
    Commented Jun 22, 2018 at 21:34
  • please include it in the code above, I am a novice and just learning wp coding. help me fix it please. Commented Jun 22, 2018 at 21:39
  • 1
    Sorry, we're here to provide ideas but not do all the coding for you. :) Generally speaking my suggestion is to get rid of your calls to preg_replace() and use wp_strip_all_tags() instead to strip the HTML off of whatever text you are trying to grab.
    – WebElaine
    Commented Jun 22, 2018 at 21:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.