• 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)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    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="https://example.com/path/to/lock.png" alt="Protected">', $title);
    }
    add_filter('the_title','change_protected_to_image');
    Thread Starter junipingla

    (@junipingla)

    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="https://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?

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    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?

    Thread Starter junipingla

    (@junipingla)

    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="https://my-url-and-theme/images/lock.gif" alt="Protected">', $title);
    }
    add_filter('the_title','change_protected_to_image');
    ?>
    Thread Starter junipingla

    (@junipingla)

    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

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    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.

    Thread Starter junipingla

    (@junipingla)

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

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

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

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

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

    Thread Starter junipingla

    (@junipingla)

    OK, coming!

    Thread Starter junipingla

    (@junipingla)

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    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.

    Thread Starter junipingla

    (@junipingla)

    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! ??

    Thread Starter junipingla

    (@junipingla)

    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.