0

Hi I am making my own theme in WordPress and setting up some customizer options and I have one for the text in my footer but I would like to still use <? echo date('Y');?> in the footer to dynamically change the date so I don't have to keep going in and doing it manually but the sanitizer call-back obviously blocks it is there a way to still be able use that php function in the sanitizer here is my code what I'm using

customizer.php

$wp_customize-> add_setting('set_copyright', array(
'type' => 'theme_mod',
'default' => "Copyright &copy; <?php echo date('Y'); ?> . All Rights Reserved",
'sanitize_callback' => 'esc_attr'
));

 $wp_customize-> add_control('ctrl_copyright', array(
'label' => 'Copyright Information',
'description' => 'Enter your copyright info',
'section' => 'sec_footer',
'settings' => 'set_copyright',
'type' => 'text',

));

/////

 footer.php

 <footer class="main-footer">
<div class="copy pt-3 pb-3">
    <div class="container">
        <div class="row">
            <div class="col text-center ">
                <p class="small m-0"><?php echo do_shortcode( get_theme_mod( 'set_copyright', "Copyright &copy; <?php echo date('Y'); ?> . All Rights Reserved") ) ; ?></p>
            </div>
        </div>
    </div>
</div>

1 Answer 1

1

This sort of theme mod is only capable of conveying a string into the markup regardless of any sanitization callback - any PHP included within the string will never be interpreted by the PHP engine, short of running the output through eval() which would be extremely dangerous and likely result in the theme failing review for any marketplace.

In this case, since you're running the mod value through shortcodes anyway, I think it would be easiest and provide the best end-user experience to just include a shortcode which outputs the current year, or as a wrapper around the wp_date() function:

function wpse406257_date( $atts, $content ) {
  $atts = shortcode_atts( [
    'format'   => 'F j, Y',
    'timezone' => null,
  ], $atts, 'date' );

  $timestamp = !empty( $content ) ? strtotime( $content ) : null;

  return wp_date( $atts['format'], $timestamp, $atts['timezone'] );
}

add_shortcode( 'date', 'wpse406257_date' );
//...
'default' => 'Copyright &copy; [date format="Y"] . All Rights Reserved'
//...

You could achieve a similar effect without shortcodes by running the string output through str_replace() and swapping out custom placeholders such as {year}.

7
  • thanks for the quick response. I would like to keep the short codes in because they are used for other things like the phone number and address etc would that function be placed in the customizer.php where i have set up the setting and control or can it go in the functions.php file?
    – besty_01
    Commented May 30, 2022 at 22:24
  • functions.php would be fine! customizer.php is a not a core component of a theme so I'm not really sure what effect placing it there might have - if that file's loaded as it is in the default themes, placing it there would have the same effect as placing it in functions.php... but given that the shortcode isn't really customizer-specific, I think functions.php is probably a better home for it.
    – bosco
    Commented May 30, 2022 at 22:29
  • I tried that code and it doesn't seem to be working for me i copied the function exactly its still not showing the year in the footer.
    – besty_01
    Commented May 30, 2022 at 23:04
  • Yikes I might have messed something up! I'll test my code as well when I have some time tonight.
    – bosco
    Commented May 31, 2022 at 0:05
  • @besty_01 You'll need to run apply_shortcodes() on the value when outputting for the shortcode to be printed. Commented May 31, 2022 at 14:52

Your Answer

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

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