Copy shortcode from the form list table

This feature is now available within the Gravity Hopper plugin.

Although the WordPress block editor supports embedding forms with the Gravity Forms block, sometimes you can still use easy access to the Gravity Forms shortcode for a form. Taking after the Gravity PDF implementation of a shortcode copy column, this scripts adds a column to the form list table that offers a click to copy button for each form’s shortcode.

// Copy [[gravityform…]] shortcode from the form list table
add_filter( 'gform_form_list_columns', function( $columns ) {
    $columns['shortcode'] = 'Shortcode';
    return $columns;
}, 999 );

add_action( 'gform_form_list_column_shortcode', function( $form ) {
    echo "<span class='gform_shortcode_snippet' onclick='copyShortcode(this);'><span class='dashicons dashicons-shortcode'></span><span> Copy Shortcode</span></span>";
    echo "<input type='text' style='position: absolute; left: -99999px;' readonly='readonly' value='[[gravityform id={$form->id} title=false description=false ajax=true]]'></input>";
} );

add_action( 'admin_init', function() {

    if ( class_exists( 'GFForms' ) && GFForms::is_gravity_page() ) {

        add_action( 'admin_print_styles', function() {
            echo '<style>
                    th.column-shortcode { width: 145px; }
                    .gform_shortcode_snippet {
                        display: inline-block; white-space: nowrap; cursor: copy; height: auto; padding: .2em .45em;
                        font-size: .75rem; background-color: #ecedf8; border: 1px solid #d5d7e9; border-radius: 4px;
                    .gform_shortcode_snippet:hover { border-color: #3b7ca877; }
                    .gform_shortcode_snippet.copied { cursor: none; pointer-events: none; background-color: #f6f9fc; border-color: #3b7ca877; }
                    .gform_shortcode_snippet .dashicons { position: relative; top: 3px; font-size: 15px; }
        } );

        add_action( 'admin_print_footer_scripts', function() {
            echo '<script>
                    function copyShortcode(item) {
              ; document.execCommand("copy");
                        item.className = "gform_shortcode_snippet copied"; item.lastChild.innerHTML = " Shortcode copied!";
                        setTimeout(function(){ item.className = "gform_shortcode_snippet"; item.lastChild.innerHTML = " Copy Shortcode"; }, 3000);
        } );


} );Code language: PHP (php)

This is what you’ll get.

How do you find this article?