Is it possible to setup a gallery in the backend without including it in the content?

I’ve used this to display the gallery under the content in a few of my website, or in a custom position, you can manipulate it however you need. The only draw back (sort of) is if there are 2 galleries, it will only work with the first gallery.

<?php
    get_header();

    // Get Post Gallery before The Loop
    global $post;
    $gallery = get_post_gallery($post->ID);
?>

    <?php if(have_posts()) : ?>
        <?php while(have_posts()) : the_post(); ?>
            $content = strip_shortcode_gallery( get_the_content() );                                // remove Gallery Shortcode
            $content = str_replace( ']]>', ']]&gt;', apply_filters( 'the_content', $content ) );    // Resetup Content
            echo $content;                                                                          // Echo Content
        <?php endwhile; ?>
    <?php endif; ?>

    <div id="gallery">
        <?php echo $gallery; ?>
    </div>

<?php get_footer(); ?>

Functions.php

function  strip_shortcode_gallery( $content ) {
    preg_match_all( "https://wordpress.stackexchange.com/". get_shortcode_regex() .'/s', $content, $matches, PREG_SET_ORDER );
    if ( ! empty( $matches ) ) {
        foreach ( $matches as $shortcode ) {
            if ( 'gallery' === $shortcode[2] ) {
                $pos = strpos( $content, $shortcode[0] );
                if ($pos !== false)
                    return substr_replace( $content, '', $pos, strlen($shortcode[0]) );
            }
        }
    }
    return $content;
}

error code: 523