Support

Account

Home Forums General Issues How to html encode a text field output when printed on page

Solved

How to html encode a text field output when printed on page

  • Hi
    using custom template for in house reporting setup. Still using 4x. Looking at 5-Pro but “almost done” with existing project which got delayed from last year.

    Need help or advice on how to output a text field with HTML encoding to drop in the %20 for blank spaces from something like a book title, perhaps using esc_attr somehow?

    So, if we have a book title like
    This Great Book Title
    in a normal text field.

    e.g., <php the_field('s2p_release_title');>

    normal usage might be
    <a href="http://searchable.zzz/s?q=<?php the_field('s2p_release_title'); ?>&window=realtime&sort=date" target="_blank" rel="nofollow" class="btn btn-primary" role="button">Click for Report</a>

    Problem is for the search to work
    it breaks with ?q=This Great Book Title

    when it needs to print on page in template as
    ?q=This%20Great%20Book%20Title

    I found this solution, but it seems like a lot of natter when presumably the esc_attr or similar built-in to WP might be able to do it?
    http://snippets.khromov.se/sanitizing-and-securing-advanced-custom-fields-output/

    For example something like this, but this is obviously wrong?

    <?php $s2p_release_title = esc_attr( $s2p_release_title ); ?>

    Any advice would be greatly appreciated. This one has me stumped on the obvious.

    THANK YOU!

  • I’d use a simple PHP function

    urlencode();

    echo urlencode(get_field('s2p_release_title'));

  • Thanks @Hube2 (I see you help out a lot here; I do this on a couple of WP theme forums for folks using same themes as me). 🙂

    I will give this a try now since I happen to have the template open right this moment!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘How to html encode a text field output when printed on page’ is closed to new replies.