Last active
April 16, 2017 04:14
-
-
Save tammalee/c287220405420de560475c9071c12b72 to your computer and use it in GitHub Desktop.
ACF Template Partials in a mu-plugin Class
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 | |
/* Wherever you place this code, the ACF partials for the post will output. I tend to put it after the default content. */ | |
$tmpart = new <name of class>(); | |
$tmpart->tmpltPartials(); | |
?> |
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 | |
/** | |
* @wordpress-plugin | |
* Plugin Name: <name of client> Main | |
* Plugin URI: <URI> | |
* Description: Functions required by <name of theme> theme | |
* Version: 1.0.0 | |
* Author: <author name> | |
* Author URI: <author URI> | |
* License: GPL-2.0+ | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
* Text Domain: <same as for the theme> | |
* Domain Path: /languages | |
*/ | |
// If this file is called directly, abort. | |
if (!defined('WPINC')) { | |
die; | |
} | |
add_action('init', ['<name of class>', 'init']); | |
register_activation_hook(__FILE__, ['<name of class>', 'activatePlugin']); | |
class <name of class> | |
{ | |
/** | |
* Creates a new instance. | |
* | |
* @wp-hook init | |
* | |
* @see __construct() | |
*/ | |
public static function init() | |
{ | |
new self(); | |
// Hide the WordPress generator meta | |
add_filter('the_generator', function () { | |
return ''; | |
}); | |
} | |
/** | |
* <name of class> constructor. | |
*/ | |
public function __construct() | |
{ | |
add_filter('body_class', [$this, 'bodyClasses']); | |
add_filter('post_class', [$this, 'removeHEntry']); | |
// Enable SVG upload and display | |
add_filter('upload_mimes', [$this, 'enableSVGUpload']); | |
add_action('admin_head', [$this, 'enableSVGDisplay']); | |
// Shortcodes | |
add_shortcode('shortcodetest', [$this, 'shortcodeTest']); | |
} | |
/** | |
* Adds custom classes to the array of body classes. | |
* | |
* @param array $classes Classes for the body element. | |
* | |
* @return array | |
*/ | |
public function bodyClasses($classes) | |
{ | |
global $post; | |
// Adds a class of group-blog to blogs with more than 1 published author. | |
if (is_multi_author()) { | |
$classes[] = 'group-blog'; | |
} | |
// Adds a class of hfeed to non-singular pages. | |
if (!is_singular()) { | |
$classes[] = 'hfeed'; | |
} | |
if (isset($post)) { | |
$classes[] = $post->post_type.'-'.$post->post_name; | |
} | |
return $classes; | |
} | |
/** | |
* Remove hentry from pages. | |
* | |
* WordPress adds hentry to posts and pages, but hentry should only be for posts. | |
* | |
* @param $classes | |
* | |
* @return array | |
* | |
* @link https://core.trac.wordpress.org/ticket/28482 | |
*/ | |
public function removeHEntry($classes) | |
{ | |
if (is_page()) { | |
$classes = array_diff($classes, ['hentry']); | |
} | |
return $classes; | |
} | |
/** | |
* Enables SVG uploads in the Media Library. | |
* | |
* @param $mimes | |
* | |
* @return mixed | |
*/ | |
public function enableSVGUpload($mimes) | |
{ | |
$mimes['svg'] = 'image/svg+xml'; | |
return $mimes; | |
} | |
/** | |
* Enables proper SVG display in the Media Library. | |
*/ | |
public function enableSVGDisplay() | |
{ | |
echo '<style>kp.media-icon img[src$=".svg"], img[src$=".svg"].attachment-post-thumbnail { width: 100% !important; height: auto !important; }</style>'; | |
} | |
/** | |
* Shortcode: Button. | |
* | |
* @param $atts | |
* @param null $content | |
* | |
* @return string | |
*/ | |
public function shortcodeTest($atts, $content = null) | |
{ | |
extract(shortcode_atts( | |
[ | |
'foo' => '', | |
'bar' => '', | |
], $atts) | |
); | |
$results = $foo . " > " . $barr; | |
return $results; | |
} | |
/** | |
* Partials. | |
* Original Source: https://selfteach.me/acf-flexible-content-modular-wordpress-themes/ | |
* When you define a new content block in ACF, you'll need to create a partial file called partial-*.php. | |
* The * being the slug of the content block. It must be exactly the same, otherwise it will not load. | |
*/ | |
public function tmpltPartials() | |
{ | |
// Create an array to hold the content blocks. This keeps them in order. | |
$results = []; | |
if (have_rows('layout')) { | |
while (have_rows('layout')) { | |
the_row(); | |
if (get_row_layout() === 'template_picker') { | |
$file_name = basename(get_sub_field('template'), '.php'); | |
$template = substr($file_name, strpos($file_name, '-') + 1); | |
ob_start(); | |
get_template_part('template-parts/partial', $template); | |
$results[] = ob_get_clean(); | |
} else { | |
ob_start(); | |
get_template_part('template-parts/partial', get_row_layout()); | |
$results[] = ob_get_clean(); | |
} | |
} | |
} | |
foreach ($results as $result) { | |
echo $result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment