Skip to content

Instantly share code, notes, and snippets.

@delennerd
Created September 28, 2023 10:38
Show Gist options
  • Save delennerd/4b6b1db0fc1f5f607f24b60c8063650c to your computer and use it in GitHub Desktop.
Save delennerd/4b6b1db0fc1f5f607f24b60c8063650c to your computer and use it in GitHub Desktop.
WordPress Custom Shortcode Controller
<?php
/**
* @since 1.0.0
* @package MyPlugin
* @subpackage PostType
* @author XXXXX
*/
namespace MyPlugin\Controllers;
if ( !defined('ABSPATH') )
exit; // Exit if accessed directly
class PostFormShortcodeController
{
private $atts = [];
private $shortcode_tag = 'my_custom_shortcode';
function __construct()
{
add_shortcode($this->shortcode_tag, [$this, 'shortcode_callback']);
}
public function set_attributes($atts)
{
$this->atts = shortcode_atts(array(), $atts);
}
public function render_output($content)
{
$atts = $this->atts;
ob_start();
include MY_PLUGIN_DIR . 'templates/post-form-page.php';
$output = ob_get_contents();
ob_get_clean();
return $output;
}
public function shortcode_callback($atts = [], $content = null)
{
$atts = array_change_key_case((array) $atts, CASE_LOWER);
$this->set_attributes($atts);
return $this->render_output($content);
}
}
add_action('init', function () {
new PostFormShortcodeController();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment