1

Well here I am again with a problem that I just can't seem to figure out. I have searched and searched, found answers but I just can't seem to get this to work.

I have a theme called WP-Forge and it uses the theme customizer extensively. One of the options available to users is to add or modify the copyright text in the footer. This could be something as simple as them changing the year or company name but more often than not people like to add a link to a site. I keep getting emails about html not outputting the way it should. So I would like to figure this out.

In the theme customizer of WP-Forge I have the following setting and control:

  $wp_customize->add_setting('wpforge_footer_text',array(/* footer text */
    'default' => '',
    'type' => 'theme_mod',
    'transport' => 'postMessage',
    'sanitize_callback' => 'wpforge_sanitize_text',
    'priority' => 20,
  ));
  $wp_customize->add_control('wpforge_footer_text',array(
    'label' => __('Footer Text','wp-forge'),
    'section' => 'footer_content',
    'type' => 'textarea',
  ));

as you can see the sanitize callback is wpforge_sanitize_text. Here is the sanitize function:

  function wpforge_sanitize_text( $input ) {
      return wp_kses_post( force_balance_tags( $input ) );
  }

It works fine if you just enter in text. However, if I add the following:

Copyright © 2015 - <a href="http://somelinkhere.com">SomeLinkHere</a>

I get this in the preview window:

Copyright © 2015 - SomeLinkHere

Everything looks good so I then hit save and publish and look at the home page I see this:

Copyright © 2015 - <a href="http://somelinkhere.com">SomeLinkHere</a>

So what exactly am I missing? Can someone help me get this working the way I want it? Any help is greatly appreciated and I thank you in advance.

3
  • Are you sure the problem is this code and not their input?
    – s_ha_dum
    Jun 2, 2015 at 16:47
  • 1
    I think the problem is, that after you've sanitized the input, the code that displays the link on the front end escapes the HTML again.
    – BdN3504
    Jun 2, 2015 at 16:56
  • @s_ha_dum - as you can see from the example above the simple html I put in shows up the same way on the front end. This is how I call the footer text in the footer <?php echo esc_attr(get_theme_mod( 'wpforge_footer_text')); ?>
    – tsquez
    Jun 2, 2015 at 21:03

1 Answer 1

2

Likely a bit late, Tsquez, but esc_attr is for attributes, e.g. <a title="esc_attr( $title )">. You could also sanitize the HTML in your footer with wp_kses_post instead.

1
  • No worries amigo, can you believe I just figured this out a few weeks ago. I went with <?php echo wp_kses_post(get_theme_mod( 'wpforge_footer_text')); ?> Which works perfectly. Thanks for trying to help.
    – tsquez
    Oct 22, 2015 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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