
如何将PHP代码添加到WordPress文章或页面
sanitize_post ( $post, $context = 'display' )
sanitize_post: 这是一个WordPress的函数,它对一个文章对象进行净化。它用于验证和净化文章的数据,如标题和内容: 这个函数有一个参数,就是要净化的文章对象。
对每个文章字段进行净化。
如果上下文是”raw”,那么文章对象或数组将得到最小的整数字段的净化处理。
function sanitize_post( $post, $context = 'display' ) { if ( is_object( $post ) ) { // Check if post already filtered for this context. if ( isset( $post->filter ) && $context == $post->filter ) { return $post; } if ( ! isset( $post->ID ) ) { $post->ID = 0; } foreach ( array_keys( get_object_vars( $post ) ) as $field ) { $post->$field = sanitize_post_field( $field, $post->$field, $post->ID, $context ); } $post->filter = $context; } elseif ( is_array( $post ) ) { // Check if post already filtered for this context. if ( isset( $post['filter'] ) && $context == $post['filter'] ) { return $post; } if ( ! isset( $post['ID'] ) ) { $post['ID'] = 0; } foreach ( array_keys( $post ) as $field ) { $post[ $field ] = sanitize_post_field( $field, $post[ $field ], $post['ID'], $context ); } $post['filter'] = $context; } return $post; }