Title: Execute code snippet from button
Last modified: February 25, 2021

---

# Execute code snippet from button

 *  Resolved [pmonty](https://wordpress.org/support/users/pmonty/)
 * (@pmonty)
 * [5 years, 3 months ago](https://wordpress.org/support/topic/execute-code-snippet-from-button/)
 * So I have a new code snippet and at the bottom of that it has the following code
   to actually call it
 *     ```
       if (!empty($_POST['paynow'])) {
           //Generate signature (see Custom Integration -> Step 2)
           $data["signature"] = generateSignature($data, $passPhrase);
   
           //Convert the data array to a string
           $pfParamString = dataToString($data);
   
           //Generate payment identifier
           $identifier = generatePaymentIdentifier($pfParamString);
   
           if ($identifier !== null) {
               echo '<script type="text/javascript">window.payfast_do_onsite_payment({"uuid":"' . $identifier . '"}, function (result) {
                   if (result === true) {
       		 		// Payment Completed
                   }
                   else {
                     	// Payment Window Closed
                   }
                 });</script>';
           }
       }
       ```
   
 * I am wondering if there is a way to call this code from the front end. Can’t 
   really be a submit button I guess because I don’t want the form to continue to
   the next step and at the same time I need the code to execute which actually 
   makes a modal popup.
 * Happy to change the if statement if needed.
 * Any tips or ideas no matter how helpful would be incredible!

Viewing 1 replies (of 1 total)

 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [5 years, 3 months ago](https://wordpress.org/support/topic/execute-code-snippet-from-button/#post-14165582)
 * The easiest method would probably be to create a shortcode:
 *     ```
       add_shortcode( 'payment_modal', function () {
       	ob_start();
   
       	// do output here
   
       	return ob_get_clean();
       } );
       ```
   
 * Once added, you can just use the `[payment_modal]` shortcode in a post.

Viewing 1 replies (of 1 total)

The topic ‘Execute code snippet from button’ is closed to new replies.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * Last activity: [5 years, 3 months ago](https://wordpress.org/support/topic/execute-code-snippet-from-button/#post-14165582)
 * Status: resolved