One more common request is to make the “Read More” text in the archive pages editable. Let’s just do that in this lesson.
Go ahead and put the following code at the end of the
// Setting for Read More text. $wp_customize->add_setting( 'nd_dosth_readmore_text', array( 'type' => 'option', 'default' => __( 'Read More ', 'nd_dosth' ), 'sanitize_callback' => 'sanitize_text_field', 'transport' => 'refresh', ) ); // Control for Read More text $wp_customize->add_control( 'nd_dosth_readmore_text', array( 'type' => 'text', 'priority' => 10, 'section' => 'nd_dosth_text_options', 'label' => 'Read More text', 'description' => 'Text put here will be as the text for Read More link in the archives', ) );
You already know what’s happening in the above code. It just adds a new Control and Setting for “Read More” text inside:
Customize Panel -> Theme Options -> Text Options
Nothing new, right?
But if you notice the
add_setting() function in the above code, I added an extra argument called “type” to its configuration array and I have provided “option” as its value.
The “type” argument accepts two values.
- theme_mod (default)
The default value for the “type” argument is “theme_mod”. This is why we did not provide this argument for the “Copyright Text” Setting and we used the
get_theme_mod() function to retrieve the Setting value.
But I want the “Read More” Setting to be the type of “option”, so I have specified the “type” without omitting it.
The importance of the “type” argument is that it tells WordPress how to store a particular Setting’s value inside the WP_OPTIONS table. That’s all.
This will make more sense if you take a look at how “Copyright Text” Setting is store inside the WP_OPTIONS table.
See? Not just “Copyright Text” Setting, All the Settings inside Customize Panel whose type is “theme_mod” are getting store inside a single row of the table called “theme_mods_nd-dosth”.
If you have some good amount of knowledge about Database Performance, you know this is not good for performance.
This is where the Setting type of “option” comes in.
You know the Settings Screen inside the Admin Dashboard, right?
Along with some internal WordPress settings, The values of all the above Settings are stored the WP_OPTIONS as well, but each Setting gets its own row!
So, if we set the “type” of a particular Customization Setting to “option”, this Setting’s value will get its own inside the WP_OPTIONS table and there will be a performance boost when we access this Setting’s value for outputting in the frontend.
Common, let’s test this out! Go back to the Text Options:
Admin Dashboard -> Appearance -> Customize -> Theme Options -> Text Options
And change the text of “Read More Text” Setting to “Continue Reading” and hit the Publish button to save changes to the Database.
If we now check out the WP_OPTIONS table, we can indeed see that “Read More Text” Setting got its own row.
Trust me, This is a really good thing.
Anyway, now let’s retrieve and output “Read More Text” Setting inside our Blog posts Index and archive pages.
If you remember, we are outputting the “Read More” link from the
parts/blog-index.php template file. We are using this template part to render the blog posts on the Blog post index page, Homepage, and archive pages.
Seeing the advantage of writing modular template parts yet?
Go ahead and open up the
blog-index.php template file and replace the following code:
<a class="read-more-link" href="<?php the_permalink(); ?>"><?php _e( 'Read More', 'nd_dosth' ); ?></a>
<a class="read-more-link" href="<?php the_permalink(); ?>"> <?php echo get_option( 'nd_dosth_readmore_text', __( 'Read More', 'nd_dosth' ) ); ?> </a>
Because we have the Setting type to be “option”, we can no longer use the
get_theme_mod() function to retrieve the value of the Setting. Instead, we have to use
get_option() function accepts the same two parameters that the
get_theme_mod() function accepts. Setting ID and a Default Value.
If you now go to the Blog posts Index, Homepage or any of the archive pages, on individual articles, you should see the text “Continue Reading” instead of “Read More”.
Slick, Isn’t it?
Here is one more secret, the
get_theme_mod() function internally uses the
get_option() function. So you can still retrieve and output a Setting with “theme_mod” type using the
get_option() function as well. It saves one internal function call for WordPress.
Common, go ahead and replace the
get_theme_mod() function inside the
footer.php file with the
'%s. %s © %s',
get_option('nd_dosth_copyright_text', __( 'All Rights Reserved', 'nd_dosth' ) ),
date_i18n( 'Y' )
Everything still works the same. From here onwards, for the rest of the module, We will use
get_option() function instead of
In the next lesson, we will learn how to create a “Checkbox” Control and how to sanitize it.