开发者

Replace Wordpress shortcode containing id with html markup using the id

I'm building a filter plugin in Wordpress and I'm replacing some plugin specific tags with bits of html.

Example: [VIDEO ID=12] will be replaced via preg_replaced in this function

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

I'm not exactly sure how to make sure I supply the correct ($id) param to my get_video_block() function for each replacement occurrence.

There's no real loop going on other than inside the preg_replace() function so, how would I supply that value?

For more context开发者_运维问答, get_video_block() function:

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;
    
}


You can use preg_replace_callback() for that purpose. You'll also need to wrap \d+ in ( parentheses ) so it can be captured and used in the callback function.

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

Note that $matches[1] is used because $matches[0] contains the entire string matched by the regular expression.

Erwin's comment may be of use to you — WordPress has a shortcode API that manages parsing of shortcodes for you, so you can concentrate on dealing with what you want to do with the shortcode attributes.


@BoltClock's answer is correct, but create_function() is a little old fashioned now.

Pass the captured id to the helper function via an anonymous function inside of preg_replace_callback().

The helper function's returned string will be used to replace the entire matched shortcode.

Since no limit was declared for preg_replace_callback(), it will make potentially multiple replacements.

Code: (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");

Output:

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

P.s. As @azureru mentioned in a comment, this does seem like a good candidate for implementing Wordpress's shortcode api. http://codex.wordpress.org/Shortcode_API

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