Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created April 6, 2017 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/a14b284a07d515fe2b08c62ec48ff8fd to your computer and use it in GitHub Desktop.
Save tommcfarlin/a14b284a07d515fe2b08c62ec48ff8fd to your computer and use it in GitHub Desktop.
[WordPress] Properly Filtering Post Content (And Understanding apply_filters)
<?php
$post->post_content = $my_content;
<?php
$post->post_content = apply_filters( 'the_content', $my_content );
<?php
/**
* Assume $contents is an array of information we want to convert into content for a post.
*/
$new_content = '';
foreach ( $contents as $line ) {
if ( false === strpos( $line, $name ) ) {
$new_content .= $line;
}
}
$post->post_content = $new_content;
<?php
/**
* Assume $contents is an array of information we want to convert into content for a post.
*/
$new_content = '';
foreach ( $contents as $line ) {
if ( false === strpos( $line, $name ) ) {
$new_content .= $line;
}
}
$post->post_content = apply_filters( 'the_content', $new_content );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment