Accessing the post data inside filters

Hire a WordPress Expert on Codeable
Updated On: June 23rd, 2020 0 Comments

Let’s just say you want to replace those three dots (…) with a read more link. And, This is a perfect real-world scenario.

We can actually do this by accessing the global $post variable and other template tags that we usually use inside the loop.

Yep! If it is a filter hook that handles the data that belongs to a particular post, you can access every piece of that post inside that filter with the help of the template tags that we usually inside the loop.

For example:


/*
 * Remove brackets at the end of each excerpt and read more link
 */
function nd_dosth_custom_excerpt_more() {
    return '' . ' Read More »' . '';
}
add_filter( 'excerpt_more', 'nd_dosth_custom_excerpt_more' );

The above code might look complicated for a beginner PHP developer, but it is just some basic PHP concatenation.

Powerful, right? 

Anyway, I don’t want this filter based read more like, I am reverting it back to the three dots I love. 

In the next lesson, we will fix a problem related to the categories located at the bottom of every blog post.

Leave a Reply

Your email address will not be published. Required fields are marked *