Wordpress

How to Use a Single Function for Multiple Shortcodes

The Shortcode API in WordPress allows you to create “snippets” in order to introduce special content (like HTML code or PHP functions) in a post. Most people build a separate function for each shortcode, but it is sometimes redundant and exhausting.

To prove this statement, look at the following code:

12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

<?phpfunction one_half_sc( $atts, $content = null, $tag ) {

extract( shortcode_atts(  array(

// extra classes

‘class’ => ”,

‘last’ => ”

), $atts ) );

if ( $class != ” )

$class = ‘ ‘ . $class;

if ( $last = ‘yes’ )

$last_class = ‘ last’;

return ‘<div >’ . do_shortcode( $content ) . ‘</div>’;

}

add_shortcode( ‘one_half’, ‘one_half_sc’ );

function one_third_sc( $atts, $content = null, $tag ) {

extract( shortcode_atts(  array(

// extra classes

‘class’ => ”,

‘last’ => ”

), $atts ) );

if ( $class != ” )

$class = ‘ ‘ . $class;

if ( $last = ‘yes’ )

$last_class = ‘ last’;

return ‘<div >’ . do_shortcode( $content ) . ‘</div>’;

}

add_shortcode( ‘one_third’, ‘one_third_sc’ );

function one_quarter_sc( $atts, $content = null, $tag ) {

extract( shortcode_atts(  array(

// extra classes

‘class’ => ”,

‘last’ => ”

), $atts ) );

if ( $class != ” )

$class = ‘ ‘ . $class;

if ( $last = ‘yes’ )

$last_class = ‘ last’;

return ‘<div >’ . do_shortcode( $content ) . ‘</div>’;

}

add_shortcode( ‘one_quarter’, ‘one_quarter_sc’ );

function two_thirds_sc( $atts, $content = null, $tag ) {

extract( shortcode_atts(  array(

// extra classes

‘class’ => ”,

‘last’ => ”

), $atts ) );

if( $class != ” )

$class = ‘ ‘ . $class;

if( $last = ‘yes’ )

$last_class = ‘ last’;

return ‘<div >’ . do_shortcode( $content ) . ‘</div>’;

}

add_shortcode( ‘two_thirds’, ‘two_thirds_sc’ );

function three_quarters_sc( $atts, $content = null, $tag ) {

extract( shortcode_atts(  array(

// extra classes

‘class’ => ”,

‘last’ => ”

), $atts ) );

if ( $class != ” )

$class = ‘ ‘ . $class;

if ( $last = ‘yes’ )

$last_class = ‘ last’;

return ‘<div >’ . do_shortcode( $content ) . ‘</div>’;

}

add_shortcode( ‘three_quarters’, ‘three_quarters_sc’ );

?>

Except for the CSS classes, the same code is being used in every function. In addition, the attributes are also the same. It’s not only hard to read, but also difficult to amend. If you wanted to change any feature of the div, you would have to change the same part in all the other functions.

Wouldn’t it be a lot easier if there was a variable that could collect the shortcode’s tag? Unsurprisingly, there is a variable called $tag that could check the shortcode’s tag and behave accordingly.

Now look at the aforementioned example again: the code would be a lot shorter and easier to read and amend if the $tag variable was used.

Similarly, I have come with two scenarios where the $tag variable could be used, but the usage of this variable is not limited to these scenarios. You can use it however you want to.

Example 1: Column divs

Columns are one of the most important parts of a CSS layout as they make responsive design websites more adaptive. They can be seen in almost all WordPress themes and they use shortcodes in the same way as the example above.

Let’s take the above example again. As you can see, all the functions have almost the same code. Therefore, you can easily merge them into a single function and use the $tag variable to connect them:

12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

<?php// Column shortcodes

function wptuts_columns_sc( $atts, $content = null, $tag ) {

extract( shortcode_atts(  array(

// extra classes

‘class’ => ”

), $atts ) );

if ( $class != ” )

$class = ‘ ‘ . $class;

$last = ”;

// check the shortcode tag to add a “last” class

if ( strpos( $tag, ‘_last’ ) !== false )

$tag = str_replace( ‘_last’, ‘ last’, $tag);

$output = ‘<div>’ . do_shortcode( $content ) . ‘</div>’;

return $output;

}

