• Hello,

    I am trying to dynamically generate shortcode content based on user-input form fields. I have created the function where the shortcode names (i.e., [blah]) come from the custom fields of any given post. However, I can only generate the content for the shortcode if I type a specific string value in the function called by add_shortcode.

    For instance:

    function myFields()
    {
      $fieldValue = "Content for the shortcode."
      return $fieldValue;
    }
    foreach ( $custom_field_keys as $key => $keyName )
    {
      //Bunch of other stuff
    
      add_shortcode((string)$keyName, 'myFields');
    }

    When [$keyName] exists in the post, it will be replaced by “Content for the shortcode” with no problems.

    However, if I try and reference $fieldValue directly or even convert it into a string, [$keyName] has no value in the post:

    $custom_field_keys = get_post_custom_keys();
      function myFields()
      {
             return $fieldValue;
      }
      foreach ( $custom_field_keys as $key => $keyName )
      {
             //Bunch of other stuff
    	add_shortcode((string)$keyName, 'myFields');
      }

    I am thinking that it has something to do with calling the function from within foreach. For some reason the value for $fieldValue is not being retrieved.

    Any help would be appreciated and I’ll answer any questsions! Thanks!

Viewing 1 replies (of 1 total)
  • Thread Starter B & L Marketing

    (@unifiedac)

    I should have mentioned that on the second example, $fieldValue is being defined by a $_GET statement. If I take the $_GET statement out of the foreach loop, it does not retrieve the value:

    $custom_field_keys = get_post_custom_keys();
    
    function myFields()
    {
             return $fieldValue;
    }
    
    foreach ( $custom_field_keys as $key => $keyName )
    {
              //Bunch of other stuff
    	$fieldValue = $_GET["$keyName"];
    	add_shortcode((string)$keyName, 'myFields');
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Dynamic Shortcode Values’ is closed to new replies.