Support » Fixing WordPress » add nofollow to links inserted by php functions

  • How do I add rel=”nofollow” to the anchor tags of links inserted by php plugins like:

    the_author_posts_link()
    edit_post_link()
    the_category()
    the_tags()

    Or where can I find these functions in the installation files?

    Thank you many times in advance!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi there I just started using WP and came across the same problem. Your post was the only thing I could find about it here in the support area.

    I just figured out a solution, I hope this helps you.

    <a href="<?php echo get_category_link($cat);?>" rel="nofollow"><?php
    foreach((get_the_category()) as $category) {
        echo $category->cat_name . '';
    }
    ?></a>

    This will return the category of the post and make it a nofollow link with the name of the category as the anchor text.

    On my blog I am not posting posts to multiple categories, only one category per post, so if you are posting to multiple categories this code will need to be tweaked a bit so the anchor text looks nice. Just put a , or a space between the single quotes in this line:

    echo $category->cat_name . '';

    and it should look fine.

    Namaste

    I just found a way better solution. 🙂

    Go to this topic and download the plugin posted by Kafkaesqui.

    To make category links on posts and pages nofollow just add this line to the code:

    add_filter('the_category', 'szub_nofollow_tag');

    Should work with the other ones too.

    desimanifesto

    (@desimanifesto)

    YES! Finally something worked! I tried a number of plugins and nothing achieved success. THANK YOU. I sound overly emphatic, but after three straight weeks of trying to get this to work, it’s a relief.

    I was happy to see this plugin but it doesnt seem to work for me.

    Here is the source of the activated plugin:

    function szub_nofollow_tag($text) {
    	return str_replace('<a href=', '<a rel="nofollow" href=',  $text);
    }
    add_filter('wp_list_cats', 'szub_nofollow_tag');
    add_filter('wp_get_archives', 'szub_nofollow_tag');
    ?>

    Here is how cats and archives are being generated:

    <h3>Kategorien</h3>
    <ul>
      <?php wp_list_cats('show_count=1'); ?>
     </ul>
    </div>
    <h3>Archiv</h3>
    <ul>
    <?php wp_get_archives('type=monthly'); ?>
    </ul>

    … still they are follow.
    Im using WordPress v2.5.1

    any ideas? many thanks!

    This is a modification of megaman’s code which worked for me. Give it a shot.

    <?php foreach((get_the_category()) as $category) { ?>
    <a href="<?php echo get_category_link($cat) . $category->cat_name;?>" rel="nofollow">
    <?php echo $category->cat_name . ''; } ?>
    </a>

    I got a better solution

    Look for wp-includes\category-template.php

    then look for this code
    function get_the_category_list($separator = ”, $parents=”, $post_id = false) {
    global $wp_rewrite;
    $categories = get_the_category($post_id);
    if (empty($categories))
    return apply_filters(‘the_category’, __(‘Uncategorized’), $separator, $parents);

    $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ‘rel=”category tag”‘ : ‘rel=”category”‘;

    then change rel=”category tag to “nofollow”

    @pbo – You should never modify the WordPress core; when WP updates, you will lose your changes.

    Instead, you should either have a plugin do what you want, or use a functions.php file that is in your theme’s folder.

    Although my article on WordPress nofollow functions doesn’t specifically address your question, it shows that you should just copy the function and paste the modified function in your functions.php file. Do NOT change the WP core itself.

    I would recommend making a new nofollow_get_the_category() function using this technique.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘add nofollow to links inserted by php functions’ is closed to new replies.