【问题标题】:Replace Wordpress shortcode containing id with html markup using the id使用 id 将包含 id 的 Wordpress 短代码替换为 html 标记
【发布时间】:2010-08-25 16:37:06
【问题描述】:

我正在 Wordpress 中构建一个过滤器插件,并用一些 html 替换一些插件特定的标签。

示例:[VIDEO ID=12] 将通过此函数中的 preg_replaced 替换

function display_video($text){
   
   $pattern = '/\[VIDEO ID\=\d+\]/';
   
   $text=preg_replace($pattern,get_video_block($id),$text);
   
   return $text;
}

我不确定如何确保为每个替换事件向我的 get_video_block() 函数提供正确的 ($id) 参数。

除了preg_replace() 函数内部没有真正的循环,那么我将如何提供该值?

更多上下文,get_video_block()函数:

function get_video_block($id){
    global $wpdb;
    $wpdb->show_errors();
    $table_name = $wpdb->prefix . "video_manager";
    $query = "SELECT * FROM " . $table_name . " WHERE `index` = '$id'"; 
    $results = $wpdb->get_results($query, ARRAY_A);
    
    $results = $results[0];
    
    $returnString = '<div class="vidBlock">';
        $returnString .= $results['embed_code'];
        
        $returnString .= '<div class="voteBar">';
            $returnString .= $results['vote_text'];
            $returnString .= '<input type="button" value="YES" class="voteButton">';
            $returnString .= '<input type="button" value="NO" class="voteButton">';
        $returnString .= '</div>';
        
        $returnString .= $results['title'] . '<br>';
        $returnString .= $results['description'] . '<br>';
    
    $returnString .= '</div>';
    
    return $returnString;
    
}

【问题讨论】:

  • 只是好奇,如果它是一个带有 [code attrib=""] 模式的 wordpress 过滤器,为什么不使用 wordpress 短代码 API? codex.wordpress.org/Shortcode_API
  • get_video_block() 在做什么?
  • 我没有使用它,因为我以前从未听说过它;)
  • @Felix... 它正在构建一个小视频模块。
  • 附注:无论前缀是否可信,在 get_video_block() 函数中转义表名会很有用。

标签: php regex wordpress preg-replace shortcode


【解决方案1】:

您可以为此目的使用preg_replace_callback()。您还需要将\d+ 包裹在( 括号中),以便在回调函数中捕获和使用它。

function display_video($text) {
    $callback = create_function('$matches', 'return get_video_block($matches[1])');
    return preg_replace_callback('/\[VIDEO ID\=(\d+)\]/', $callback, $text);
}

注意使用$matches[1]是因为$matches[0]包含正则表达式匹配的整个字符串。

Erwin 的评论可能对您有用 — WordPress 有一个 shortcode API 可以为您管理短代码解析,因此您可以专注于处理您想要对短代码属性执行的操作。

【讨论】:

    【解决方案2】:

    @BoltClock 的回答是正确的,但create_function() 现在有点过时了。

    通过preg_replace_callback() 内部的匿名函数将捕获的 id 传递给辅助函数。

    辅助函数返回的字符串将用于替换整个匹配的短代码。

    由于没有为 preg_replace_callback() 声明任何限制,它可能会进行多次替换。

    代码:(Demo)

    function get_video_block($id) {
        return "***replacement text for $id***";
    }
    
    function display_video($text) {
       return preg_replace_callback(
                  '/\[VIDEO ID=(\d+)]/',
                  function($m) {
                      return get_video_block($m[1]);
                  },
                  $text
              );
    }
    
    echo display_video("Here is a video [VIDEO ID=33] to watch");
    

    输出:

    Here is a video ***replacement text for 33*** to watch
    

    附:正如@azureru 在评论中提到的那样,这似乎是实现 Wordpress 短代码 api 的一个很好的候选者。 http://codex.wordpress.org/Shortcode_API

    【讨论】:

      猜你喜欢
      • 2023-01-12
      • 2023-03-28
      • 2016-06-22
      • 2018-03-07
      • 2021-11-29
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多