最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Help using ShortCodes to style whole chunks of the post

programmeradmin1浏览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?

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?

Share Improve this question asked Jul 11, 2019 at 7:40 ducklordducklord 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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/"]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' );
发布评论

评论列表(0)

  1. 暂无评论