tag in the document head, and expect WordPress to
* provide it for us.
*/
add_theme_support('title-tag');
/**
* Theme actions
*/
// manage redirects
// add_action('template_redirect', 'sandbox_template_redirect');
// load scripts
add_action('wp_enqueue_scripts', 'sandbox_enqueue_scripts');
if ( !is_admin() ) {
add_action('script_loader_tag', 'sandbox_script_loader_tag', 10, 2);
}
// init sidebars
add_action('widgets_init', 'sandbox_sidebars_init');
/**
* Theme filters
*/
// add shortcodes to widgets
add_filter('widget_text', 'do_shortcode');
add_filter('widget_title', 'do_shortcode');
// add shortcodes to the excerpt
add_filter('the_excerpt', 'shortcode_unautop');
add_filter('the_excerpt', 'do_shortcode');
// Adds filters for the description/meta content in archives.php
add_filter('archive_meta', 'wptexturize');
add_filter('archive_meta', 'convert_smilies');
add_filter('archive_meta', 'convert_chars');
add_filter('archive_meta', 'wpautop');
// remove style attribute from cancel_comment_reply_link
add_filter('cancel_comment_reply_link', 'sandbox_cancel_comment_reply_link', 10, 3);
// filters the Categories archive widget to include the post count inside the link
// https://www.sodawebmedia.com/blog/display-post-counts-within-link-for-categories-and-archive/
add_filter('wp_list_categories', 'sandbox_count_span');
// filters the Archive widget to include the post count inside the link
add_filter('get_archives_link', 'sandbox_count_span');
}
/*
* Setup hooks
* ===========
*/
function sandbox_template_redirect(){
// hide profile pages from not logged in users
$profile_page_ID = array(5142, 5480);
if ( !is_user_logged_in() && ( sandbox_is_page_or_descendant($profile_page_ID[0]) || sandbox_is_page_or_descendant($profile_page_ID[1]) ) ) {
wp_redirect(home_url());
die;
}
}
// load scripts
function sandbox_enqueue_scripts(){
$profile_post_ID = array(5142, 5480); // on profile page or descendants
$post_id = get_the_ID();
$post = get_post();
$post_type = get_post_type();
$post_type_object = get_post_type_object($post_type);
$post_type_rest_base = $post_type;
if ( !is_null($post_type_object) && property_exists($post_type_object, 'rest_base') && is_string($post_type_object->rest_base) ) {
$post_type_rest_base = $post_type_object->rest_base;
}
if ( !is_admin() ) {
// jquery plugins
wp_enqueue_script('jquery-plugins', get_template_directory_uri() . '/js/jquery.plugins.js', array('jquery'));
// wp_enqueue_script('jquery-ui-css', get_template_directory_uri() . '/css/jquery-ui.theme.min.css');
// comments reply script
if ( is_singular() && comments_open() && (get_option('thread_comments') == 1 ) ) {
wp_enqueue_script('comment-reply');
}
// remove wp-embed.min.js
// https://wordpress.stackexchange.com/a/285907/81728
wp_deregister_script('wp-embed');
// Javascript data for window.sandboxTheme_data
$data = array(
'id' => $post_id,
'urls' => array(
'ajax' => admin_url('admin-ajax.php'),
'template' => get_stylesheet_directory_uri(),
'rest' => rest_url('wp/v2/' . $post_type_rest_base . '/' . $post_id . '/'),
),
'verify' => wp_create_nonce('sandbox_ajax_call'),
'name' => sandbox_get_the_slug(),
'categories' => get_the_category(),
// 'terms' => get_the_terms(),
'meta' => sandbox_get_post_meta(),
'info' => array(
'is_home' => is_home(),
'is_front_page' => is_front_page(),
'is_category' => is_category(),
'is_archive' => is_archive(),
'is_admin' => is_admin(),
'is_single' => is_single(),
'has_content_form' => !is_null($post) && has_shortcode($post->post_content, 'cf7form'),
'comments_open' => comments_open(),
'is_page' => is_page(),
'is_tag' => is_tag(),
'is_tax' => is_tax(),
'is_author' => is_author(),
'is_search' => is_search(),
'is_singular' => is_singular(),
'is_user_logged_in' => is_user_logged_in(),
'post_type' => $post_type,
),
);
// custom script: edit there for your own needs
wp_register_script('actions', get_template_directory_uri() . '/js/actions.js', array('jquery-core'), '3', true);
// Javascript data for window.sandboxTheme_data
wp_localize_script('actions', 'sandboxTheme_data', $data );
wp_enqueue_script('actions');
// custom script: edit there for your own needs
}
/*
if ( is_user_logged_in() && ( is_page($profile_post_ID[0]) || is_page($profile_post_ID[1]) ) ) {
wp_enqueue_script('wp-util');
wp_enqueue_script('password-strength-meter');
}
*/
}
function sandbox_script_loader_tag($tag, $handle){
// add script handles to the array below
$scripts_to_defer = array(
'jquery-core',
'jquery-migrate',
'jquery-plugins',
'actions',
'query-monitor',
'easy-swipebox-init',
'easy-swipebox',
'contact-form-7',
'cf7-grid-layout',
);
if ( in_array($handle, $scripts_to_defer) && strpos($tag, 'defer') === false ) {
$tag = str_replace(' src', ' defer="defer" src', $tag);
}
return $tag;
}
// init sidebars
function sandbox_sidebars_init() {
if ( !function_exists('register_sidebar') ) {
return;
}
// enter your sidebars here
$sidebars = array('header', 'menu', 'highlight', 'content-before', 'content-after', 'content-footer', 'footer', 'footer-bar');
foreach ( $sidebars as $sidebar ) {
$config = array(
'name' => $sidebar,
'id' => $sidebar,
'before_widget' => '
',
'before_title' => '',
'after_title' => '
'
);
// Special markup for some sidebars
if ( $sidebar === 'menu') {
$config['before_widget'] = '';
}
else if ( $sidebar === 'footer') {
$config['before_widget'] = '';
}
else if ( $sidebar === 'header') {
$config['before_widget'] = '';
$config['after_widget'] = '
';
}
else if ( $sidebar === 'content-footer') {
$config['before_widget'] = '';
}
else if ( $sidebar === 'content-after') {
$config['before_widget'] = '';
}
register_sidebar($config);
}
}
// filters to include the post count inside any link
function sandbox_count_span($output){
$output = str_replace(' (', ' (', $output);
$output = str_replace(')', ')', $output);
return $output;
}
/**
* Template helper functions
* =========================
*/
// is a given page ID the anchestor of the current page?
function sandbox_is_page_or_descendant($post_ID){
$is_page_or_descendant = false;
if ( is_page($post_ID) ) {
$is_page_or_descendant = true;
} elseif ( is_post_type_hierarchical(get_post_type()) ) {
$post = get_post();
$ancestors = get_post_ancestors($post->ID);
if ( in_array($post_ID, $ancestors) ) {
$is_page_or_descendant = true;
}
}
return $is_page_or_descendant;
}
// Page slug
function sandbox_get_the_slug(){
$slug = basename(get_permalink());
do_action('before_slug', $slug);
$slug = apply_filters('slug_filter', $slug);
do_action('after_slug', $slug);
return $slug;
}
function sandbox_the_slug(){
echo sandbox_get_the_slug();
}
// Page description
function sandbox_get_page_description($sep, $num_words, $meta = true){
if ( !is_string($sep) || empty($sep) ) { $sep = '-'; }
if ( !is_numeric($num_words) || empty($num_words) ) { $num_words = 40; }
$description = wp_title($sep, false);
$sep = ' ' . $sep . ' ';
if ( is_404() ) {
$description = __( 'Apologies, but we were unable to find what you were looking for.', 'socio' );
} else if ( is_single() || is_page() || ( is_front_page() && !is_home() ) ) {
$post = get_post(get_the_ID());
if ( $post_seo_description = get_post_meta($post->ID, '_seo-description', true) ) {
$description = $post_seo_description;
} else if ( has_excerpt() ) {
$description = $post->post_excerpt;
} else {
$description = $post->post_content;
}
} else {
if ( is_archive() || is_home() ) {
if ( is_tax() || is_tag() || is_category() || is_author() ) {
if ( is_tax() || is_tag() || is_category() ) {
$termTitle = single_term_title('', false);
$termDesc = term_description();
$taxonomyTitle = get_taxonomy(get_queried_object()->taxonomy)->labels->singular_name;
}
if ( is_author() ) {
$termTitle = get_the_author_meta('display_name');
$termDesc = get_the_author_meta('description');
$taxonomyTitle = __('Author', 'socio');
}
if ( !empty($termDesc) ) {
$termDesc = $sep . $termDesc;
}
$description = $taxonomyTitle . ' ' . __('archive', 'socio') . $sep . $termTitle . $termDesc ;
}
if ( is_home() && ( $posts_page = get_option('page_for_posts') ) ) {
$description = get_the_title($posts_page);
} else{
$description = get_option('blogname');
}
if ( is_search() ) {
$description = __('Search Results For', 'socio') . ' ' . get_search_query();
}
}
$description .= $sep . get_bloginfo('description');
}
return wp_trim_words(filter_var($meta, FILTER_VALIDATE_BOOLEAN) ? esc_html(wp_strip_all_tags($description, true)) : $description, $num_words);
}
function sandbox_the_page_description($sep = '-', $num_words = 40, $meta = true){
echo sandbox_get_page_description($sep, $num_words, $meta);
}
// Page thumbnail
function sandbox_get_page_thumbnail(){
$page_thumbnail = get_theme_mod('sandbox_logo');
if ( is_single() && has_post_thumbnail() ) {
$postThumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(),'large', false);
$page_thumbnail = $postThumbnail[0];
} else if ( !empty($page_thumbnail) ) {
$page_thumbnail = esc_url($page_thumbnail);
} else {
$page_thumbnail = esc_url(get_template_directory_uri()) . '/img/logo-basisbildung.png';
}
return $page_thumbnail;
}
function sandbox_the_page_thumbnail(){
echo sandbox_get_page_thumbnail();
}
// Current page URI
function sandbox_get_current_URI(){
$protocol = ( ( !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443 ) ? 'https://' : 'http://';
return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
function sandbox_the_current_URI(){
echo sandbox_get_current_URI();
}
// Post meta single output for all fields
function sandbox_get_post_meta($post_id=0){
$numeric_fields = array('_alpha_file_count', '_favorites', '_wpcr_rating', '_thumbnail_id');
$serialized_fields = array('_more-link', '_alpha_file_options', '_section-linked', '_conditions-table', '_links-internal', '_links-external');
$post_id = absint($post_id);
if ( !$post_id ) {
$post_id = get_the_ID();
}
$post_custom = get_post_custom($post_id);
$post_meta_keys = get_post_custom_keys($post_id);
$post_meta = array();
if ( $post_custom !== false ) {
foreach ( $post_meta_keys as $post_meta_key ) {
if ( !empty($post_custom[$post_meta_key][0]) ) {
$post_meta[$post_meta_key] = $post_custom[$post_meta_key][0];
// exeptions
// numeric field type
if ( in_array($post_meta_key, $numeric_fields) ) {
$post_meta[$post_meta_key] = intval($post_custom[$post_meta_key][0]);
}
// serialized field type
if ( in_array($post_meta_key, $serialized_fields) ) {
$post_meta[$post_meta_key] = unserialize($post_custom[$post_meta_key][0]);
}
} else {
$post_meta[$post_meta_key] = '';
// exeptions
// numeric field type
if ( in_array($post_meta_key, $numeric_fields) ) {
$post_meta[$post_meta_key] = 0;
}
// serialized field type
if ( in_array($post_meta_key, $serialized_fields) ) {
$post_meta[$post_meta_key] = array();
}
}
}
}
return $post_meta;
}
// Get the first tags title
function sandbox_get_the_tag_title(){
$terms = get_the_terms(0, 'post_tag');
if ( is_wp_error( $terms ) ){ return $terms; }
if ( empty( $terms ) ){ return false; }
foreach ( $terms as $term ) {
$term_names[] = $term->name;
}
return $term_names[0];
}
function sandbox_the_tag_title(){
echo sandbox_get_the_tag_title();
}
// Get the first tags slug
function sandbox_get_the_tag_slug(){
$terms = get_the_terms(0, 'post_tag');
if ( is_wp_error( $terms ) ){ return $terms; }
if ( empty( $terms ) ){ return false; }
foreach ( $terms as $term ) {
if ( $term->slug != 'kolumne') {
$term_slugs[] = $term->slug;
}
}
return $term_slugs[0];
}
function sandbox_the_tag_slug(){
echo sandbox_get_the_tag_slug();
}
// Get the first tags ID
function sandbox_get_the_tag_id(){
$terms = get_the_terms(0, 'post_tag');
if ( is_wp_error($terms) ){ return $terms; }
if ( empty($terms) ){ return false; }
foreach ( $terms as $term ) {
$term_ids[] = $term->term_id;
}
return $term_ids[0];
}
function sandbox_the_tag_id(){
echo sandbox_get_the_tag_id();
}
// Output from +431234567891 to +43 1 234 56 78 - 91
function sandbox_sanitize_phone($phone) {
$data = array(
substr($phone, 0,3),
substr($phone, 3,1),
substr($phone, 4,3),
substr($phone, 7,2),
substr($phone, 9,2),
substr($phone, 11,2),
);
if ( !empty($data[5]) ) {
$data[5] = ' - ' . $data[5];
}
return $data[0] . ' ' . $data[1] . ' ' . $data[2] . ' ' . $data[3] . ' ' . $data[4] . $data[5];
}
// output from +436641234567 to +43 664 123 45 - 67
function sandbox_sanitize_mobile_phone($phone) {
$data = array(
substr($phone, 0,3),
substr($phone, 3,3),
substr($phone, 6,3),
substr($phone, 9,2),
substr($phone, 11,2),
);
if ( !empty($data[4]) ) {
$data[4] = ' - ' . $data[4];
}
return $data[0] . ' ' . $data[1] . ' ' . $data[2] . ' ' . $data[3] . ' ' . $data[4];
}
// Comment cancel reply link
function sandbox_cancel_comment_reply_link($formatted_link, $link, $text){
return '';
}
// Comment Item, callback for wp_list_comments()
function sandbox_comment_item($comment, $args, $depth){
if ( !isset($GLOBALS['comment']) ) {
$GLOBALS['comment'] = $comment;
}
?>
' . $meta . '';
}
}
}
return $output;
}
// Remember: the sandbox is for play.
?>