11 Elementor 代码集成

整理 Elementor 可用的短代码、模板判断、资源加载和常见维护场景。

11 Elementor 代码集成

本页关键词

Elementor shortcode elementor/frontend/after_enqueue_scripts is_plugin_active

学习目标

代码使用提醒

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

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

1. 判断 Elementor 是否启用

基础
<?php include_once ABSPATH . 'wp-admin/includes/plugin.php'; if ( is_plugin_active( 'elementor/elementor.php' ) ) { // Elementor 已启用。 }
is_plugin_active 需要先引入 plugin.php。

2. 给 Elementor 页面使用短代码

实用
<?php function mysite_quote_box_shortcode() { return '<div class="quote-box"><h3>Request a Quote</h3><p>Contact our team for project pricing.</p></div>'; } add_shortcode( 'mysite_quote_box', 'mysite_quote_box_shortcode' ); // Elementor 的 Shortcode 小工具中使用: // [mysite_quote_box]
最稳定的集成方式之一:PHP 逻辑封装成 shortcode,Elementor 页面插入。

3. Elementor 前台加载额外脚本

实用
<?php function mysite_elementor_frontend_assets() { wp_enqueue_script( 'mysite-elementor-enhance', get_stylesheet_directory_uri() . '/assets/js/elementor-enhance.js', array(), '1.0.0', true ); } add_action( 'elementor/frontend/after_enqueue_scripts', 'mysite_elementor_frontend_assets' );
适合只在 Elementor 前台上下文添加增强脚本。

4. 在 body class 中标记 Elementor 页面

实用
<?php function mysite_elementor_body_class( $classes ) { if ( did_action( 'elementor/loaded' ) && is_singular() ) { $classes[] = 'has-elementor-support'; } return $classes; } add_filter( 'body_class', 'mysite_elementor_body_class' );
可以给 Elementor 相关页面加样式标记。

5. 避免 Elementor 页面重复加载主题模块

性能
<?php function mysite_skip_theme_slider_on_elementor() { if ( did_action( 'elementor/loaded' ) && is_page_template( 'elementor_header_footer' ) ) { return; } wp_enqueue_script( 'mysite-slider', get_stylesheet_directory_uri() . '/assets/js/slider.js', array(), '1.0.0', true ); } add_action( 'wp_enqueue_scripts', 'mysite_skip_theme_slider_on_elementor' );
减少 Elementor 页面中不必要的主题脚本。

6. Elementor 中输出动态联系信息

实用
<?php function mysite_phone_shortcode() { $phone = get_option( 'mysite_phone', '+61 0000 0000' ); return '<a href="tel:' . esc_attr( preg_replace( '/[^0-9+]/', '', $phone ) ) . '">' . esc_html( $phone ) . '</a>'; } add_shortcode( 'mysite_phone', 'mysite_phone_shortcode' );
后台设置 + shortcode 是让 Elementor 内容动态化的实用组合。

本页总结

Elementor 集成建议通过短代码、hooks 和条件加载完成。不要改 Elementor 核心文件,避免更新后丢失。