Skip to content

Commit c150af1

Browse files
committed
Added [current_year] shortcode example
1 parent c8f7133 commit c150af1

4 files changed

Lines changed: 40 additions & 9 deletions

File tree

app/Plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function load_plugin() {
7070
new Widgets\WidgetLoader();
7171

7272
// Load shortcodes
73-
new Shortcodes\ShortcodeLoader();
73+
new Shortcodes\Shortcode_Loader();
7474

7575
}
7676

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace VendorName\PluginName\Shortcodes;
3+
use VendorName\PluginName\Plugin;
4+
5+
class CurrentYear_Shortcode extends Plugin
6+
{
7+
8+
public function __construct() {
9+
10+
// Usage: [current_year]
11+
if ( ! shortcode_exists( 'current_year' ) ) {
12+
add_shortcode( 'current_year', array( $this, 'current_year_shortcode' ) );
13+
}
14+
15+
}
16+
17+
/**
18+
* A short code that returns the current year
19+
*
20+
* @param $atts array Shortcode Attributes
21+
* @return string Output of shortcode
22+
* @since 0.4.0
23+
*/
24+
public function current_year_shortcode( $atts ) {
25+
26+
return date( 'Y' );
27+
28+
}
29+
30+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@
22
namespace VendorName\PluginName\Shortcodes;
33
use VendorName\PluginName\Plugin;
44

5-
class HelloShortcode extends Plugin
5+
class Hello_Shortcode extends Plugin
66
{
77

88
public function __construct() {
99

1010
// Usage: [hello name="Daniel"]
1111
if ( ! shortcode_exists( 'hello' ) ) {
12-
add_shortcode( 'hello', array( $this, 'hello_world' ) );
12+
add_shortcode( 'hello', array( $this, 'hello_world_shortcode' ) );
1313
}
1414

1515
}
1616

1717
/**
18-
* A short code the returns "Hello {$name}!", if provided
18+
* A short code that returns "Hello {$name}!", if provided
1919
*
2020
* @param $atts array Shortcode Attributes
2121
* @return string Output of shortcode
2222
* @since 0.1.0
2323
*/
24-
public function hello_world( $atts ) {
24+
public function hello_world_shortcode( $atts ) {
2525
$atts = shortcode_atts( array(
2626
'name' => 'world'
2727
), $atts, 'hello' );
2828

29-
return sprintf( __( 'Hello', self::$textdomain ) . ' %s!', $atts[ 'name' ] );
29+
return sprintf( __( 'Hello %s!', self::$textdomain ), $atts[ 'name' ] );
3030
}
3131

3232
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace VendorName\PluginName\Shortcodes;
33
use VendorName\PluginName\Plugin;
44

5-
class ShortcodeLoader extends Plugin {
5+
class Shortcode_Loader extends Plugin {
66

77
/**
88
* @var array Shortcode class name to register
@@ -13,7 +13,8 @@ class ShortcodeLoader extends Plugin {
1313
public function __construct() {
1414

1515
$this->shortcodes = array(
16-
HelloShortcode::class
16+
Hello_Shortcode::class,
17+
CurrentYear_Shortcode::class
1718
);
1819

1920
foreach( $this->shortcodes as $shortcodeClass ) {
@@ -22,7 +23,7 @@ public function __construct() {
2223
if( $shortcode instanceof ShortcodeInterface ) {
2324
new $shortcode();
2425
} else {
25-
// You could log not added shortcodes here
26+
// Log or show error notices
2627
}
2728

2829
}

0 commit comments

Comments
 (0)