Support » Plugin: Code Snippets » Add snippet to form

  • Resolved jrweb

    (@jrweb)


    Hi!

    Is there any way to add the snippet to work in an Elementor form? I need to generate a referencenumber for each submission. At the moment I have a PHP code that generates a random number but I can’t seem to figure out how to include it in the Elementor form. Anyone got any ideas?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Code Snippets v3 Pro will be adding much better support for working with Elementor, but in the meantime, you can add your code as a shortcode like so:

    add_action( 'display_ref_number', function () {
        $output = 'generate output here';
        return $output;
    } );

    And then you just use [display_ref_number] in an Elementor widget.

    Thread Starter jrweb

    (@jrweb)

    Thank you @bungeshea! So where exactly would I add my own code? It looks like this and creates a random string containing 10 random characters.

    function generateRandomString($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }
    
    echo generateRandomString();
    • This reply was modified 1 year ago by jrweb.
    Plugin Author Shea Bunge

    (@bungeshea)

    You could combine them like this:

    function generateRandomString($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }
    
    add_action( 'display_ref_number', function () {
        $output = generateRandomString();
        return $output;
    } );
    Thread Starter jrweb

    (@jrweb)

    @bungeshea I added it as a snippet and inside my form I added [display_ref_number] in both email and email 2. However this doesn’t print the string it only prints “[display_ref_number]”. Am I doing something wrong? Is there a shortcode I can use inside the form that creates the string?

    Plugin Author Shea Bunge

    (@bungeshea)

    Ah, I missed that you were using a form widget. Is this one of the core Elementor widgets?

    Thread Starter jrweb

    (@jrweb)

    @bungeshea Yes it is Elementors widget form!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Add snippet to form’ is closed to new replies.