最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

theme customizer - Shortcode don't work in custom option field, any solution?

programmeradmin1浏览0评论

I have a custom field in the theme Customizer that allow to define own copyright information. I created a shortcode displaying the current year. How to make the shortcode work when someone has pasted it into textarea?

I use Kirki framework to create theme options.

Kirki::add_field( 'my_customizer', array(
  'type'     => 'editor',
  'settings' => 'copyright',
  'label'    => __( 'Content', 'mytheme' ),
  'section'  => 'section_footer',
  'default'  => __( 'Copyright ⓒ All rights reserved.', 'mytheme' ),
  'description' => __( 'You can use [current-year] shortcode if you want.', 'mytheme' ),
) );

To display above option I add this to footer.php:

echo Kirki::get_option( 'my_customizer', 'copyright' );

What I see on the site:

Copyright ⓒ [current-year] All rights reserved.
All rights reserved. ⓒ [current-year] by XYZ 
All rights reserved. ⓒ 2016 - [current-year]

What I need to achieve:

Copyright ⓒ 2019 All rights reserved.
All rights reserved. ⓒ 2019 by XYZ 
All rights reserved. ⓒ 2016 - 2019

Any solution / idea? Thanks!

I have a custom field in the theme Customizer that allow to define own copyright information. I created a shortcode displaying the current year. How to make the shortcode work when someone has pasted it into textarea?

I use Kirki framework to create theme options.

Kirki::add_field( 'my_customizer', array(
  'type'     => 'editor',
  'settings' => 'copyright',
  'label'    => __( 'Content', 'mytheme' ),
  'section'  => 'section_footer',
  'default'  => __( 'Copyright ⓒ All rights reserved.', 'mytheme' ),
  'description' => __( 'You can use [current-year] shortcode if you want.', 'mytheme' ),
) );

To display above option I add this to footer.php:

echo Kirki::get_option( 'my_customizer', 'copyright' );

What I see on the site:

Copyright ⓒ [current-year] All rights reserved.
All rights reserved. ⓒ [current-year] by XYZ 
All rights reserved. ⓒ 2016 - [current-year]

What I need to achieve:

Copyright ⓒ 2019 All rights reserved.
All rights reserved. ⓒ 2019 by XYZ 
All rights reserved. ⓒ 2016 - 2019

Any solution / idea? Thanks!

Share Improve this question asked May 31, 2019 at 6:08 AvigoAvigo 377 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

If you have a shortcode string saved, try echoing it with do_shortcode function, which turns shortcodes into content.

$saved_shortcode = get_option( 'my_customizer', 'copyright' );
if ( $saved_shortcode && is_string( $saved_shortcode ) ) {
echo do_shortcode( $saved_shortcode );
}

More about the function on the developer handbook, https://developer.wordpress/reference/functions/do_shortcode/

EDIT

Another option is just echo the year directly on the footer template without first getting the shortcode string from the database and then rendering it.

发布评论

评论列表(0)

  1. 暂无评论