I'm trying to pass a variable to be used in the function that's an arguement of add_action, like so.

$map_code = "test";
function insert_map_code_after_content($map_code){
        <script id=\"google-maps\">
add_action("after_content", "insert_map_code_after_content", 10, 1);

However, the var_dump just produces an empty string. How do I get it to output "test"?

  • Why does it need to be an argument? Where is the value coming from? Dec 21, 2017 at 2:21
  • You may want to check the difference between add_action and do_action to understand why its not getting passed. Dec 21, 2017 at 2:34
  • Jacob, I am trying to create a shortcode for a google map. The lat and lon get passed as attributes. Therefore, I'm generating the google map JS code on the fly and I store that in a variable. I need to use that variable, which contains the code. Dec 21, 2017 at 2:38
  • This isn't code for a shortcode though. This is an action hook onto after_content, which isn't a standard wordpress hook. Do you want to create a shortcode like [map lat="1234" lng="4321"]? Dec 21, 2017 at 2:55
  • Yes, I do. I created the after_content action myself in page.php. I'm basically trying to create multiple google maps on one page using shortcodes and attributes...if there are multiple, I slowly build up the JS code. Then, I output all of that at once after the content. So, I need to be able to pass that variable into my function, since it uses the shortcode atts. Dec 21, 2017 at 2:57


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.