WooCommerce新用户注册时通过Session添加头部跟踪脚本的技术咨询
Got it, here's a structured, refined version of your solution to add the Facebook CompleteRegistration tracking script immediately after a new user finishes registering on your WooCommerce site:
1. Add Session Marker in functions.php
We'll use the user_register action hook to set a session flag when a user successfully registers. This flag lets us identify users who just completed the registration process:
add_action('user_register', 'registration_save'); function registration_save($user_id) { if ($user_id) { $_SESSION["user_registered"] = true; } }
Important: If your WordPress site doesn't automatically start sessions, you'll need to add
session_start();at the very top of yourfunctions.phpfile. Without this, the$_SESSIONvariable won't work as expected.
2. Output Tracking Script in header.php
Next, we check for the session flag in your theme's header. If it exists, we output the Facebook pixel tracking script, then immediately unset the flag to prevent the script from firing on subsequent page loads:
<?php if (isset($_SESSION["user_registered"])): ?> <script>fbq('track', 'CompleteRegistration');</script> <?php unset($_SESSION["user_registered"]); ?> <?php endif; ?>
- Make sure you've already loaded the base Facebook pixel code on your site—this tracking script won't work without it.
- If you're using a child theme, always modify the child theme's
functions.phpandheader.phpfiles. This way, your custom code won't get overwritten when the parent theme updates.
内容的提问来源于stack exchange,提问作者Elichy

