0

Someone asked me to create a solution to use HTML anchors () in the WP blog tagline. E.g. like:

Just another <a href="http://www.wordpress.org">WordPress</a> website

I came up with a small plugin where the user can use pseudo-code in the tagline, which is converted by the plugin to HTML anchors. Something like

Just another [[http://www.wordpress.org]]WordPress[[/]] website

This works fine, however WordPress keeps encoding the HTML to entities, and I have no clue how to resolve or prevent this.

My best guess is that some other filter hook fires after my plugin, and maybe I need to use some other hook(?) I tried option_blogdescription and pre_option_blogdescription and both have the same effect. However, this is just a wild guess and even then, I don't know whch hook to use then.

Also I tried diffent ways of getting the tagline content (To prevent that it already arrived as entities) I tried to receive it directly from the hook parameter, getting with get_option, getting with $wpdb->get_results but all with the same result.

And I tried several things to decode it like html_entity_decode() and wp_kses(). Again, all with the same result.

But if everything is encoded after my plugin comes in, none of my effort will have any effect.

I have more versions. Two of the latest:

The Hook add_filter( 'option_blogdescription', 'add_html_blog_description' );

Function 1

function add_html_blog_description( $option_value ) {
    $url = 'http://www.wordpress.org';
    $tagopen = '[[';
    $tagclose = ']]';
    $option_value = str_replace($tagopen, '<a href="' . $url .' ">', $option_value);
    $option_value = str_replace($tagclose, '</a>', $option_value);
    return $option_value;
} 

function 2

function add_html_blog_description( $option_value ) {
    global $wpdb;
    $allowed_html = array(
        'a' => array(
            'href' => array(),
            'title' => array()
        )
    );
    $results = $wpdb->get_results( "SELECT `option_value` FROM `wdwp_options` WHERE `option_name` = 'blogdescription'", ARRAY_A );
    $new_tagline = $results[0]['option_value'];
    $new_tagline =  html_entity_decode (wp_kses( $new_tagline, $allowed_html ) );
    return $new_tagline;
}
  • Please, show us your code. – cybmeta Nov 4 '17 at 9:13
  • The blog description is used in other places, like the title element and by some plugins in meta elements. HTML is not appropriate in these places, so the HTML is encoded. Allowing HTML here would mess up these elements. If you have need for outputting a description with HTML, I suggest creating a new field. – Jacob Peattie Nov 4 '17 at 12:18
  • Ah, that makes sense. My first approach is always to use the default WP option to achieve something, to leave the original functionality as intact as possible. In this case the blog description option, and filter that. However in this case it indeed sounds better to use a new option. You're right, I didn't look at it that way. I didn't want to touch the theme, so I wanted my plugin to be 'theme independent'. With a new option the theme needs to be adjusted to implement the new option. So maybe it's better to not build it as a plugin, but as a theme option in functions.php. – Gooly Nov 4 '17 at 15:11
0

With all credits and many thanks to Jacob Peattie in the comment above. After Jacob's remark that the title is used for e.g. the title element and by some plugins, I followed his advice to create a new field because he made me realize that the front page tagline needed somehow to be seperated from the other places where the description is being used.

So I created a custom field in the WP settings, a function to (only) allow HTML for HTML anchors (All other code keeps decoded to entities) and a function to parse the alternative tagline.

Then the new function needed to be implemented in the theme, so I created a child theme with the new alternative tagline in header.php

So not a simple plugin as I hoped for, but a child theme with all good reasons :) And it works like a charm. Thanks Jacob!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.