Created
January 8, 2018 05:44
-
-
Save adamplabarge/4826a005c41aa8f348c722a4004c761a to your computer and use it in GitHub Desktop.
Portland WP Meetup - Hooks and Filters - Jan 08 2018 - Hooks and Filters in a plugins and Class
This file contains 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 | |
/** | |
* Plugin Name: TheAudioBeatnik Category Slideshow | |
* Description: A category page slideshow using Slick.js and WP Plugin Advanced Custom Fields | |
* Author: Adam LaBarge | |
* Version: 1.0 | |
* Author URI: https://www.linkedin.com/in/adamlabarge | |
*/ | |
class tab_CategorySlideShow { | |
public function init () { | |
// unhook remove_catchresponsive_featured_overall_image | |
$this->hook_custom_template(); | |
// setup assets | |
$this->css(); | |
$this->js(); | |
// setup ACF slideshow fields | |
$this->acf_slideshow_fieldgroup(); | |
// build slideshow | |
$this->hook_in_slideshow(); | |
} | |
public function hook_custom_template() { | |
add_filter('template_include', array($this, 'get_archive_template')); | |
} | |
public function get_archive_template($template) { | |
if (is_archive()) | |
return plugin_dir_path( __FILE__ ) . 'archive.php'; | |
return $template; | |
} | |
public function hook_in_slideshow() { | |
add_action('tab_category_pages_slideshow', array($this, 'make_slideshow'), 40); | |
} | |
public function make_slideshow() { | |
if (! function_exists('get_field')) return; | |
$slides = get_field('tab_category_slideshow', array_shift(get_the_category())); | |
if (! $slides) return; | |
?> | |
<style> | |
.page-header .page-title {border: none;} | |
.tab-category-slideshow {max-width: 780px; margin: 15px 0 20px 20px;} | |
.tab-category-slideshow .hide { display: none;} | |
.tab-category-slideshow button.slick-arrow {display: none !important;} | |
</style> | |
<div class="tab-category-slideshow"> | |
<?php | |
foreach ($slides as $key => $slide) { | |
if ($slide['tab_disable_slide'][0]) continue; | |
$target = ($slide['tab_open_in_same_page'][0]) ? '' : '_blank'; | |
$class = ($key > 0) ? 'hide' : ''; | |
?> | |
<div class="slide <?php echo $class; ?>"> | |
<a target="<?php echo $target; ?>" href="<?php echo $slide['tab_link']; ?>" title="Add on The Audio Beatnik" rel="nofollow"> | |
<img src="<?php echo $slide['tab_slide']; ?>" title="<?php $slide['tab_img_alt_tag_text']; ?>" alt="<?php $slide['tab_img_alt_tag_text']; ?>"> | |
</a> | |
</div> | |
<?php | |
} | |
?> | |
</div> | |
<?php | |
} | |
public function css() { | |
wp_enqueue_style( | |
'tab_homepageslideshow_slick', | |
plugin_dir_url( __FILE__ ) . 'slick/slick/slick.css' | |
); | |
wp_enqueue_style( | |
'tab_homepageslideshow_slick_theme', | |
plugin_dir_url( __FILE__ ) . 'slick/slick/slick-theme.css' | |
); | |
} | |
public function js() { | |
wp_enqueue_script( | |
'tab_homepageslideshow_slick', | |
plugin_dir_url( __FILE__ ) . 'slick/slick/slick.min.js', | |
array(), | |
'1.0.1', | |
true | |
); | |
wp_enqueue_script( | |
'tab_homepageslideshow', | |
plugin_dir_url( __FILE__ ) . 'javascript/tab_js.js', | |
array(), | |
'1.0', | |
true | |
); | |
} | |
public function acf_slideshow_fieldgroup() { | |
if (function_exists('acf_add_local_field_group')) : | |
acf_add_local_field_group(array ( | |
'key' => 'group_59cdb50018892', | |
'title' => 'The Audio Beatnik Category Pages Slideshow', | |
'fields' => array ( | |
array ( | |
'key' => 'field_59cdb54e39ccd', | |
'label' => 'TAB Category Slideshow', | |
'name' => 'tab_category_slideshow', | |
'type' => 'repeater', | |
'instructions' => '', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array ( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'collapsed' => '', | |
'min' => 0, | |
'max' => 0, | |
'layout' => 'row', | |
'button_label' => 'Add Slide', | |
'sub_fields' => array ( | |
array ( | |
'key' => 'field_59cdba940262a', | |
'label' => 'Link', | |
'name' => 'tab_link', | |
'type' => 'url', | |
'instructions' => 'Enter full link a click should take the user to. example: https://google.com', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array ( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'default_value' => '', | |
'placeholder' => '', | |
), | |
array ( | |
'key' => 'field_59cdbabb0262b', | |
'label' => 'Open in same page?', | |
'name' => 'tab_open_in_same_page', | |
'type' => 'checkbox', | |
'instructions' => 'Will open new tab by default.', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array ( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'choices' => array ( | |
'Yes' => 'Yes', | |
), | |
'allow_custom' => 0, | |
'save_custom' => 0, | |
'default_value' => array ( | |
), | |
'layout' => 'vertical', | |
'toggle' => 0, | |
'return_format' => 'value', | |
), | |
array ( | |
'key' => 'field_59cdbae20262c', | |
'label' => 'Slide', | |
'name' => 'tab_slide', | |
'type' => 'file', | |
'instructions' => 'Image Size: 780x260', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array ( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'return_format' => 'url', | |
'library' => 'all', | |
'min_size' => '', | |
'max_size' => '', | |
'mime_types' => '', | |
), | |
array ( | |
'key' => 'field_59cdbb8a524d6', | |
'label' => 'Image Alt Tag Text', | |
'name' => 'tab_img_alt_tag_text', | |
'type' => 'text', | |
'instructions' => 'Add image alternate text here. Will be used for title tag as well.', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array ( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'default_value' => '', | |
'placeholder' => '', | |
'prepend' => '', | |
'append' => '', | |
'maxlength' => '', | |
), | |
array ( | |
'key' => 'field_59cdbafb0262d', | |
'label' => 'Disable', | |
'name' => 'tab_disable_slide', | |
'type' => 'checkbox', | |
'instructions' => 'Checking yes will hide this slide from the slideshow.', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array ( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'choices' => array ( | |
'Yes' => 'Yes', | |
), | |
'allow_custom' => 0, | |
'save_custom' => 0, | |
'default_value' => array ( | |
), | |
'layout' => 'vertical', | |
'toggle' => 0, | |
'return_format' => 'value', | |
), | |
), | |
), | |
), | |
'location' => array ( | |
array ( | |
array ( | |
'param' => 'taxonomy', | |
'operator' => '==', | |
'value' => 'all', | |
), | |
), | |
), | |
'menu_order' => 0, | |
'position' => 'acf_after_title', | |
'style' => 'default', | |
'label_placement' => 'top', | |
'instruction_placement' => 'label', | |
'hide_on_screen' => '', | |
'active' => 1, | |
'description' => '', | |
)); | |
endif; | |
} | |
} | |
add_action('init', 'tab_ActivateCategorySlideShow'); | |
function tab_ActivateCategorySlideShow() { | |
// check to see if ACF functions exist | |
if (function_exists('get_field') | |
|| function_exists('acf_add_options_page') | |
|| function_exists('acf_add_local_field_group')) { | |
$tab_CategorySlideShow = new tab_CategorySlideShow(); | |
$tab_CategorySlideShow->init(); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment