Post body:

[paragraph value="1"]text text text[/paragraph]
[paragraph value="2"]moretext moretext moretext[/paragraph]

Desired result:

You are reading paragraph 1: text text text

You are reading paragraph 2: moretext moretext moretext

Can that be achieve using the above structure cause I couldn't figure out how to do it using the examples at: https://codex.wordpress.org/Shortcode_API

Can you have enclosing shortcodes and values in them or are these 2 different shortcode types?

Yes. That result can be achieved by single shortcode function. Here is your code-

function paragraph_shortcode( $atts, $content = null ) {
    $pull_atts = shortcode_atts( array(
        'value' => 0
    ), $atts );
    return 'You are reading paragraph ' . wp_kses_post( $pull_atts[ 'value' ] ) . ': ' . do_shortcode($content);
}
add_shortcode( 'paragraph', 'paragraph_shortcode' );

It'll work as you expected. Just input your data as you described-

[paragraph value="1"]text text text[/paragraph]
[paragraph value="2"]moretext moretext moretext[/paragraph]

An d you'll get your desired output.

  • For some reason though Wordpress doesn't interpret it as a paragraph this way? – kat Aug 28 '16 at 16:13
  • Your question is not clear to me. Can you please explain more ? I tested it on my system and it works pretty well. @kat – CodeMascot Aug 28 '16 at 17:20
  • OK. I think I found your problem. In your theme files use <?php the_content();?> or <?php echo apply_filters('the_content',$post_content);?> or <?php echo apply_filters('the_content',$wp_query->post->post_content);?> to render the content. Otherwise the shortcode API will not function. – CodeMascot Aug 28 '16 at 17:47
  • It means that it removes the <p> tags. And even if I add them manually (eg. return '<p>You are reading paragraph ' . wp_kses_post( $pull_atts[ 'value' ] ) . ': ' . do_shortcode($content).'</p>';) apparently they're still not interpreted as paragraphs because I have a function that adds an ad code after X number of paragraphs and it doesn't add the code this way => it can't see them as paragraphs. – kat Aug 28 '16 at 18:38
  • Actually its your theme or your system functionality problem. It has nothing to do with this answer. – CodeMascot Aug 28 '16 at 18:41

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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