Get current user_id on wordpress for custom variable (Solved)
any clue how i can achive this? cause when i run as client side it wont get the data on typebot
1 Reply
thanks for this great tools! now i can full automate my whole member include payment gateway without woocommerce so if u need this case too here how u can solve it
Create php snippet or inject this code to ur function.php child theme
function add_user_id_to_footer() {
if (is_user_logged_in()) {
$current_user = wp_get_current_user();
echo '<script>var currentUserId = ' . json_encode($current_user->ID) . ';</script>';
}
}
add_action('wp_footer', 'add_user_id_to_footer');
then just add your typebot embed script + prefilledvariables
<typebot-standard style="height: 100vh; "></typebot-standard> <script> const typebotInitScript = document.createElement("script"); typebotInitScript.type = "module"; typebotInitScript.innerHTML =
import Typebot from 'https://cdn.jsdelivr.net/npm/@typebot.io/[email protected]/dist/web.js'
Typebot.initStandard({ typebot: "YOUR-BOT-ID", prefilledVariables: { user_id: currentUserId.toString(), }, apiHost: "YOUR-TYPEBOT-HOST", });; document.body.append(typebotInitScript); </script>