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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.