You’ve probably seen code in your files which looks like this:
printf('%s World', $var );
This code includes a function named printf()
which in most cases is more efficient than using the echo or print functions.
To learn more about using PHP code in WordPress & Genesis, you’ll need to understand how the printf
function works.
Video Guide
Coming Soon!
Printf generally includes a %
percentage sign with an s
for string like this:
Printf can be used in replace of print
or echo
so rather than use this:
or
Instead, you can use printf like this:
printf('%s World', "Hello" );
These %
percentage signs with a s
work like placeholders which are converted when the code is executed. In the above case %s
is replaced with the text Hello.
Here’s the full working code you can test in your child themes functions file:
add_action('loop_start','printf_example1' );
function printf_example1() {<br />
printf('%s World', 'Hello' );
}
You can then replace your text ‘Hello’ with a variable so the value for your $text
variable is now equal to Hello:
The full working code you can test in your child themes functions file would be this:
add_action('loop_start','printf_example2');
function printf_example2() {<br />
$text = 'Hello';
printf('%s World', $text );
}
In WordPress themes, you’ll find code which uses printf
which you can paste into your child themes functions.php file to see how it works:
add_action('loop_start','printf_example3');
function printf_example3() {<br />
printf( '<a href="%s">' . esc_html__( 'Your link text here', 'text-domain' ) . '</a>', 'http://example.com' ) );
}
The code above uses printf
with one instance of %s
which is replaced by the URL http://example.com
when the function is executed.
The above code can also be written like this:
add_action('loop_start','printf_example4');
function printf_example4() {
$link = esc_url( get_permalink() );
$text = "Value for Your Text Variable";
printf( '<a href="%s">%s</a>', $link, $text );
}
In this case, we have replaced the URL with a variable named $link
. We have also replaced the text with a variable named $text
.
The value for the $link
variable is equal to the posts permalink rather than the example.com URL and the value for the $text
variable is equal to the text.
You’ll also notice each parameter in the string is separated by a comma like this:
When the code executes, the 1st placeholder %s
is replaced with the 1st parameter which in this case is the $link
variable which equals get_permalink();
After the 1st placeholder is replaced, the 2nd, 3rd and so on are replaced step by step in the order they occur in the string, separated by a comma.
Note: The order of the placeholders %s must match the order of the arguments in the code.
You can also write the code as follows:
add_action('loop_start','printf_example5');
function printf_example5() {
$text = "Value for Your Text Variable";
printf( '<a href="%s">%s</a>', esc_url( get_permalink() ), $text );
}
This way the 1st parameter is equal to the get_permalink
function rather than the value for the $link
variable as in example 4.
printf & sprintf
Printf
is used to output the formatted string. Example:
add_action('loop_start','printf_example6' );
function printf_example6() {<br />
printf('%s World', 'Hello' );
}
Sprintf is returned as the value for a variable to be used later then the variable can be output using echo
or printf
. Example:
add_action('loop_start','printf_example7' );
function printf_example7() {<br />
$output = sprintf('%s World', 'Hello' );
echo $output;
}
Ask Questions
Members can ask questions & get answers.
Related PHP Code