0

I simply want to create a Shortcode for a simple styled DIV-Wrapper.

At Sites in WordPress i want to add someting like this:

[quotehead]Headline text[/quotehead]
...a normal blockquote from wordpress...

The Output should be:

<div class="block_header">Headline text</div>

My function inside functions.php (in childtheme) has the following content:

/**
 * Function block_header_start
 **/
function block_header_start()
{
    return ('<div class="block_header">');
}

/**
 * Function block_header_end
 **/
function block_header_end()
{
    return ('</div>');
}

add_shortcode('blockhead','block_header_start');
add_shortcode('/blockhead','block_header_end');

But the output looks like that:

<div class="block_header">
    <p>&nbsp;</p>
    <blockquote>...my content of blockquote...</blockquote>
</div>
  1. Issue: The Words inside the new Wrapper doesn't appears in Frontend!
  2. Issue: The Wrapper includes the following blockquote, but the closing [/quotehead] is in front of the blockquote in Backend.?!

Is it possible to use only one Shortcode, and wrap the Content that follows, till the end of that line in one function..

Something like the leading # for headers in here or for Markdown files?

Or just the wrapping variant, that works?

Where is my failure?

2

Shortcodes come in two flavors:

[shortcodename param1 = value, ..., param-n = value]

and

[shortcodename param-1 = value, ..., param-n = value]some content[/shortcodename]

In both cases, add_shortcode('shortcodename', 'functionname') function is identical.

The functionname() function's parameters, determine which flavor will be used:

functionname($atts) is for shortcode with no closing tag, and
functionname($atts, $content) is for shortcode with an arbitrary content, between opening, and closing tags.

In our case, the second type should be used:

function my_block_header($atts, $content) {
    return '<div class="block_header">' . $content . '</div>';
}
add_shortcode('blockhead', 'my_block_header');

Use as:

<blockquote>
    [blockhead]Headline text[/blockhead]
    ... quoted text ...
</blockquote>

Note: second add_shortcode, in your code is unnecessary, and incorrect, as shortcode tag cannot start with / character, and one shortcode is enough, to return a division, with both, opening, closing tags. Also, both shortcode functions, in your code, lack mandatory parameters.

  • for what is $atts ? $content is clear ;) .. – MonTea Apr 3 '18 at 13:41
  • 1
    $atts is an array of shortcode's parameters. It has to be specified, even, if not used. – Frank P. Walentynowicz Apr 3 '18 at 13:46
  • thank you.. it works.. but i have the header block outsite the blockquote.. for different styling ;)... – MonTea Apr 3 '18 at 13:47
  • 2
    @FrankP.Walentynowicz nice but no upvote, you never explained what the OP did incorrectly, and how to fix it, your answer could just be copypaste with no learning :( – Tom J Nowell Apr 3 '18 at 14:13
  • 2
    @TomJNowell you are right. Quite often, I forget that the answer is not for one person, but for broader audience, with different levels of experience. I'm going to update my answer. +1 for your comment. – Frank P. Walentynowicz Apr 3 '18 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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