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.

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>
share|improve this question

migrated from stackoverflow.com Jun 30 '11 at 8:46

This question came from our site for professional and enthusiast programmers.

    
Could you elaborate on why you need to do this? –  Frank Martin Jun 29 '11 at 16:16
add comment

2 Answers 2

up vote 0 down vote accepted

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>
share|improve this answer
    
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 '11 at 16:06
add comment

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>
share|improve this answer
    
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 '11 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 '11 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 '11 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 '11 at 16:34
    
To be honest I don't think I'm entirely clear on what you need to achieve. –  Frank Martin Jun 29 '11 at 19:44
add comment

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.