Skip to content

Instantly share code, notes, and snippets.

@leowebguy
Last active November 24, 2015 20:30
Show Gist options
  • Save leowebguy/d76f2a41423a51217293 to your computer and use it in GitHub Desktop.
Save leowebguy/d76f2a41423a51217293 to your computer and use it in GitHub Desktop.
A simple php function to add a meta box (add_meta_box) to posts/pages and write it on the footer (wp_footer).
<?php
/******
Simple function to add custom content to posts/pages on WordPress.
Just add to functions.php
******/
function custom_css_add() {
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
add_meta_box(
'custom-css',
'Custom CSS',
'custom_css',
'page', // post type
'normal', // options: side
'low' ); // options: default, high
}
function custom_css_save( $post_ID ) {
if( isset($_POST) ) {
update_post_meta( $post_ID, 'custom-css', $_POST['custom-css'] );
}
}
add_action( 'add_meta_boxes', 'custom_css_add' );
add_action( 'save_post', 'custom_css_save' );
function custom_css( $post ) {
$cwid = get_post_meta( $post->ID, 'custom-css', true ); ?>
<p>
<textarea name='custom-css' id='custom-css' cols="120" rows="10"><?php if( $cwid ) { echo $cwid; } ?></textarea>
</p><?php
}
function custom_css_write() { ?>
<style>
<?php echo get_post_meta( get_the_ID(), 'custom-css', true ); ?>
</style><?php
}
add_action( 'wp_footer', 'custom_css_write', 100 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment