Jump to content

Call a function within a foreach loop


unifiedac

Recommended Posts

Hello,

 

I am new to PHP and have come to a grinding halt in my project. I am attempting to dynamically generate values for Wordpress shortcodes ( [example] ). I have successfully created the shortcodes and am having trouble setting their value. When I call the function that is supposed to return the value for the shortcode, it returns empty. I can however hard code a string and have it returned to the function, thereby manually creating a value for the shortcode. However, because the shortcodes are being generated and filled dynamically, I need this function to work without specific values. I know this may sound like a Wordpress specific question, but I have a feeling it's just my lack of understanding of PHP operations. So, here is the basic layout:

 

The name of the shortcode is generated from within a foreach loop that retrieves the name of custom fields on the post ($keyName).

 

$custom_field_keys = get_post_custom_keys();
foreach ( $custom_field_keys as $key => $keyName ) 
{
		$valuet = trim($keyName);
  		if ( '_' == $valuet{0} )
		continue;
	if ( get_post_meta($thePostID, $keyName, true) ) :
	$fieldName = get_post_meta($thePostID, $keyName, true);
	endif;
	$fieldValue = $_GET["$keyName"];
	add_shortcode((string)$keyName, 'myFields');

	}

 

The add_shortcode function has two parameters ('name_of_shortcode', 'function_name'):

 

add_shortcode((string)$keyName, 'myFields');

 

I need the myFields function to return the value of $fieldValue from within the foreach loop. That's where the problem lies.

This doesn't work:

 

function myFields()
{ 
$myString = preg_replace('/[^a-zA-Z\s]/', '', $fieldValue);
return $myString;
}

 

This does work:

function myFields()
{ 
return 'Content to replace the shortcode';
}

 

This code will replace [shortcode] with "Content to replace the shortcode" in the post.

 

The ultimate goal is: when [$keyName] appears anywhere within the post, it will display the content returned by the myFields function ($fieldValue). However, it's not working.  :'( I can't seem to pass the value of $fieldValue to the myFields function and thereby return it to add_shortcode. Any help is appreciated and I am awaiting anxiously to answer any questions.

 

Here's all the code together:

 

function myFields()
{ 
$myString = preg_replace('/[^a-zA-Z\s]/', '', $fieldValue);
return $myString;
}

                $custom_field_keys = get_post_custom_keys();

foreach ( $custom_field_keys as $key => $keyName ) 
{
		$valuet = trim($keyName);
  		if ( '_' == $valuet{0} )
		continue;
	if ( get_post_meta($thePostID, $keyName, true) ) :
	$fieldName = get_post_meta($thePostID, $keyName, true);
	endif;
	$fieldValue = $_GET["$keyName"];
	add_shortcode((string)$keyName, 'yourFunction');

	}

Link to comment
https://forums.phpfreaks.com/topic/232065-call-a-function-within-a-foreach-loop/
Share on other sites

I had already tried this, it returns no value. When myFields is called, there is no way to send a parameter. I have also tried this:

 

add_shortcode((string)$keyName, 'myFields($fieldValue)');

 

Thanks for the interest. Let me know if anything else comes to mind.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.