Title: Passing variable from template to functions.php
Last modified: May 10, 2023

---

# Passing variable from template to functions.php

 *  Resolved [ingridskard](https://wordpress.org/support/users/ingridskard/)
 * (@ingridskard)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/passing-variable-from-template-to-functions-php/)
 * This is not directly Insert Pages related but I’m trying here first because it
   seems related to context issues that arise with Insert Pages. I’m sure there 
   is a simple solution for experienced developers. Let me know and I can move the
   topic to the main forum.
 * I have a site with a set of pages where each page uses the same Contact Form (
   WPForms). I want the form to send the email-on-submit to an address pulled from
   ACF, as seen here: [https://wordpress.org/support/topic/use-an-acf-field-for-the-form-admin-email/](https://wordpress.org/support/topic/use-an-acf-field-for-the-form-admin-email/)
 * It is not working because the callback function, which has to(?) live in functions.
   php can’t pull postId from context so I need to send that in. Calling `InsertPagesPlugin::
   get_instance()->inserted_page_ids;` doesn’t work there. Tried global variables,
   set/get_query_vars() which did not work.
 * The template:
 *     ```wp-block-code
       $stack = InsertPagesPlugin::get_instance()->inserted_page_ids;
       $context_id = end($stack);
       $email = get_field('contact_email', $context_id);
   
       if ($email):
           set_query_var('recipient_email', $email);
           echo do_shortcode('[wpforms id="1250" title="false"]');
       endif;
       ```
   
 * In functions.php:
 *     ```wp-block-code
       function wpf_dev_register_smarttag( $tags ) {
           $tags['recipient_email'] = 'Recipient Email';
           return $tags;
       }
       add_filter( 'wpforms_smart_tags', 'wpf_dev_register_smarttag' );
   
       function wpf_dev_process_smarttag( $content, $tag ) {
           if ( 'recipient_email' === $tag ) 
           {
               $recipient_email = get_query_var('recipient_email');
               $content = str_replace( '{recipient_email}', $recipient_email, $content );
           }
   
           return $content;
       }
       add_filter( 'wpforms_smart_tag_process', 'wpf_dev_process_smarttag', 10, 2 );
       ```
   
 * I’m noticing it would have been perfect if the WPForms shortcode could accept
   this value as an argument.
 * Any help will be appreciated while I continue to read WP docs to try and understand
   contex better in WP. Thank you!
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fpassing-variable-from-template-to-functions-php%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 1 replies (of 1 total)

 *  Thread Starter [ingridskard](https://wordpress.org/support/users/ingridskard/)
 * (@ingridskard)
 * [3 years ago](https://wordpress.org/support/topic/passing-variable-from-template-to-functions-php/#post-16734622)
 * A global variable did the trick. Ugly as sin but works!

Viewing 1 replies (of 1 total)

The topic ‘Passing variable from template to functions.php’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/insert-pages.svg)
 * [Insert Pages](https://wordpress.org/plugins/insert-pages/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/insert-pages/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/insert-pages/)
 * [Active Topics](https://wordpress.org/support/plugin/insert-pages/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/insert-pages/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/insert-pages/reviews/)

 * 1 reply
 * 1 participant
 * Last reply from: [ingridskard](https://wordpress.org/support/users/ingridskard/)
 * Last activity: [3 years ago](https://wordpress.org/support/topic/passing-variable-from-template-to-functions-php/#post-16734622)
 * Status: resolved