Skip to content

Instantly share code, notes, and snippets.

@Sogl
Created November 21, 2024 15:10
Show Gist options
  • Save Sogl/e261c3ff41d6fb8dddbf1e141ef15875 to your computer and use it in GitHub Desktop.
Save Sogl/e261c3ff41d6fb8dddbf1e141ef15875 to your computer and use it in GitHub Desktop.
Admincontent shortcode
<?php
namespace Grav\Plugin\Shortcodes;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use Parsedown;
class AdminContentShortcode extends \Grav\Plugin\Shortcodes\Shortcode
{
public function init()
{
$this->shortcode->getHandlers()->add('admincontent', function (ShortcodeInterface $sc) {
$user = $this->grav['user'];
$adminContent = $sc->getParameter('admin');
$defaultContent = $sc->getParameter('default');
if ($user->authorize('admin.login') || $user->authorize('admin.super')) {
return $this->processHtml($adminContent);
}
return $this->processHtml($defaultContent);
});
}
private function processHtml($text)
{
$parsedown = new Parsedown();
$html = $parsedown->text($text);
$html = preg_replace('/<p>(.*?)<\/p>/', '$1', $html);
return $html;
}
}

[admincontent admin='John Doe' default='John Doe']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment