0

I want to create a shortcode, but it creates empty <p> tags in the output.

I'm using the following code to avoid that problem, however I'm not sure if this is correct way. I have read that if shortcodes are added incorrectly, they create problems with WordPress core functionality, so please advice if the following method is correct.

function run_my_shortcode( $content ) {
    global $shortcode_tags;
    $original_shortcode_tags = $shortcode_tags;
    remove_all_shortcodes(); 
    add_shortcode( 'box', 'my_shortcode' ); 
    $content = do_shortcode( $content );
    $shortcode_tags = $original_shortcode_tags; 
    return $content;
} 
add_filter( 'the_content', 'run_my_shortcode', 7 );

//output the shortcode content
function my_shortcode( $atts, $content = null ) {   
    extract(shortcode_atts(array(
        'title' => ''
       ), $atts));

    $output = '';
    $output .= "<div class='box' >";

    if (!empty($title)){
        $output .= '<h3>'.$title.'</h3>';
    }

    $output .= '<p>'. do_shortcode( $content ).'</p>';
    $output .= '</div>';

    return $output; 
}
add_shortcode( 'box', 'my_shortcode' );
0

The codex explain, I think, very well the creation of shortcodes

http://codex.wordpress.org/Shortcode_API

A very basic shortcode

function foobar_func( $atts ){
    return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );

To use it, you just need to [foobar]

The first parameter on add_shortcodeis the name of the shortcode

function bartag_func( $atts ) {
    extract( shortcode_atts( array(
        'foo' => 'something',
        'bar' => 'something else',
    ), $atts ) );

    return "foo = {$foo}";
}
add_shortcode( 'bartag', 'bartag_func' );

To use it, just use [bartag foo="foo-value"]

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.