Thursday, 6 April 2017

Properly Filtering Post Content (And Understanding apply_filters)


Now and then, whenever I’m working with pre-existing source code or performing a code audit, I’ll see others modifying the post content of a post object like this: <?php
$post->post_content = $my_content;
At this point, some will drop in and immediately say you’re doing it wrong. I hate that phrase (and it’s even littered throughout the WordPress codebase, but I digress).
There are ways to use other existing WordPress API functions to sanitize the data before setting it equal to the value of the post_content attribute, using those functions isn’t the best way to go about doing it.
Instead, we all should be using apply_filters. The problem? Some don’t know, some don’t know how it works, and some don’t know how to use it. That is it’s not clear how to go about properly filtering post content.
But for those who may fall into any of the categories above, then perhaps this can help.
Properly Filtering Post Content
At this point, I’m assuming that you know about WP_Post, its properties, and how to modify them through various API functions.
The primary issue I’m talking about is making sure the content that will be written
Source: https://managewp.org/articles/14798/properly-filtering-post-content-and-understanding-apply-filters




source https://williechiu40.wordpress.com/2017/04/06/properly-filtering-post-content-and-understanding-apply_filters/

No comments:

Post a Comment