Vim li cas thiab cov kev daws teeb meem rau WordPress cov lus qhia luv luv tsis raug parsing

WordPress Sau rau hauv seem lus tawm tswv yim ntawm tsab xov xwm [shortcode]Txawm li cas los xij, qhov tshwm sim tsuas yog tso saib cov ntawv yooj yim es tsis yog qhov xav tau? Qhov no yog WordPress lub tshuab ua haujlwm. Hauv qab no, kuv yuav tshuaj xyuas cov laj thawj thiab muab cov kev daws teeb meem tiav.

Vim li cas cov lus tawm tswv yim tsis txhawb nqa shortcodes?

  • Cov kev txwv tsis pub dhau los
    Vim yog kev ruaj ntseg, WordPress tsis khiav tau hauv seem lus tawm tswv yim. do_shortcode()Qhov no tiv thaiv cov neeg siv kom tsis txhob muab cov ntawv, cov ntawv sau, lossis lwm yam ntsiab lus uas yuav cuam tshuam rau kev ruaj ntseg ntawm lub xaib.

  • Kev txwv ntawm daim ntawv thov
    Cov lus luv luv no feem ntau yog tsim los siv rau hauv cov ntsiab lus ntawm tsab xov xwm (the_contentQhov no suav nrog cov khoom siv hluav taws xob lossis cov plugins tshwj xeeb, thaum lub sijhawm tsis txhawb nqa ntu lus tawm tswv yim.

  • Kev cuam tshuam tsis siv neeg formatting
    Cov lus teb rau cov lus nug yuav yog wpautop Ntxiv cia li <p><br> Cov cim npe, uas yuav ua rau cov qauv syntax ntawm shortcodes tawg, ua rau lawv tsis tuaj yeem txheeb xyuas tau.

Vim li cas thiab cov kev daws teeb meem rau WordPress cov lus qhia luv luv tsis raug parsing

Ua tiav...

Kuv yuav ua li cas thiaj pab tau kev txhawb nqa shortcode rau cov lus tawm tswv yim?

  • Qhib cov lus qhia luv luv
    Nyob rau hauv lub ntsiab lus functions.php Ntxiv cov code hauv qab no rau koj cov ntaub ntawv lossis cov code snippet plugin:

    add_filter('comment_text', 'do_shortcode');
    

    Qhov no yuav ua rau ntu lus tawm tswv yim siv cov shortcodes.

  • Xyuas seb lub plugin puas tau qhib lawm.
    Muaj ntau cov shortcodes los ntawm cov plugins (xws li Contact Form 7 thiab WooCommerce). Yog tias lub plugin tsis qhib, cov shortcodes yuav tsis ua haujlwm.

  • Tsis txhob ua yuam kev syntax
    Thov paub tseeb tias cov lus luv luv no sau kom raug, piv txwv li:

    [shortcode attribute="value"]内容[/shortcode]
    
  • Tshawb xyuas seb puas muaj kev tsis sib haum xeeb ntawm cov ntsiab lus lossis plugin
    Hloov mus rau lub ntsiab lus tsis siv neeg (xws li Twenty Twenty-Five), lov tes taw txhua lub plugin ib qho los ntawm ib qho los sim thiab paub meej tias puas muaj kev tsis sib haum xeeb uas ua rau lub shortcode ua haujlwm tsis zoo.

  • tshem cache
    Thaum siv caching plugins lossis CDNs, koj yuav tsum tshem tawm cov cache tom qab kho kom pom qhov cuam tshuam.

Cov lej kom ruaj ntseg qhib cov lus qhia luv luv

Yuav kom tsis txhob muaj kev pheej hmoo ntawm kev ruaj ntseg, nws ua tau functions.php Ntxiv cov piv txwv tiav hauv qab no:

<?php
// 在评论区启用简码解析
add_filter('comment_text', 'do_shortcode');

// 限制允许的简码列表
function safe_comment_shortcodes($content) {
    // 定义允许的简码(加入 su_posts)
    $allowed_shortcodes = array('gallery', 'audio', 'video', 'contact-form-7', 'su_posts');

    // 遍历评论内容中的简码
    return preg_replace_callback('/\[(\w+)([^\]]*)\]/', function($matches) use ($allowed_shortcodes) {
        $shortcode = $matches[1];
        // 如果简码在允许列表中,正常解析
        if (in_array($shortcode, $allowed_shortcodes)) {
            return do_shortcode($matches[0]);
        }
        // 否则直接输出原始文本,避免执行
        return esc_html($matches[0]);
    }, $content);
}
add_filter('comment_text', 'safe_comment_shortcodes', 9);

kab lus piav qhia

  • Qhib cov shortcodesKab thawj zaug add_filter('comment_text', 'do_shortcode'); Pab txhawb nqa shortcode hauv seem lus.
  • Cov lej luv luv uas txwv tsis pub siv:pass $allowed_shortcodes Cov lus txhais ntawm array tso cai rau cov lus luv luv, piv txwv li gallery,audio,video,contact-form-7,su_posts.
  • Kev tswj hwm kev nyab xeebYog tias tus neeg siv nkag mus rau hauv daim ntawv luv luv uas tsis nyob hauv daim ntawv tso cai, cov lej yuav raug tso tawm mus rau cov ntawv nyeem yooj yim kom tsis txhob ua cov lej luv luv uas muaj kev phom sij.

Cov Kev Xyaum Zoo Tshaj Plaws

  • Tsuas yog tso cai rau cov shortcodes tsim nyog xwbPiv txwv li, hauv kev nthuav qhia xov xwm lossis daim ntawv, tsis txhob siv ntau cov shortcodes.
  • Tshawb xyuas cov plugin hloov tshiab tas liXyuas kom meej tias qhov shortcode source plugin muaj kev nyab xeeb thiab txhim khu kev qha.
  • Kev sib txuas ua ke ntawm caching thiab kev tiv thaiv pluginsYuav kom tsis txhob muaj kev ua tau zoo los ntawm kev ua shortcode.

Dynamically Enhanced Version: Backend Settings Interface (Tshawb nrhiav + Pawg + Frontend Hints)

Yog tias koj xav tau kev ywj pheej ntau dua, koj tuaj yeem siv cov lej txhim kho hauv qab no:

  • Sau tag nrho cov shortcodes uas tau sau npe cia(Xws li cov uas muab los ntawm plugins).
  • Txhawb kev nkag mus rau cov lej luv luv(如 。。。
  • lub thawv tshawb nrhiav:ceevTxoj HaujlwmKev luv luv.
  • Kev tso saib ua pawgKev muab pawg los ntawm plugin qhov chaw ua rau nws yooj yim dua.
  • Kev Tso Cai Rau Lub Luag HaujlwmTsuas yog cov lej luv luv hauv cov lus tawm tswv yim ntawm tus thawj coj xwb thiaj li yuav raug txheeb xyuas; cov lej luv luv hauv cov lus tawm tswv yim ntawm cov neeg siv ib txwm yuav tso tawm cov lus "Cov lej luv luv no tsuas yog muaj rau cov thawj coj xwb".
<?php
// 添加后台菜单
function cwl_shortcode_settings_menu() {
    add_options_page(
        '评论简码设置',
        '评论简码设置',
        'manage_options',
        'cwl-shortcode-settings',
        'cwl_shortcode_settings_page'
    );
}
add_action('admin_menu', 'cwl_shortcode_settings_menu');

// 注册设置
function cwl_register_shortcode_settings() {
    register_setting('cwl_shortcode_settings_group', 'cwl_allowed_shortcodes');
    register_setting('cwl_shortcode_settings_group', 'cwl_custom_shortcodes');
}
add_action('admin_init', 'cwl_register_shortcode_settings');

// 设置页面内容
function cwl_shortcode_settings_page() {
    global $shortcode_tags;
    $allowed = get_option('cwl_allowed_shortcodes', array());
    $custom  = get_option('cwl_custom_shortcodes', array());

    ?>
    <div class="wrap">
        <h1>评论区简码设置</h1>
        <form method="post" action="options.php">
            <?php settings_fields('cwl_shortcode_settings_group'); ?>

            <h2>搜索简码</h2>
            <input type="text" id="cwl_shortcode_search" placeholder="输入简码关键字..." style="width:300px;">

            <h2>已注册简码(按插件来源分组)</h2>
            <p>勾选允许在评论区执行的简码:</p>
            <div id="cwl_shortcode_list">
                <?php
                // 按插件来源分组
                $groups = array();
                foreach ($shortcode_tags as $tag => $callback) {
                    $source = is_array($callback) ? get_class($callback[0]) : (is_object($callback) ? get_class($callback) : '主题/未知来源');
                    $groups[$source][] = $tag;
                }

                foreach ($groups as $source => $tags) {
                    echo "<h3>" . esc_html($source) . "</h3>";
                    foreach ($tags as $tag) {
                        ?>
                        <label class="cwl_shortcode_item">
                            <input type="checkbox" name="cwl_allowed_shortcodes[]" value="<?php echo esc_attr($tag); ?>" <?php checked(in_array($tag, $allowed)); ?>>
                            <?php echo esc_html($tag); ?>
                        </label><br>
                        <?php
                    }
                }
                ?>
            </div>

            <h2>自定义简码</h2>
            <p>输入额外允许的简码(用逗号分隔):</p>
            <textarea name="cwl_custom_shortcodes" rows="3" cols="50"><?php echo esc_textarea(implode(',', (array)$custom)); ?></textarea>

            <?php submit_button(); ?>
        </form>
    </div>

    <script>
    // 简码搜索功能
    document.getElementById('cwl_shortcode_search').addEventListener('keyup', function() {
        var keyword = this.value.toLowerCase();
        document.querySelectorAll('.cwl_shortcode_item').forEach(function(item) {
            var text = item.textContent.toLowerCase();
            item.style.display = text.indexOf(keyword) > -1 ? '' : 'none';
        });
    });
    </script>
    <?php
}

// 评论区简码过滤(结合角色权限 + 前端提示)
function cwl_safe_comment_shortcodes($content) {
    $allowed_shortcodes = get_option('cwl_allowed_shortcodes', array());
    $custom_shortcodes  = get_option('cwl_custom_shortcodes', array());
    $allowed_shortcodes = array_merge($allowed_shortcodes, (array)$custom_shortcodes);

    return preg_replace_callback('/\[(\w+)([^\]]*)\]/', function($matches) use ($allowed_shortcodes) {
        $shortcode = $matches[1];
        // 管理员可解析简码
        if (current_user_can('manage_options')) {
            if (in_array($shortcode, $allowed_shortcodes)) {
                return do_shortcode($matches[0]);
            }
            return esc_html($matches[0]);
        } else {
            // 普通用户提示信息
            if (in_array($shortcode, $allowed_shortcodes)) {
                return '<span style="color:red;">此简码仅管理员可用</span>';
            }
            return esc_html($matches[0]);
        }
    }, $content);
}
add_filter('comment_text', 'cwl_safe_comment_shortcodes', 9);

mus hais txog

Cov ntu lus tawm tswv yim hauv WordPress tsis txheeb xyuas cov lus luv luv los ntawm lub neej ntawd vim yog kev ruaj ntseg thiab kev ua tau zoo. Koj tuaj yeem siv [lub cuab yeej txheeb xyuas] hloov chaw. functions.php Ntxiv ib lub lim dej kom pab tau cov shortcodes thiab ua ke nrog kev ruaj ntseg.

评论 评论

Koj email chaw nyob yuav tsis raug luam tawm. Yuav tsum tau siv cov liaj teb * Ntaus Cim

Scroll rau saum