0

Is it possible to filter the output of either the content or excerpt so that I only get the the shortcode output and so that if a user enters more than the shortcode the rest of the content for that page would be stripped.

1 Answer 1

2
function shortcode_only_wpse_96114($content) {
  $regex = get_shortcode_regex();
  preg_match_all('/'.$regex.'/',$content,$matches);
  if (!empty($matches[0])) {
    $content = do_shortcode(implode(' ',$matches[0]));
  }
  return $content;
}
add_filter('the_content','shortcode_only_wpse_96114',1);

That should check for any registered shortcodes and if present completely replace the post content with the output of the shortcode(s), removing any other content. If no shortcodes are found the post content gets returned as normal.

Barely tested. Possibly buggy. Caveat emptor. No refunds.

Reference

http://codex.wordpress.org/Function_Reference/get_shortcode_regex

1
  • Seemed to work fine. I ended up discovering I was going about my task the complete wrong way so I haven't tested it extensively myself either Commented Apr 17, 2013 at 17:20

Your Answer

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

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