add_shortcode( ‘one_half’, ‘wptuts_columns_sc’ );

add_shortcode( ‘one_half_last’, ‘wptuts_columns_sc’ );

add_shortcode( ‘one_third’, ‘wptuts_columns_sc’ );

add_shortcode( ‘one_third_last’, ‘wptuts_columns_sc’ );

add_shortcode( ‘one_quarter’, ‘wptuts_columns_sc’ );

add_shortcode( ‘one_quarter_last’, ‘wptuts_columns_sc’ );

add_shortcode( ‘two_thirds’, ‘wptuts_columns_sc’ );

add_shortcode( ‘two_thirds_last’, ‘wptuts_columns_sc’ );

add_shortcode( ‘three_quarters’, ‘wptuts_columns_sc’ );

add_shortcode( ‘three_quarters_last’, ‘wptuts_columns_sc’ );

?>

This code still repeats a few lines, but if you want, you can replace the add_shortcode()lines with this:

12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?php$tags = array(

‘one_half’,

‘one_half_last’,

‘one_third’,

‘one_third_last’,

‘one_quarter’,

‘one_quarter_last’,

‘two_thirds’,

‘two_thirds_last’,

‘three_quarters’,

‘three_quarters_last’,

);

foreach( $tags as $tag ) {

add_shortcode( $tag, ‘wptuts_columns_sc’ );

}

?>

Now, the code’s even easier to manage!

Example 2: Post lists

Most people would want to list a few previous articles in a post. Numerous plugins are available to carry out this function by using shortcodes, but they require you to use various attributes which could result in a complex, ugly tag like [posts cat="5,6" author="tabish" status="private" postsperpage="4" and="so on"].

Instead, you could use the $tag variable like this:

12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

<?php// Post list shortcodes

function wptuts_post_lists_sc( $atts, $content = null, $tag ) {

global $post;

$post_ID = $post->ID;

$post_author = $post->post_author;

extract( shortcode_atts(  array(

‘number’ => 5,

‘exclude_current’ => ‘yes’,

‘orderby’ => ‘date’

), $atts ) );

$args = ”;

switch( $tag ) {

case “latest_posts”:

// we don’t need any arguments to retrieve latest posts 🙂

break;

case “category_posts”:

$categories = get_the_category( $post_ID );

$first_category = $categories[0]->term_id;

$args = ‘cat=’ . $first_category;

break;

case “author_posts”:

$args = ‘author=’ . $post_author;

break;

case “future_posts”:

$args = ‘post_status=future’;

break;

}

$not_in = ‘&post__not_in[]=’ . $post_ID;

if ( $exclude_current == ‘no’ )

$not_in = ”;

$get_posts = get_posts( $args . $not_in . ‘&posts_per_page=’ . $number . ‘&orderby=’ . $orderby );

$output = ‘<ul>’;

foreach ( $get_posts as $post ) {

$output .= ‘<li><a href=”‘ . get_permalink() . ‘” title=”‘ . esc_attr( get_the_title() ) . ‘”>’ . get_the_title() . ‘</a></li>’;

}

$output .= ‘</ul>’;

wp_reset_query();

return $output;

}

add_shortcode( ‘latest_posts’, ‘wptuts_post_lists_sc’ );

add_shortcode( ‘category_posts’, ‘wptuts_post_lists_sc’ );

add_shortcode( ‘author_posts’, ‘wptuts_post_lists_sc’ );

add_shortcode( ‘future_posts’, ‘wptuts_post_lists_sc’ );

?>

Likewise, you can use the $tag variable in a variety of ways and make your coding life simpler!

About the author

Tabish Shaikh

Website designer, website developer, blogger and a writer. These are the things I like, love and live for. Having years of experience in each field. Developing sites since childhood. Getting the most complicated things done is my specialty. Providing each and everything of top most quality to each and every client.

Leave a Comment


*