Last active
March 18, 2022 17:23
-
-
Save Sanabria/24ea53654b1d91f1bcea0771a6feef6c to your computer and use it in GitHub Desktop.
Create a shortcode in Roots 9.0. Source: https://discourse.roots.io/t/the-correct-way-to-add-shortcode/14394/4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sage required files | |
* | |
* The mapped array determines the code library included in your theme. | |
* Add or remove files to the array as needed. Supports child theme overrides. | |
*/ | |
array_map(function ($file) use ($sage_error) { | |
$file = "../app/{$file}.php"; | |
if (!locate_template($file, true, true)) { | |
$sage_error(sprintf(__('Error locating <code>%s</code> for inclusion.', 'sage'), $file), 'File not found'); | |
} | |
}, ['helpers', 'setup', 'filters', 'admin', 'shortcodes']); //add shortcodes to the required files array |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/shortocodes.php | |
namespace App; | |
use App\Controllers\App; | |
/** | |
* Return if Shortcodes already exists. | |
*/ | |
if (class_exists('Shortcodes')) { | |
return; | |
} | |
/** | |
* Shortcodes | |
*/ | |
class Shortcodes | |
{ | |
/** | |
* Constructor | |
*/ | |
public function __construct() | |
{ | |
$shortcodes = [ | |
'calendario' | |
]; | |
return collect($shortcodes) | |
->map(function ($shortcode) { | |
return add_shortcode($shortcode, [$this, strtr($shortcode, ['-' => '_'])]); | |
}); | |
} | |
/** | |
* Calendar | |
* Loads the calendar event partial | |
* | |
* @param array $atts | |
* @param string $content | |
* @return string | |
*/ | |
public function calendario($atts, $content = null) | |
{ | |
return \App\template('partials.eventos', array('shortcode' => 'true')); | |
} | |
} | |
new Shortcodes(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment