Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Adds a taxonomy to the native 'post' object type.
- *
- * Also adds a rewrite endpoint to test the issue. Visiting any posts URL with
- * the taxonomy slug segment prepended will trigger output of the custom
- * taxonomy's terms assigned to the post; best to view page source.
- *
- * @link https://wordpress.org/support/topic/turning-off-frontend-actions-and-filter
- */
- class OrgSupportTopic862744
- {
- const TAXONOMY_SLUG = 'mytax';
- const OBJECT_TYPE = 'post';
- const PERMASTRUCT_TYPE = EP_ALL;
- const TEMPLATE_FILE = 'org-862744.php';
- const TAXONOMY_HOOK_PRIORITY = 11;
- const TEMPLAGE_HOOK_PRIORITY = 99;
- public function __construct()
- {
- $this->_construct();
- }
- protected function _construct()
- {
- }
- public function init()
- {
- $this->_hook();
- }
- protected function _hook()
- {
- $me = $this;
- add_action('init', function() use ($me) {
- $me->_registerTaxonomy();
- }, static::TAXONOMY_HOOK_PRIORITY);
- }
- public function _registerTaxonomy()
- {
- register_taxonomy(static::TAXONOMY_SLUG, static::OBJECT_TYPE, array(
- 'label' => 'My Tax',
- 'hierarchical' => true
- ));
- var_dump(static::PERMASTRUCT_TYPE);
- add_rewrite_endpoint(static::TAXONOMY_SLUG, static::PERMASTRUCT_TYPE);
- flush_rewrite_rules();
- add_filter('template_include', array($this, '_overrideTemplate'), static::TEMPLAGE_HOOK_PRIORITY);
- }
- public function generateTermList($objectId)
- {
- return get_the_term_list($objectId, static::TAXONOMY_SLUG);
- }
- public function _overrideTemplate($template)
- {
- global $wp_query;
- if ($this->_getQueryVar(static::TAXONOMY_SLUG) !== null && ($objectId = get_post()->ID)) {
- echo $this->generateTermList($objectId);
- }
- return $template;
- // return $this->_locateTemplateFile(static::TEMPLATE_FILE);
- }
- protected function _locateTemplateFile($files)
- {
- if (!is_array($files)) {
- $files = array($files);
- }
- return strlen($path = locate_template($files, false))
- ? $path
- : null;
- }
- protected function _getQueryVar($key)
- {
- return get_query_var($key, null);
- }
- }
- /**
- * @staticvar type $instance
- * @return \OrgSupportTopic862744
- * @link https://wordpress.org/support/topic/turning-off-frontend-actions-and-filter
- */
- function orgSupportTopic862744()
- {
- static $instance = null;
- if (is_null($instance)) {
- $instance = new OrgSupportTopic862744();
- $instance->init();
- }
- return $instance;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement