Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I keep seeing this pattern, and wonder what it's for, how WP uses it internally:

if ( empty($post) ) {
        if ( isset($GLOBALS['post']) )
            $_post = & $GLOBALS['post'];
        else
            return $null;
    } elseif ( is_object($post) && empty($post->filter) ) {
        _get_post_ancestors($post);
        $_post = sanitize_post($post, 'raw');
        wp_cache_add($post->ID, $_post, 'posts');
    } else {
        if ( is_object($post) )
            $post_id = $post->ID;
        else
            $post_id = $post;

The specific line in question is is_object($post) && empty($post->filter). I've seen this filter property associated with other objects as well. Wondering what it does and how it can be used?

EDIT

I'm hoping to get more input on this! Unfortunately, a Google search with "filter" in it only turns up Filter hooks etc. This is a tough one to get independent info on. Buehler?

share|improve this question
    
I recently came across this. Scribu also tells where this is used during tests. –  kaiser Sep 30 '11 at 9:08
    
@Rarst I'm not sure how this is an exact duplicate at all. I'm looking for an explanation of what the filter property on some internal objects are, how it's used and how it can be leveraged. The bug report question you linked to definitely makes mention of the filter property, but doesn't do anything as far as explaining it. –  Tom Auger Sep 30 '11 at 16:15
    
Not an exactly duplicate. The other question was running into an issue where $post->filter needed to be set. It didn't explain what it was, why it was there, or how it's used in core. I agree with the 2 votes cast to re-open. –  EAMann Sep 30 '11 at 16:37
    
That seemed like more extensive coverage of the topic, I might have misjudged. :) Anyway reopened so all well. –  Rarst Sep 30 '11 at 17:26
add comment

2 Answers

up vote 4 down vote accepted

The $post->filter contains the context under which the contents of the post have been filtered. Context could be something like "display" indicating the post is meant to be displayed on the page.

This is done by the sanitization functions, to indicate under what context the post data was sanitized. Different contexts get different types of sanitization. "raw", for example, is unsanitized data. "display" has probably gone through the esc_html function, or whatever is relevant for that context. The sanitize_post() function uses this to check for pre-existing sanitizations, in order to prevent double escaping things and so forth.

One other use for the $post->filter is in the get_sample_permalink() function. You'll find that it sets the $post->filter = 'sample' before calling get_permalink(). The use of "sample" here indicates to the get_permalink function, and to code below it, that the purpose of this call is to create a sample permalink for displaying on the edit post screen. This will change the way some of the functions below it work.

share|improve this answer
    
Thanks @Otto so much for taking the time to clear this up. Makes perfect sense now. Would it be discouraged to f$%@ with this filter value for one's own purposes (within a plugin), following the same philosophy of providing context for the most recent filter on the post? I can't think of a clear example right now, just wondering whether this should be considered "read only" for plugin/theme devs? –  Tom Auger Oct 5 '11 at 14:35
    
Oh, one more thing: in my code example above it tests to see whether filter is empty. If it is, it sanitizes the post, passing "raw" as the parameter. Is it safe to say that if the filter is set to "raw" it's the same thing as not having the filter set at all: ie, the post is as yet unsanitized? –  Tom Auger Oct 5 '11 at 14:37
2  
Essentially, you'd call sanitize_post($post, $context) with your context. It passes each field in the post down to sanitize_post_field($field, $post->$field, $post->ID, $context) with your context as well. Built in contexts are: 'raw', 'edit', 'db', 'display', 'attribute' and 'js'. The default is "display", and it essentially runs apply_filters("post_{$field}", $value, $post_id, $context) over the field. You can hook in here and detect your custom context and act accordingly. The filter value automatically becomes the context when sanitize_post is done. –  Otto Oct 5 '11 at 18:21
    
Yah man. Thanks @Otto. –  Tom Auger Oct 5 '11 at 20:09
add comment

@Shaan: I don't think that Tom was looking for a description about the WP Plugin API. He is looking for a description of the object property filter which you get as a result of WP_Query. For example if you use get_posts():

stdClass Object
(
    // other post properties
    [filter] => raw
)

I don't know whether and where this property is used inside WordPress. After looking around in the sourcecode, I think this isn't used anywhere at the moment.

Just speculating here. The default value is raw for all posts so far. It could be that the core developers wanted to add a filtering on per post base. For example, if you want to allow only certain HTML elements inside the post content, you could create a filter sanitizeHtml, attach it to the post and WordPress will do the rest for you.

share|improve this answer
    
It's actually used quite a bit, usually as part of a test to see whether it's empty or not. I'm not sure beyond that whether the values are used. I'm wondering how/when it gets populated and what it means if it's not. –  Tom Auger Sep 30 '11 at 16:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.