Skip to content

Instantly share code, notes, and snippets.

@dartiss
Created April 23, 2018 10:35
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 dartiss/d4240720c69ce8f32860eb7096378dd5 to your computer and use it in GitHub Desktop.
Save dartiss/d4240720c69ce8f32860eb7096378dd5 to your computer and use it in GitHub Desktop.
<?php
function embed_code( $content ) {
global $post;
$i = 1;
while ( $i < 6 ) {
$code = 'CODE' . $i;
$html = get_post_meta( $post->ID, $code, false );
$content = str_replace( '{{' . $code . '}}', $html[0], $content );
$i++;
}
return $content;
}
add_filter( 'the_content', 'embed_code' );
@dartiss
Copy link
Author

dartiss commented Apr 23, 2018

Super-simple method of embedding code into a WordPress post without the editor modifying it.

Once the above code is in place, add a custom field named CODEx, where x is a number between 1 and 5. The field value is your code to be embedded. Then, in your post, add {{CODEx}} where you wish the code to appear (again, replace the x with the appropriate number).

** VIP full coding-standards compliant **

@dartiss
Copy link
Author

dartiss commented Apr 23, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment