WordPress.org

Ready to get started?Download WordPress

Forums

Image icon instead of the text "Protected" (14 posts)

  1. junipingla
    Member
    Posted 6 years ago #

    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?

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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');
  3. junipingla
    Member
    Posted 6 years ago #

    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?

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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?

  5. junipingla
    Member
    Posted 6 years ago #

    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');
    ?>
  6. junipingla
    Member
    Posted 6 years ago #

    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

  7. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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.

  8. junipingla
    Member
    Posted 6 years ago #

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

  9. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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.

  10. junipingla
    Member
    Posted 6 years ago #

    OK, coming!

  11. junipingla
    Member
    Posted 6 years ago #

  12. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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.

  13. junipingla
    Member
    Posted 6 years ago #

    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! :)

  14. junipingla
    Member
    Posted 6 years ago #

    Works perfectly!

    Much thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.