Jump to content

Use a string within a function called from outside


etrader

Recommended Posts

I want to introduce some variable into Wordpress functions, but it does not work when defining the string outside the function. Consider this example function:

 

	function bbp_get_topic_edit_link( $args = '' ) {
	$defaults = array (
		'id'           => 0,
		'link_before'  => '',
		'link_after'   => '',
		'edit_text'    => __( 'Edit', 'bbpress' )
	);

 

This does not work when I introduce a string as (when string to replace "edit" with "something")

 

	
$str="SOMETHING";
               function bbp_get_topic_edit_link( $args = '' ) {
	$defaults = array (
		'id'           => 0,
		'link_before'  => '',
		'link_after'   => '',
		'edit_text'    => $str
	);

 

But it works when introducing the string within the function as

               function bbp_get_topic_edit_link( $args = '' ) {
$str="SOMETHING";
	$defaults = array (
		'id'           => 0,
		'link_before'  => '',
		'link_after'   => '',
		'edit_text'    => $str
	);

 

How I can make my strings active within functions? I want to store all strings within a reference file and the include it in each page (or through header).

It's called scope. You can see that in the function declaration there is $args = '', that is a single argument. You can have more by separating them with commas.

 

function my_cool_function($arg1, $arg2, $arg3)

You can define arguments are optional by setting a default value, which is what yours is doing. You can add another argument to the function and pass in the string when you need too. Otherwise you can use the global keyword, although it's not exactly a solution but more of a bandaid to your problem.

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.