Unlock your potential with a Pro Membership
Here is where you propel your career forward. Come join us today. Learn more.
PHP 101: Building Strings
Lab: WordPress Custom Taxonomy Basics
Video Runtime: 08:19
We need to build a string from dynamic content. We’ll have a string literal and we need to insert some content into it at the appropriate spot. How do we do it?
There are a couple of techniques you can use in PHP.
Concatenate Strings – Embedded Function
You can concatenate strings together using the dot operator. You can use string literals, variables, and functions. For example, let’s say we need to insert a translated string into a user-defined attribute of a shortcode.
$post_meta .= ' [post_terms taxonomy="department" before="' . __( 'Department: ', 'teambios' ) . '"]';
How does this work?
- The WordPress translation function runs first to get the string value.
- The left string literal is concatenated with the translated “Department: ” string.
- Then it’s concatenated with the closing string literal.
- The new string is concatenated to the string value in the variable.
- The new string is assigned back to the variable.
Watch the video to see how this works and to work through the steps.
Use Placeholders and a Formatted String Pattern
The other strategy is to use placeholders in a formatted string pattern. Then run that through a function that replaces out each placeholder with a string value. PHP provides sprintf()
and printf()
. We’ll use sprintf
.
$post_meta .= sprintf( | |
' [post_terms taxonomy="department" before="%s"]', | |
__( 'Department: ', 'teambios' ) | |
); |
The placeholder is the %s
character string. The PHP construct looks for that placeholder and then matches it up with its argument. It then processes the argument and inserts the value to replace the placeholder.
Watch the video to see how this works.
This approach is prevalent and very popular. It’s my preferred approach.. Why?
- You can see the resultant string pattern without having to figure it out. #EasierToRead
- If you have more than one embed, it’s definitely easier to read and maintain.
Code Challenge
Let’s challenge you. Ready? When this code runs, what is the value of $entry_content
?
function filter_the_entry_footer_post_meta( $html, $taxonomy ) { | |
$html .= sprintf( ' [post_terms taxonomy="%s" before="%s"]', | |
$taxonomy, | |
__( 'Department: ', 'teambios' ) | |
); | |
return $html; | |
} | |
$entry_content = filter_the_entry_footer_post_meta( '[post_categories]', 'department' ); | |
// what is the value of $entry_content? |
-
Check your answer here
Break. Go rest your noodle for a couple of minutes.
Episodes
Total Lab Runtime: 01:30:53
- 1 Lab Introductionfree 08:15
- 2 Custom Taxonomy - The What, Why, and Whenfree 08:32
- 3 Registering a Custom Taxonomypro 09:54
- 4 Configure the Labelspro 14:51
- 5 Bind to Post Typespro 11:55
- 6 Configuring Argumentspro 07:52
- 7 Render Entry Footer Termspro 08:40
- 8 PHP 101: Concatenating Assignment Operatorpro 02:26
- 9 PHP 101: Building Stringspro 08:19
- 10 Test Entry Footer Termspro 03:03
- 11 Flush Rewrite Rulespro 03:56
- 12 Wrap it Uppro 03:10