1

Is there a way to wrap a wordpress shortcode around a div in a template file? Here is my code:

<div id="subscription">
<?php do_shortcode('[subscribe]');?>
<p>This is subscription text</p>
</div>

I've also tried

<div id="subscription">
<?php do_shortcode('[subscribe]');?>
<p>This is subscription text</p>
<?php do_shortcode('[/subscribe]');?>
</div>
1
  • Could you elaborate on why you need to do this?
    – Frank Martin
    Jun 29, 2011 at 16:16

2 Answers 2

1

If by template file you mean a php file in the themes folder, you can call do_shortcode to have the shortcode processor do its magic on your div.

EDIT: not entirely sure I understand what you want, but you could try this:

<div id="subscription">
<?php do_shortcode('[subscribe]'.call_php_function().'[/subscribe]');?>
</div>
1
  • Yes thats exactly what I meant, and I've tried to insert it in the div but I dont think I'm doing it right. I'll past the code above. Thanks @Femi
    – Carlos Rios
    Jun 29, 2011 at 16:06
3

I'm pretty sure the above code should be

<?php do_shortcode('[subscribe]<p>This is the subscription text</p>[/subscribe]'); ?>

Then you can use the content in the shortcode function like so:

<?php
function subscribe_shortcode($atts, $content = '', $code = NULL) {

return '<div id="whatever-etc">'.$content.'</div>';
}
add_shortcode('subscribe', 'subscribe_shortcode');

?>

The above would output:

<div id="whatever-etc">
<p>This is the subscription text</p>
</div>
6
  • Thanks @Frank Martin! This is what I was also thinking, but the problem is the real code that Im using has some Php inside of my subscription div. is there a way that I could work with this?
    – Carlos Rios
    Jun 29, 2011 at 16:25
  • This code is being used to hide and show content for premium users on a website. If they are logged in as a premium member it will show the content that is inside of the shortcode, otherwise it will say they can purchase it
    – Carlos Rios
    Jun 29, 2011 at 16:26
  • If it's in the template file is there any need for the shortcode at all? Can't you just put a conditional statement around the content?
    – Frank Martin
    Jun 29, 2011 at 16:29
  • That was my next option, but the plugin that I'm using to show and hide content targets what to hide with a shortcode. I know its pretty dumb but its normally used to hide content that is written within wordpress but for my case its different. Would you have an idea of how I could use a conditional for this?
    – Carlos Rios
    Jun 29, 2011 at 16:34
  • To be honest I don't think I'm entirely clear on what you need to achieve.
    – Frank Martin
    Jun 29, 2011 at 19:44

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.