variable

You’ve probably seen code which includes the $ dollar sign like this:

$output

This is whats called in PHP language, a variable.

WordPress & Genesis use thousands of variables in the code.

If you want to learn more about the code, you’ll need to understand variables.

Video Guide

This video walks you through this post and shows you the results of testing all the code in this post. Members can ask questions at any time.

A variable is something you can create ( declare ) and use in PHP code like this:

add_action( 'genesis_after_header', 'using_variables' );
function using_variables() {
$text = "Hello World";
echo $text;
}

When you create a variable, you always start with a $ dollar sign and then create a descriptive name for the variable like this:

$My_Name

Once you have created the variable, you can then assign a value to it like this:

$My_Name = "Brad";

Once the variable has a value, you can then output or print the value for your variable like this:

$My_Name = "Brad";
echo $My_Name;

We can now hook in the above code and output the value for the variable in a custom function, ( in a themes functions file ) like this:

add_action( 'genesis_after_header', 'using_variables' );
function using_variables() {
$My_Name = "Brad";
echo $My_Name;
}

Or

We can wrap the code in opening & closing PHP tags and use the code in a template file like this:

<?php 
$My_Name = "Brad";
echo $My_Name;
?>

The above variable only works within the function it is defined meaning you can’t take the $My_Name variable and use it in another function like this:

add_action( 'genesis_before_header', 'using_variables_in_other_functions');
function using_variables_in_other_functions() {
echo $My_Name;
}

The only way you can use the variable again in another function is if you originally declare it outside the function ( in your functions file ) to begin with like this:

$My_Name = "Brad";

Then you’ll need to use the global keyword like this:

add_action( 'genesis_after_header', 'using_variables' );
function using_variables() {
global $My_Name;
echo $My_Name;
}
Note: It’s best to use the appropriate API functions when available, instead of modifying global variables directly.

Case Sensitive Variables Names

This code won’t work:

add_action( 'genesis_after_header', 'case_sensitive_variable' );
function case_sensitive_variable() {
$My_Name = "Brad";
echo $my_name;
}

Nor will this:

add_action( 'genesis_after_header', 'uppercase_lowercase_variables' );
function uppercase_lowercase_variables() {
$My_Name = "Brad";
echo $MY_NAME;
}

Using Numbers in Variables

This won’t work:

add_action( 'genesis_after_header', 'case_sensitive_variable' );
function case_sensitive_variable() {
$1my_name = "Brad";
echo $1my_name;
}

This code works:

add_action( 'genesis_after_header', 'numbers_in_variables' );
function numbers_in_variables() {
$my1_name = "Brad";
echo $my1_name;
}

Variables must always start with a $ dollar sign followed by a character or underscore.

To get access to hundreds of code snippets which include the use of variables and answers to your questions, register for membership. Here’s some of the benefits.

Usage of Variables in Genesis

The following code includes 2 variables named $link and $output.

add_action( 'genesis_after_header','variable_output' );
function variable_output() {	
$link = esc_url( 'http://example.com' );  
$output = sprintf( '<a href="%s">' . __( 'Your link text here' ) . '</a>',  $link  ); 	
echo $output;
}

The following code includes 1 variable named $link which is equal to the posts permalink.

add_action( 'genesis_after_header','variable_output' );
function variable_output() {	
$link = esc_url( get_permalink() );
printf( '<a href="%s">' . __( 'Link text' ) . '</a>',  $link  ); 
}

The code above uses printf to output the formatted string.

This code snippet enables you to modify the default value for the original variable already declared in Genesis, via your child themes functions file, using a filter function.

//* Modify comments title text in comments
add_filter( 'genesis_title_comments', 'sp_genesis_title_comments' );
function sp_genesis_title_comments() {
	$title = '<h3>Discussion</h3>';
	return $title;
}

Understanding how variables work will make it easier to understand how to read and write custom filter functions to modify the default value for a variable already included in Genesis or WordPress.


Comments

2 responses to “variable”

  1. This is great info Brad – would love more like this 😉

    1. Brad Dalton Avatar
      Brad Dalton

      Thanks Cathy. More coming soon.

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.