Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active October 23, 2019 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/5d555468c2c5b3448e5bb0ed6f666f6f to your computer and use it in GitHub Desktop.
Save tommcfarlin/5d555468c2c5b3448e5bb0ed6f666f6f to your computer and use it in GitHub Desktop.
<?php
add_action('single_template', 'acmeIncludeSingleTemplate');
/**
* Includes a custom, single template as included in a plugin. If
* the template is being viewed for a custom post type then use it;
* otherwise, use the template that's provided by WordPress at runtime.
*
* @param string $originalTemplate the path to the original template
*
* @return string the path to the original template or the custom template.
*/
function acmeIncludeSingleTemplate($originalTemplate)
{
$singleTemplate = plugin_dir_path(
\dirname(
__DIR__
)
);
$singleTemplate .= '/templates/single-acme.php';
if ('acme-cpt' === get_post_type(get_the_ID())) {
if (file_exists($singleTemplate)) {
return $singleTemplate;
}
}
return $originalTemplate;
}
<?php
add_action('archive_template', 'acmeIncludeArchiveTemplate');
/**
* Includes a custom, archive template as included in a plugin. If
* the template is being viewed for a custom post type then use it;
* otherwise, use the template that's provided by WordPress at runtime.
*
* @param string $originalArchiveTemplate the path to the original template
*
* @return string the path to the original template or the custom template.
*/
function acmeIncludeArchiveTemplate($originalArchiveTemplate)
{
$archiveTemplate = plugin_dir_path(
\dirname(
__DIR__
)
);
$archiveTemplate .= '/templates/archive-acme.php';
if ('acme-cpt' === get_post_type(get_the_ID())) {
if (file_exists($archiveTemplate)) {
return $archiveTemplate;
}
}
return $originalArchiveTemplate;
}
<?php
add_action('single_template', 'acmeIncludeSingleTemplate');
/**
* Includes a custom, single template as included in a plugin. If
* the template is being viewed for a custom post type then use it;
* otherwise, use the template that's provided by WordPress at runtime.
*
* @param string $originalTemplate the path to the original template
*
* @return string the path to the original template or the custom template.
*/
function acmeIncludeSingleTemplate($originalTemplate)
{
// More to come...
}
<?php
add_action('archive_template', 'acmeIncludeArchiveTemplate');
/**
* Includes a custom, archive template as included in a plugin. If
* the template is being viewed for a custom post type then use it;
* otherwise, use the template that's provided by WordPress at runtime.
*
* @param string $originalArchiveTemplate the path to the original template
*
* @return string the path to the original template or the custom template.
*/
function acmeIncludeArchiveTemplate($originalArchiveTemplate)
{
// More to come...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment