Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Where you can see it: http://themeforward.com/demo2/functional-codes

The first button is my code, as it should appear. The second button is within <pre><code></pre></code> tags to show users how I made the button appear. But instead of showing the code, the button is generated.

How can I get WordPress' code tag to work for ALL code? HTML, PHP, and CSS. Something I can paste in my functions would be preferred.

The actual code for the button (a shortcode)

[pdf href="http://www.constitution.org/usdeclar.pdf"]Declaration of Independence[/pdf]
share|improve this question
    
I don't understand your example. Button as in what? You have multiple elements of different kinds on that page that are buttons. And what shortcode has to do with it? –  Rarst Oct 16 '11 at 20:42

4 Answers 4

up vote 1 down vote accepted

You can display a shortcode by using two (or three) square brackets at the open and close, e.g.

[[[pdf href="http://www.constitution.org/usdeclar.pdf"]Declaration of Independence[/pdf]]]

In my experience, a standalone shortcode just needs two whereas one that wraps needs three on each end, even though I've seen it said that it should work just two in either case.

share|improve this answer

You can convert the tags to HTML so it will display as you want. There are some generators online for this. Here is a link to one: http://web.forret.com/tools/html.asp

Just paste your code into the top box and click convert to HTML. The generated output will allow you to place it in HTML view and it will display how you're wanting.

share|improve this answer
    
That doesn't actually work in this situation, thanks for trying though. –  AndrettiMilas Oct 12 '11 at 19:11
1  
HMM? Maybe a plug-in? kpumuk.info/projects/wordpress-plugins/codecolorer –  Jeremy Jared Oct 12 '11 at 19:43
    
I'd rather have it built into the theme. –  AndrettiMilas Oct 12 '11 at 19:49

I use a simple custom shortcode to return htmlentities

put this in you functions.php file:

add_shortcode('better_code','better_code_handler');
function better_code_handler($attr,$content){
    return htmlentities($content);
}

Usage:

[better_code] 
[pdf href="http://www.constitution.org/usdeclar.pdf"]Declaration of Independence[/pdf]
[/better_code]
share|improve this answer

This plugin allows you to create Custom Fields with any kind of content, name them like mic:name and then, inside posts, when you use {mic:name} or [mic:name], it is replaced by the Custom Field, only after wpautop kicks in so it's not defaced anymore. (priority 11)

micev:name works the same (use {micev:name} or [micev:name]) but the output is eval()-ed before inserted into content. Use with caution... as it allows actual PHP code to run inside posts.

/*
Plugin Name: [Post] Metas In Content
Description: Enables <code>[mic:MIC_Name]</code> and <code>[micev:MIC_Name]</code> inside content.
Version: 0.0.7
Author: EarnestoDev
Author URI: http://www.earnestodev.com/
*/
// ---------------------------------------------------------- //
class Meta_In_Content{
    public function __construct(){
        register_uninstall_hook(__FILE__, array($this, 'uninstall'));
        // Run after the WordPress wpautop and such killers of code
        add_filter('the_content', array($this, 'the_content'), 11);
    }
    public function uninstall(){
        global $wpdb;
        $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE (`meta_key` LIKE 'mic:%') OR (`meta_key` LIKE 'micev:%');");
    }
    public function the_content($content){
        global $post, $wpdb; $postID = $post->ID;
        // mic:NAME metas are not eval()-ed before inserted in the content
        $ph_metas = $wpdb->get_col("SELECT DISTINCT(`meta_key`) FROM {$wpdb->postmeta} WHERE `post_id`={$postID} AND `meta_key` LIKE 'mic:%';");
        foreach($ph_metas as $ph_meta){
        $value = get_post_meta($postID, $ph_meta, true);
        if(!is_string($value)) continue;
            $content = str_replace(array("[{$ph_meta}]", "{{$ph_meta}}") , array($value, $value), $content);
        }
        // micev:NAME metas are eval()-ed before inserted in the content
        $ph_metas = $wpdb->get_col("SELECT DISTINCT(`meta_key`) FROM {$wpdb->postmeta} WHERE `post_id`={$postID} AND `meta_key` LIKE 'micev:%';");
        foreach($ph_metas as $ph_meta){
        $value = get_post_meta($postID, $ph_meta, true);
            if(!is_string($value)) continue;
            ob_start();
        eval(sprintf(' ?>%s<?php ', $value));
        $value = ob_get_clean();
            $content = str_replace(array("[{$ph_meta}]", "{{$ph_meta}}") , array($value, $value), $content);
        }
        return $content;
    }
};
// ---------------------------------------------------------- //
// Instantiate the Object once
$Meta_In_Content = new Meta_In_Content();
// ---------------------------------------------------------- //

Have fun!

Just save this in a plugin in /wp-content/mu-plugins. To disable the eval() functionality, just remove from // micev:NAME metas to right before the return.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.