0

I have created a custom post type which doesn't use the_content() to display anything in the front-end, just using echo's.

The user enters data into a text-area field in the plugin admin back-end, the CPT does some data manipulation and everything is output through a template file using a shortcode.

How can I get short-codes to fire when a user enters them into a CPT text-area in the backend admin. I can't see any filters being used. Also since it's an admin area text input field, php snippets won't work.

EDIT:

Perhaps a better question is how to implement the_loop properly in a custom post? My code currently;

<?php
/*
Template Name: Article
*/
?>
<article class="article-single">
    <?php 
        echo do_shortcode('[banner]');

        ... loads of shortcodes to build up html...

        echo do_shortcode('[info]');
    ?>
</article>
2
  • You want to include the output of a shortcode in your template/function without using the_content()? Dec 12, 2014 at 17:48
  • Yea, pretty much...
    – myol
    Dec 12, 2014 at 17:50

2 Answers 2

0

You can use the do_shortcode() function for this.

echo do_shortcode( '[your-shortcode including="any" parameters="needed"]' );
1
  • Edited my answer for clarification.
    – myol
    Dec 12, 2014 at 17:59
0

I ended up writing a basic function which used preg_match_all and preg_replace to find my short-code tags and sanitize the content before building the HTML output for the template page.

In my case, only useful if you have the one short tag being used.

function img_strip($str)
{
    $preg = '/\[\bimg\b\](.*?)\[\/\bimg\b\]/';      
    preg_match_all($preg, $str, $matches);  

    if ($matches) {
        foreach ($matches[1] as $match) {

            $parsed = parse_url($match);

            if (empty( $parsed['scheme'])) {
                $match = '//' . ltrim($match, '/');
            }
            $replace = '<img src="' . $match . '" alt="floating-image" class="image">';
            $str = preg_replace($preg, $replace, $str, 1);
        }   
    }

return $str;
}

In future I will look into implementing the loop in my template with the_content().

1
  • 1
    WordPress has built in functions for parsing shortcodes in content, I'm sure they would simplify things if it was more clear what you were trying to accomplish.
    – Milo
    Dec 13, 2014 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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