0

I have been working on wordpress website. Few days back i changed theme. My previous them was supporting wordpress shortcode. I was using [box type=”shadow”] to creates a shadow box. E.g

[box type=”shadow”] Lorem Ipsum is simply dummy text of the printing and typesetting industry.[/box] and outuput had been displaying like shown in image below

enter image description here

I don't want to use any plugin for this. I wanna do this with pure code.

1
  • For future reference, you are expected to do your research and make an attempt at the problem yourself before posting here. This isn't a code-writing service. Take a look at how to ask a good question?. In future, make sure your question includes a specific coding question and the code you have tried in a Minimal, Complete, and Verifiable example so we can help. Commented Mar 8, 2019 at 19:22

2 Answers 2

1

The shortcode is quiet popular in WordPress.

Here is how it works.

function boxShow($atts, $content = null ){
    //default values
    $option = shortcode_atts( array(
         'type' => '',
    ), $atts );

    ob_start(); 

    $class = $option[ 'type' ] ? 'shadow' : 'normal';

    //HTML goes here
    ?>
    <div class="box <?php echo $class; ?>"><?php echo $content; ?></box>

    <?php
    $output = ob_get_clean();
    return $output;
}
add_shortcode( 'box', 'boxShow' );

You can control the design with the class defined with the type of box.

You can use on text editor with this format

[box type="shadow"]Your content here[/box]

If you want to use as a code level use this format:

<?php echo do_shortcode( '[box type="shadow"]this is text[/box]' ); ?>

For more understanding about WordPress Shortcode API

Sign up to request clarification or add additional context in comments.

8 Comments

I added same code. But nothing works. Could you please change and write full code and i will add code in function.php because i have not created any shortcode before and have not knowledge about this. I wanna output like this i.sstatic.net/SPSSo.png
Sorry that i have forgot to add_shortode. Please see my edit on my answer.
I replaced this code. But nothing works. Here is output [box type=”shadow”] Lorem Ipsum is simply dummy text of the printing and typesetting industry.[/box]
@Jack I have tried on my end, it worked perfectly. Where have you place this shortcode?
i am adding this code i function.php before opening of <!DOCTYPE html>
|
0

If you don't want to use a plugin you can write the code direct into the functions.php within your theme folder.

// functions.php
function boxShow($atts, $content = null){
     return '<div class="box">'.$content.'</div>';
} 

add_shortcode('box',boxShow);

After this you can add the shortcode within your Pages:

[box]Text[/box]

Look into this guide for more infos: https://speckyboy.com/getting-started-with-wordpress-shortcodes-examples/

1 Comment

sorry to disturb you. can you please create full code? i have also shared snapshot.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.