Including Custom Fields Inside Your Post

WordPress/TinyMCE will sometimes remove code that you’d rather it not. For example, have you have tried to add a Google Map to a post without the aid of a plugin? You can’t. The iframes aren’t allowed.

Because of this, and a few other similar instances, I wrote a plugin that would let me easily bypass this filters.

Basically, you’ll just save your “funky” code to a custom fields, then use a shortcode to bring that custom fields back into your post. If your custom field has the name “snippet” than you’d include it in your post with this shortcode [cf name=snippet].

I like this method because it’s not limited to Google maps – I can use for anything. Just be mindful about who has access to this feature, as I’m sure it could be easily abused by someone with malicious intent.

<?php
    $cfsc = new cfsc();
    class cfsc {
        function cfsc() {
            add_shortcode( 'cf', array( &$this , 'grab_field' ) );
        }
        function defaults() {
            return array( 'name' => '' );
        }
        function grab_field($atts) {
            extract(shortcode_atts( $this->defaults() , $atts));
            global $post;
            if (!empty($name)) {
                $info = get_post_meta($post->ID, $name ,true);
                if (!empty($info)) { return $info; }
            }
            return '';
        }
    }
    ?>

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: