Display gallery on top before content

Is it possible to execute the gallery shortcode before any other content no matter where the shortcode is in the post HTML?

Related posts

Leave a Reply

2 comments

  1. function gallery_first( $content) {
        $expr = '/[gallery(.*?)]/i';
        return preg_replace_callback( $expr, create_function('$matches', 'return do_shortcode($matches[0]);'), $content);
    }
    add_filter( 'the_content', 'gallery_first', 6); // prio 6 executes this function previous to all other filter functions
    
  2. function gallery_first( $content) {
        $expr = '/[gallery(.*?)]/i';
        return (" [gallery] " . preg_replace( $expr, '', $content)); // deletes all existing gallery shortcodes
    }
    add_filter( 'the_content', 'gallery_first', 6); // level '6' executes this function previous to all other filter functions