Support » Themes and Templates » Image icon instead of the text “Protected”

  • I would like to display an image of a lock instead of the text “Protected:” in the title of protected posts.

    Does anyone know how to modify I guess the post-template.php?

Viewing 13 replies - 1 through 13 (of 13 total)
  • junipingla: I wouldn’t modify the core files. I’d use a filter.

    In your theme’s functions.php file, add something like this:

    function change_protected_to_image($title)
    {
    return str_replace('Protected:','<img src="http://example.com/path/to/lock.png" alt="Protected">', $title);
    }
    add_filter('the_title','change_protected_to_image');

    Otto42: Thank you so much for your reply!

    I added this in my funtions.php

    function change_protected_to_image($title)
    {
    return str_replace('Protected:','<img src="http://my-url-and-theme/images/lock.gif" alt="Protected">', $title);
    }
    add_filter('the_title','change_protected_to_image');

    however it still says “Protected: Title

    Any idea?

    a) The theme’s functions.php. The spelling of the file is important here. Also the file must start with <?php and end with ?>

    b) Can you give us a link to the resulting page, so we can see it in action?

    Yes, it’s the functions.php located in my themes folder.
    I misspelled the file in my last post =)

    This is the entire code in that file

    <?php
    if ( function_exists('register_sidebar') )
        register_sidebar(array(
            'before_widget' => '<li id="%1$s" class="widget %2$s">',
            'after_widget' => '</li>',
            'before_title' => '<h2 class="sidebartitle">',
            'after_title' => '</h2>',
        ));
    
    function change_protected_to_image($title)
    {
    return str_replace('Protected:','<img src="http://my-url-and-theme/images/lock.gif" alt="Protected">', $title);
    }
    add_filter('the_title','change_protected_to_image');
    ?>

    I have a swedish language file that translate Protected to Skyddad.
    When replacing “Protected” with skyddad in
    return str_replace('Protected:'

    I do see the image, however it looks weird:

    This is a locked entry”>**the image here** This is a locked entry

    junipingla: Yes, the language would be the trick.

    But I can’t help you any more without seeing the actual result. Give a link to the page so we can see it.

    I have the site under htaccess passwrd protection so here’s a printscreen:
    http://fullthus.se/image.jpg

    Not good enough. I need to see the HTML source code of the resulting page.

    If you prefer, you can paste the html here: http://wordpress.pastebin.ca

    and then paste the resulting link back here so I can see it.

    OK, coming!

    Ahh… I see the problem, the_title() is being used in your permalink as well.

    That common usage was one reason that some version of WordPress introduced the_title_attribute(). It produces a clean version of the title, suitable for using in an attribute.

    You’ve got code in your theme that looks pretty similar to this:

    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>

    Change it to this:
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>

    The difference is using the_title_attribute instead of the_title inside the link’s title attribute. Note that this is how the default theme does it as well.

    You may need to change more than one theme file. Probably index.php and single.php, at minimum. Look for any other uses of “the_title” in your theme and adjust them accordingly.

    I will! I’ll let you know later if I got it working!

    Thank you so much for being so kind to help me out! 🙂

    Works perfectly!

    Much thanks!

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Image icon instead of the text “Protected”’ is closed to new replies.