Created
September 1, 2021 07:59
-
-
Save delennerd/279d249e606a0e73631698f583459416 to your computer and use it in GitHub Desktop.
WordPress: Register shortcodes with Class and Interfaces
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 | |
/** | |
* @package DlmWpTheme | |
*/ | |
// Exit if accessed directly. | |
defined('ABSPATH') || exit; | |
require_once dirname(__FILE__) . '/class-interface-package.php'; | |
require_once dirname(__FILE__) . '/class-interface-package-shortcode.php'; | |
// Todo: Change your class name | |
class MY__ShortcodeClass implements Dlm__PackageInterface, Dlm__ShortcodeInterface { | |
private $atts = []; | |
private $postType = "my_cpt"; | |
function __construct() {} | |
/** | |
* Put all relevant methods in the init function | |
*/ | |
public function init() { | |
// $this->someFunctions(); | |
return $this->renderOutput(); | |
} | |
/** | |
* Used in shortcode function | |
*/ | |
public function setAttributes($atts) { | |
$this->atts = $atts; | |
} | |
/** | |
* | |
*/ | |
protected function getPosts() { | |
$posts = get_posts(array( | |
'post_type' => $this->postType, | |
'post_status' => 'publish', | |
'posts_per_page' => -1, | |
'meta_key' => 'MY_CUSTOM_FIELD', | |
// If a numberic field | |
// 'meta_type' => 'NUMERIC', | |
// 'orderby' => 'meta_value meta_value_num', | |
'orderby' => 'meta_value', | |
'order' => 'ASC', | |
)); | |
return $posts; | |
} | |
/** | |
* Get all posts and render it for the shortcode | |
*/ | |
public function renderOutput() { | |
$posts = $this->getPosts(); | |
ob_start(); | |
include dirname(__FILE__) . '/partials/default.php'; | |
$output = ob_get_contents(); | |
ob_get_clean(); | |
return $output; | |
} | |
/** | |
* Register the shortcode in wordpress | |
*/ | |
public function activateShortcode() { | |
// | |
add_shortcode( 'my-custom-shortcode', [ $this, 'shortcodeFunc' ] ); | |
} | |
/** | |
* @return | |
*/ | |
public function shortcodeFunc( $atts = [], $content = null ) { | |
$atts = array_change_key_case((array)$atts, CASE_LOWER); | |
$this->setAttributes($atts); | |
return $this->init(); | |
} | |
} | |
// Todo: Change your class name | |
(new MY__ShortcodeClass())->activateShortcode(); |
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 | |
/** | |
* Interface for shortcode packages | |
* To activate the shortcode > | |
* Use (new MY__ClassName())->activateShortcode(); | |
* | |
* @package DlmWpTheme | |
*/ | |
// Exit if accessed directly. | |
defined('ABSPATH') || exit; | |
interface Dlm__ShortcodeInterface { | |
public function setAttributes(array $atts); | |
public function activateShortcode(); | |
public function shortcodeFunc( $atts = [], $content = null ); | |
} |
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 | |
/** | |
* Interface for packages | |
* | |
* @package DlmWpTheme | |
*/ | |
// Exit if accessed directly. | |
defined('ABSPATH') || exit; | |
interface Dlm__PackageInterface { | |
public function init(); | |
public function renderOutput(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment