09 短代码与内容组件

整理短代码属性、包裹内容、输出缓冲、嵌套短代码和资源按需加载。

09 短代码与内容组件

本页关键词

add_shortcode shortcode_atts do_shortcode ob_start has_shortcode

学习目标

代码使用提醒

本页代码适合用于学习和研究。复制到正式网站前,请先备份,并优先在测试环境验证。

涉及用户输入、后台保存、接口请求、删除操作和邮件发送时,要同时考虑权限、nonce、sanitize、validate 和 escape。

1. 基础短代码

基础
<?php function mysite_button_shortcode( $atts ) { $atts = shortcode_atts( array( 'text' => 'Learn More', 'url' => '#', ), $atts, 'mysite_button' ); return '<a class="btn" href="' . esc_url( $atts['url'] ) . '">' . esc_html( $atts['text'] ) . '</a>'; } add_shortcode( 'mysite_button', 'mysite_button_shortcode' );
短代码回调必须 return,不建议直接 echo。

2. 包裹内容短代码

基础
<?php function mysite_notice_shortcode( $atts, $content = '' ) { $atts = shortcode_atts( array( 'type' => 'info', ), $atts, 'mysite_notice' ); return '<div class="notice notice-' . esc_attr( $atts['type'] ) . '">' . wp_kses_post( do_shortcode( $content ) ) . '</div>'; } add_shortcode( 'mysite_notice', 'mysite_notice_shortcode' ); // 用法:[mysite_notice type="warning"]内容[/mysite_notice]
包裹内容要处理 do_shortcode,允许内部短代码继续执行。

3. 用 ob_start 写复杂 HTML

实用
<?php function mysite_contact_card_shortcode( $atts ) { $atts = shortcode_atts( array( 'title' => 'Contact Us', 'email' => 'info@example.com', ), $atts, 'mysite_contact_card' ); ob_start(); ?> <div class="contact-card"> <h3><?php echo esc_html( $atts['title'] ); ?></h3> <p><?php echo esc_html( $atts['email'] ); ?></p> </div> <?php return ob_get_clean(); } add_shortcode( 'mysite_contact_card', 'mysite_contact_card_shortcode' );
复杂 HTML 用输出缓冲更容易维护。

4. 只在有短代码的页面加载 CSS

性能
<?php function mysite_shortcode_assets() { global $post; if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'mysite_contact_card' ) ) { wp_enqueue_style( 'mysite-contact-card', plugin_dir_url( __FILE__ ) . 'assets/contact-card.css', array(), '1.0.0' ); } } add_action( 'wp_enqueue_scripts', 'mysite_shortcode_assets' );
短代码资源最好按需加载。

5. 短代码输出查询结果

进阶
<?php function mysite_latest_posts_shortcode() { $q = new WP_Query( array( 'posts_per_page' => 3, 'post_status' => 'publish', ) ); ob_start(); if ( $q->have_posts() ) { echo '<ul class="latest-posts">'; while ( $q->have_posts() ) { $q->the_post(); echo '<li><a href="' . esc_url( get_permalink() ) . '">' . esc_html( get_the_title() ) . '</a></li>'; } echo '</ul>'; wp_reset_postdata(); } return ob_get_clean(); } add_shortcode( 'mysite_latest_posts', 'mysite_latest_posts_shortcode' );
短代码中写 WP_Query 后也要 reset。

6. 判断短代码是否已存在

兼容
<?php if ( ! shortcode_exists( 'mysite_button' ) ) { add_shortcode( 'mysite_button', 'mysite_button_shortcode' ); }
避免和其他插件短代码同名覆盖。

本页总结

短代码适合把可复用组件放进页面、文章或 Elementor。重点是 return、安全输出和资源按需加载。