File: /var/www/vcz/wp-content/themes/volonteka/functions.php
<?php
define('THEMEROOT', get_stylesheet_directory_uri() . "/");
define('FILEROOT', dirname(__DIR__) . '/' . get_option('stylesheet'));
define("THEMEDIR", __DIR__ . "/");
$composer_autoload = THEMEDIR . "/vendor/autoload.php";
/**
* Initialize Timber. It is installed using Composer, so we can initialize it only if Composer autoload file is present.
*/
if ( file_exists( $composer_autoload ) ) {
require_once $composer_autoload;
$timber = new Timber\Timber();
}
/**
* This ensures that Timber is loaded and available as a PHP class.
* If not, it gives an error message to help direct developers on where to activate
*/
if ( ! class_exists( "Timber" ) ) {
add_action(
"admin_notices",
function() {
echo '<div class="error"><p>Timber not activated. Make sure that you have Timber installed in this theme using Composer. Refresh website if needed after activating Timber.</p></div>';
}
);
add_filter(
"template_include",
function() {
echo '<h1>Timber not activated. Make sure that you have Timber installed in this theme using Composer. Refresh website if needed after activating Timber.</h1>';
}
);
return;
}
add_action( 'init', function(){
add_theme_support( 'post-thumbnails' );
register_nav_menus(array(
'main-menu' => 'Main Menu',
'footer-menu' => 'Footer Menu',
));
// ADD your image sizes here
// add_image_size( '4x3_small', 488, 387, true );
});
//helper image to pull an attachment image alt description
function get_image_alt($attach_id){
$alt_text = get_post_meta($attach_id, '_wp_attachment_image_alt', true);
if(empty($alt_text)) // If not, Use the Caption
{
$attachment = get_post($attach_id);
$alt_text = trim(strip_tags( $attachment->post_excerpt ));
}
if(empty($alt_text)) // Finally, use the title
{
$attachment = get_post($attach_id);
$alt_text = trim(strip_tags( $attachment->post_title ));
}
return $alt_text;
}
/**
* This ensures that Timber is loaded and available as a PHP class.
* If not, it gives an error message to help direct developers on where to activate
*/
if ( ! class_exists( 'Timber' ) ) {
add_action(
'admin_notices',
function() {
echo '<div class="error"><p>Timber not activated. Make sure you activate the plugin in <a href="' . esc_url( admin_url( 'plugins.php#timber' ) ) . '">' . esc_url( admin_url( 'plugins.php' ) ) . '</a></p></div>';
}
);
add_filter(
'template_include',
function( $template ) {
return get_stylesheet_directory() . '/static/no-timber.html';
}
);
return;
}
/**
* Sets the directories (inside your theme) to find .twig files
*/
Timber::$dirname = array( 'templates', 'views' );
/**
* By default, Timber does NOT autoescape values. Want to enable Twig's autoescape?
* No prob! Just set this value to true
*/
Timber::$autoescape = false;
/**
* We're going to configure our theme inside of a subclass of Timber\Site
* You can move this to its own file and include here via php's include("MySite.php")
*/
class BorealisSite extends Timber\Site {
/** Add timber support. */
public function __construct() {
add_action( 'after_setup_theme', array( $this, 'theme_supports' ) );
add_filter( 'timber/context', array( $this, 'add_to_context' ) );
add_filter( 'timber/twig', array( $this, 'add_to_twig' ) );
add_action( 'init', array( $this, 'register_post_types' ) );
add_action( 'init', array( $this, 'register_taxonomies' ) );
parent::__construct();
}
/** This is where you add some context
*
* @param string $context context['this'] Being the Twig's {{ this }}.
*/
public function add_to_context( $context ) {
$context['menu']['main'] = new Timber\Menu('main-menu');
$context['menu']['footer'] = new Timber\Menu('footer-menu');
$context['site'] = $this;
$context['privacy_page_link'] = get_field('privacy_policy_link', 'option');
$context['posts_page_link'] = get_field('posts_page_link', 'option');
$context['cookie_text'] = get_field('cookie_policy_text', 'option');
$context['copyright_text'] = get_field('copyright_text', 'option');
$context['sponsors'] = get_field('sponsors', 'option');
$context['main_hero_headline'] = get_field('main_hero_headline', 'option');
$context['main_hero_subheadline'] = get_field('main_hero_subheadline', 'option');
$context['main_hero_button'] = get_field('main_hero_button_link', 'option');
$context['main_hero_bg_image'] = get_field('main_hero_bg_image', 'option');
$context['excluded_category'] = get_field('excluded_category', 'option');
return $context;
}
public function theme_supports() {
// Make hockey available for translation. Translations can be added to the /languages/ directory.
load_theme_textdomain( 'trex', get_template_directory() . '/languages' );
// Add default posts and comments RSS feed links to head.
add_theme_support( 'automatic-feed-links' );
/*
* Let WordPress manage the document title.
* By adding theme support, we declare that this theme does not use a
* hard-coded <title> tag in the document head, and expect WordPress to
* provide it for us.
*/
add_theme_support( 'title-tag' );
/*
* Enable support for Post Thumbnails on posts and pages.
*
* @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
*/
add_theme_support( 'post-thumbnails' );
/*
* Switch default core markup for search form, comment form, and comments
* to output valid HTML5.
*/
add_theme_support(
'html5',
array(
'comment-form',
'comment-list',
'gallery',
'caption',
)
);
/*
* Enable support for Post Formats.
*
* See: https://codex.wordpress.org/Post_Formats
*/
add_theme_support(
'post-formats',
array(
'aside',
'image',
'video',
'quote',
'link',
'gallery',
'audio',
)
);
add_theme_support( 'menus' );
}
/** This is where you can add your own functions to twig.
*
* @param string $twig get extension.
*/
public function add_to_twig( $twig ) {
$twig->addExtension( new Twig\Extension\StringLoaderExtension() );
$twig->addFilter( new Timber\Twig_Filter( 'format_session', 'format_session' ) );
return $twig;
}
}
new BorealisSite();