Skip to content

Instantly share code, notes, and snippets.

@thehung2224
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thehung2224/c50e695b458d64104f62 to your computer and use it in GitHub Desktop.
Save thehung2224/c50e695b458d64104f62 to your computer and use it in GitHub Desktop.
Cách dùng Preg_match trong PHP
if ( !function_exists( 'extract_gallery_images_ids' ) ) {
function extract_gallery_images_ids() {
global $post;
$content = "[gallery ids="7,8,9,10"]";
$pattern = "\[(\[?)(embed|wp_caption|caption|playlist|audio|video|dot_recommends|dot_recommended_posts|gallery)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)"
preg_match( "/$pattern/s", $content, $match );
if( isset( $match[2] ) && ( "gallery" == $match[2] ) ) {
$atts = $match[3];
$atts = shortcode_parse_atts( $match[3] );
$gallery_images = isset( $atts['ids'] ) ? explode( ',', $atts['ids'] ) : get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID .'&order=ASC&orderby=menu_order ID' );
return $gallery_images;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment