0

Recently my sites got hacked, forcing me to start rebuilding them from scratch. It's been ages since I spent time on them, so I don't even remember the plugins I'd installed and used. One of them, though, allowed the easy creation of ShortCodes and the "mapping" of text or, even better, HTML and CSS through them. I had used this extensively to style bits and pieces of the posts like in-post Notes, marked as...

[Note]Blah-blah...[/Note]

...adding a custom class "around" the text to allow for extra styling, or...

[AuthorOpinion=X]Blah-blah...[/AuthorOpinion]

...that slapped a whole bunch of DIVs and, depending on author class, a different image as well as "his/her opinion on some matter" (the "blah-blah's")...

...but, also...

[PostIntro=BlahBlah]

...that was "self-contained" and didn't need "closing", with the "Blah-Blah" styled in a more bold way. And a YouTube one. And some other "boxes" of the kind.

Now, I don't remember what plugin that was but, at the same time, since this functionality is supposedly easy to implement directly through WP, I'd prefer doing it "the manual way". Problem is, I've spent the better part of the last six hours copy-pasting code I couldn't make to work, for I suck @PHP.

I managed to make this work:

function postnote_function( $atts, $content = null ) {
    return '<div class="note">' . $content . '</div>';
}
add_shortcode('note', 'postnote_function');

...but, for the life of me, I can't make the aforementioned "[Postintro=Blahblah]" work. Every example I saw uses arrays "to feed/pass parameters to functions", but I don't need different "parameters": I need the first paragraph. And, for some reason, no matter how much I prodded the code, it doesn't work for me.

At the moment, my latest non-working copy-paste was this:

function postintro_shortcode( $atts , $content = null ) {

    // Attributes
    $atts = shortcode_atts(
        array(
            'type' => '',
            'icon' => '',
        ),
        $atts,
        '[postintro]'
    );

    // Return custom embed code
    return '<div class="postintro">' . $content . '</div>';

}
add_shortcode( '[postintro]', 'postintro_shortcode' );

Any help? Anyone?

1 Answer 1

0

EDIT 2

Multiple unnamed parameters, starting with equals sign.

function postintro_bb_shortcode( $atts ) {
  // remove equals sign from first unnamed param
  if ( isset( $atts[0] ) && strpos( $atts[0], '=' ) !== false ) {
    $atts[0] = substr( $atts[0], 1 );
  }
  // Param array to string
  $intro = is_array( $atts ) ? implode( ' ', $atts ) : '';
  return '<div class="postintro">' . esc_html( $intro ) . '</div>';
}
add_shortcode( 'postintro_bb', 'postintro_bb_shortcode' );

[postintro_bb=Vivamus sit amet turpis ultricies lorem dignissim sollicitudin id in ex]

Unnamed parameter with closing tag

function source_shortcode( $atts, $content = null ) {
  // remove equals sign from first unnamed param
  if ( isset( $atts[0] ) && strpos( $atts[0], '=' ) !== false ) {
    $atts[0] = substr( $atts[0], 1 );
  }
  return '<a href="' . esc_url( $atts[0] ) . '">' . esc_html( $content ) . '</a>';
}
add_shortcode( 'source', 'postintro_b_shortcode' );

[source="http://www.example.com/"]test[/source]


EDIT

Here's few ways how to define and use custom shortcodes.

Shortcode with closing tag

function postintro_shortcode( $atts , $content = null ) {
  return '<div class="postintro">' . $content . '</div>';
}
add_shortcode( 'postintro', 'postintro_shortcode' );

[postintro]test[/postintro]

Self closing shortcode tag.

Apparently unnamed parameters work too.

function postintro_b_shortcode( $atts ) {
  $intro = ! empty( $atts[0] ) ? $atts[0] : '';
  return '<div class="postintro">' . $intro . '</div>';
}
add_shortcode( 'postintro_b', 'postintro_b_shortcode' );

[postintro_b blahblah]

Self-closing shortcode tag with named parameter.

function postintro_c_shortcode( $atts ) {
  $intro = ! empty( $atts['intro'] ) ? $atts['intro'] : '';
  return '<div class="postintro">' . $intro . '</div>';
}
add_shortcode( 'postintro_c', 'postintro_c_shortcode' );

[postintro_c intro="afdfds"]


Original answer

The shortcode API is freaking out, because your shortcode name has brackets. Change

add_shortcode( '[postintro]', 'postintro_shortcode' );

to

add_shortcode( 'postintro', 'postintro_shortcode' );
16
  • Nope. Didn't work :-(
    – ducklord
    Jul 11, 2019 at 8:36
  • @ducklord I updated my answer with more examples. I tested them all on my local WP install and they work. Can you get your code working with these examples? Jul 11, 2019 at 11:40
  • Something is very, very wrong in my case...
    – ducklord
    Jul 12, 2019 at 20:54
  • I tried this piece of your code: function postintro_b_shortcode( $atts ) { $intro = ! empty( $atts[0] ) ? $atts[0] : ''; return '<div class="postintro">' . $intro . '</div>'; } add_shortcode( 'postintro_b', 'postintro_b_shortcode' ); ...and typed "This is a test" after a "postintro box", just like in your example. It returned only the first "this" ignoring the rest of the text. And, since for some reason I can't grok your code at the moment, what's the part that makes the shortcode "need closing" or being-a-self-closing-specific-word?
    – ducklord
    Jul 12, 2019 at 21:08
  • I should add that I'm looking specifically for a mix of your solutions, where I'll be able to add classes to "[postintro=blahblah]" shortcodes. The reason being that I've got thousands of posts in my blog already using this specific shortcode, since my before-I-was-hacked-and-had-to-scrap-everything era. You gave an example where you define "the named parameter" ...(that = the text I type after it, if I understood your examples correctly), but how can I have the shortcode itself as "the named parameter"? As in, having an "=" immediately after the self-enclosed shortcode?
    – ducklord
    Jul 12, 2019 at 21:15

Your Answer

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

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