You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为WordPress的WPForms插件提交按钮添加onclick属性?

Adding onclick Conversion Tracking to WPForms Submit Button

Got it, let's tackle this. Adding an onclick attribute to your WPForms submit button is totally doable—here are the most reliable methods (we’ll prioritize approaches that don’t break when you update WPForms or your theme):

This is the official, update-safe way to modify the submit button HTML. It hooks into WPForms' button generation process to inject your onclick attribute.

  1. Go to your WordPress dashboard, navigate to Appearance > Theme File Editor (or use a code snippet plugin like Code Snippets for safer edits).
  2. Open your theme's functions.php file (or your child theme's functions.php to avoid losing changes on theme updates).
  3. Paste this code, then customize the values to match your needs:
add_filter( 'wpforms_submit_button', 'add_gtag_onclick_to_wpforms', 10, 2 );
function add_gtag_onclick_to_wpforms( $button, $form_data ) {
    // Replace with your target form ID(s) (use an array for multiple forms)
    $target_forms = array( 123 );
    
    // Only modify the button if it's one of your target forms
    if ( in_array( $form_data['id'], $target_forms ) ) {
        // Replace with your conversion link
        $conversion_url = 'http://example.com/your-link';
        // Escape the URL for security and build the onclick attribute
        $onclick_attr = ' onclick="return gtag_report_conversion(\'' . esc_url( $conversion_url ) . '\')"';
        // Insert the attribute into the button tag
        $button = str_replace( '>', $onclick_attr . '>', $button );
    }
    
    return $button;
}
  • How it works: This filter intercepts the default submit button HTML WPForms generates, checks if it’s for your target form, then adds the onclick attribute before closing the button tag. The esc_url function ensures your link is safe from XSS vulnerabilities.

Method 2: Bind the Click Event with JavaScript

If you prefer not to edit PHP, you can use JavaScript to attach the conversion tracking function to the button. This works well if you only need to track specific forms or want to keep changes isolated.

  1. In your WordPress dashboard, go to Appearance > Customize > Additional CSS/JS (the exact location may vary by theme; if your theme doesn’t have this, use a code snippet plugin to add custom JS).
  2. Paste this code, then update the selector and conversion URL:
document.addEventListener('DOMContentLoaded', function() {
    // Replace with your form's submit button selector (find it via browser dev tools)
    const submitBtn = document.querySelector('#wpforms-form-123 button[type="submit"]');
    
    if (submitBtn) {
        submitBtn.addEventListener('click', function(e) {
            // Replace with your conversion link
            const conversionLink = 'http://example.com/your-link';
            // Call the gtag function; prevent form submission if it returns false
            if (!gtag_report_conversion(conversionLink)) {
                e.preventDefault();
            }
        });
    }
});
  • Note: Make sure you’ve already loaded the Google Analytics gtag script with the gtag_report_conversion function defined on your site—otherwise, this will throw an error and block form submissions.

Only use this if you absolutely need to, since plugin updates will overwrite direct edits to WPForms files. To make it update-safe, you’ll need to override the template in your child theme:

  1. Copy the file wpforms/templates/submit-button.php from the WPForms plugin folder.
  2. In your child theme, create a new folder structure: wpforms/templates/ and paste the copied file there.
  3. Open the file and modify the button tag to include your onclick attribute:
<button type="submit" id="<?php echo esc_attr( $button_id ); ?>" class="<?php echo esc_attr( $button_classes ); ?>" onclick="return gtag_report_conversion('http://example.com/your-link')">
    <?php echo esc_html( $button_text ); ?>
</button>
  • Warning: WPForms may change their template structure in future updates, so this method could break unexpectedly. Stick with the filter or JS method if possible.

Important Pre-Requisite

Double-check that your site has the gtag_report_conversion function properly defined in your Google Analytics setup. If this function is missing, the onclick attribute will cause errors and prevent form submissions.

内容的提问来源于stack exchange,提问作者iceiceicy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:42:26