grasesed
grasesed

Reputation: 925

PHP Encode string as HTML

I have this code below, which comes from Wordpress Core file /wp-admin/includes/class-wp-posts-list-table.php

  printf(
    '<a class="row-title" href="%s" aria-label="%s">%s%s</a>',
    get_edit_post_link( $post->ID ),
    /* translators: %s: post title */
    esc_attr( sprintf( __( '&#8220;%s&#8221; (Edit)' ), $title )),
    $pad,
    $title
  );

In my case the variable $title contains iconfont HTML i.e.

  <i class="fa fa-heart"></i> 

enter image description here

The PHP code is making the web browser display the HTML characters as a string rather than what I'm after which is to display as HTML and render the Font Awesome Icon.

Ive tried wrapping $title in

htmlentities()
html_entity_decode()
htmlspecialchars()

Can some one help thanx

Upvotes: 0

Views: 3322

Answers (1)

Slava
Slava

Reputation: 938

Where did you use html_entity_decode? I've tried this:

printf(
    '<a class="row-title" href="%s" aria-label="%s">%s%s</a>',
    get_edit_post_link( $post->ID ),
    /* translators: %s: post title */
    esc_attr( sprintf( __( '&#8220;%s&#8221; (Edit)' ), $title )),
    $pad,
    html_entity_decode($title)
  );

and seems it works.

But this is BAD idea to change core file. You can try to write (or find) some plugin that allows to add icons to particular post title but not to all post and without changing original file.

Upvotes: 3

Related Questions