Skip to content

Instantly share code, notes, and snippets.

@i5ar
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save i5ar/714db3ea870b8c978df0 to your computer and use it in GitHub Desktop.

Select an option

Save i5ar/714db3ea870b8c978df0 to your computer and use it in GitHub Desktop.
The iSarch theme functions file
<?php
// Sets up includes functions
include_once( ABSPATH . 'wp-content/themes/twentyfourteen-child/inc/enqueues.php' );
include_once( ABSPATH . 'wp-content/themes/twentyfourteen-child/inc/post-types.php' );
include_once( ABSPATH . 'wp-content/themes/twentyfourteen-child/inc/media-gallery.php' );
include_once( ABSPATH . 'wp-content/themes/twentyfourteen-child/inc/featured-template-tags.php' );
// Disable dashboard widgets
add_action('admin_menu', 'disable_default_dashboard_widgets');
function disable_default_dashboard_widgets() {
remove_meta_box('dashboard_incoming_links', 'dashboard', 'core');
}
// Custom dashboard widget
add_action('wp_dashboard_setup', 'add_isar_required_id_widget');
function add_isar_required_id_widget() {
wp_add_dashboard_widget('isar_required_id_widget', 'Theme info', 'isar_required_id_widget');
}
function isar_required_id_widget() {
echo "
<dl>
<dt><h4>Required page slugs:</h4></dt>
<dd><b>map</b> - <code>isar_get_id_by_slug( 'map' );</code> <pre>inc\scraps\contacts_map.php</pre></dd>
<dd><b>tweets</b> - <code>isar_get_id_by_slug( 'tweets' );</code> <pre>inc\scraps\about_tweets.php</pre></dd>
<dd><b>contacts</b> - <code>isar_get_id_by_slug( 'contacts' );</code> <pre>inc\scraps\slide_out.php</pre></dd>
<dd><b>availability</b> - <code>isar_get_id_by_slug( 'availability' );</code> <pre>inc\scraps\about_availability.php,<br />inc\scraps\about_availability.php</pre></dd>
</dl>";
}
// Add menus
function register_my_menus() {
register_nav_menus(
array(
'sectors-menu' => __( 'Sectors Menu', 'twentyfourteen' ),
'events-menu' => __( 'Events Menu', 'twentyfourteen' )
)
);
}
add_action( 'init', 'register_my_menus' );
/**
* Add sidebars
*/
// Add third sidebar
function third_sidebar() {
register_sidebar(
array(
'name' => __( 'Contacts Sidebar', 'twentyfourteen' ),
'id' => 'sidebar-4',
'description' => __( 'Appears on posts and pages in the sidebar.', 'twentyfourteen' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
)
);
dynamic_sidebar( 'sidebar-4' );
}
add_action( 'widgets_init', 'third_sidebar' );
// Add fourth sidebar
function fourth_sidebar() {
register_sidebar(
array(
'name' => __( 'Resources Sidebar', 'twentyfourteen' ),
'id' => 'sidebar-5',
'description' => __( 'Appears on Total Resources Page Template.', 'twentyfourteen' ),
'before_widget' => '<aside id="%1$s" class="widget widget_nav_menu">', // widget_nav_menu i5
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
)
);
dynamic_sidebar( 'sidebar-5' );
}
add_action( 'widgets_init', 'fourth_sidebar' );
// Add fifth sidebar
function fifth_sidebar() {
register_sidebar(
array(
'name' => __( 'Floating Menu Sidebar', 'twentyfourteen' ),
'id' => 'sidebar-6',
'description' => __( 'Appears on Floating Menu Template sidebar.', 'twentyfourteen' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
)
);
dynamic_sidebar( 'sidebar-6' );
}
add_action( 'widgets_init', 'fifth_sidebar' );
// Add sixth sidebar
function sixth_sidebar() {
register_sidebar(
array(
'name' => __( 'Projects Sidebar', 'twentyfourteen' ),
'id' => 'sidebar-7',
'description' => __( 'Appears on Projects Single sidebar.', 'twentyfourteen' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
)
);
}
add_action( 'widgets_init', 'sixth_sidebar' );
// Add seventh sidebar
add_action( 'widgets_init', 'seventh_sidebar' );
function seventh_sidebar() {
register_sidebar(
array(
'name' => __( 'Content Page Sidebar', 'twentyfourteen' ),
'id' => 'sidebar-8',
'description' => __( 'Appears on pages in the sidebar.', 'twentyfourteen' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
)
);
}
// Remove Private and Protected text in post titles
add_filter( 'the_title', 'the_title_trim' );
function the_title_trim($title) {
$pattern[0] = '/Protected:/';
$pattern[1] = '/Private:/';
$replacement[0] = 'Pw:';
$replacement[1] = 'Pr:';
return preg_replace($pattern, $replacement, $title);
}
/**
* Remove specific post class twice
*/
// Remove specific post class from Team Archive
add_filter( 'post_class', 'remove_projects_has_post_thumbnail_class', 20 );
function remove_projects_has_post_thumbnail_class( $classes ) {
if( is_singular( 'projects' )
|| is_page_template( 'page-templates/maintenance.php' )
|| is_page_template( 'page-templates/about.php' )
|| is_post_type_archive( 'team' )
|| is_singular( 'press' )
){
foreach( $classes as $key => $value ){
if( $value=='has-post-thumbnail' ){ unset( $classes[$key] ); }
}
}
return $classes;
}
/**
* Remove specific body class
*/
// Remove full-width class to prevent enlarged articles when the content sidebar is inactive
add_filter( 'body_class', 'remove_full_width_class', 11 );
function remove_full_width_class( $classes ) {
if( is_post_type_archive( 'curriculum' )
) $classes = array_diff( $classes, array( 'full-width' ) );
return $classes;
}
// Remove custom-background class class
add_filter( 'body_class', 'remove_my_class', 11 );
function remove_my_class( $classes ) {
if( is_tag( 'remove-custom-background' )
|| is_page_template( 'page-templates/about.php' )
|| is_page_template( 'page-templates/maintenance.php' )
) $classes = array_diff( $classes, array( 'custom-background' ) );
return $classes;
}
/**
* Add specific body class
*/
// Add 'full-width loose' body classes
add_filter( 'body_class', 'full_width_loose_body_class' );
function full_width_loose_body_class( $classes ) {
if( is_page_template( 'page-templates/full-width-loose-total.php' )
|| is_post_type_archive( 'journal' )
) $classes[] = 'full-width loose';
return $classes;
}
// Add 'featured-view' body class to style the main page
add_filter( 'body_class', 'featured_view_body_class' );
function featured_view_body_class( $classes ) {
if( is_page_template( 'page-templates/main.php' )
) $classes[] = 'featured-view';
return $classes;
}
// Add 'total' body class
add_filter( 'body_class', 'total_body_class' );
function total_body_class( $classes ) {
if( is_tag( 'total' )
|| is_singular( 'projects' )
|| is_singular( 'press' )
|| is_singular( 'curriculum' )
|| is_singular( 'freebies' )
|| is_singular( 'team' )
|| is_singular( 'journal' )
|| is_page()
|| is_post_type_archive( 'team' )
|| is_post_type_archive( 'projects' )
|| is_post_type_archive( 'press' )
|| is_post_type_archive( 'curriculum' )
|| is_post_type_archive( 'journal' )
|| is_post_type_archive( 'freebies' )
|| is_post_type_archive( 'feeds' )
|| is_tax( 'sector' )
|| is_tax( 'pursuit' )
|| is_tax( 'architect' )
|| is_tax( 'manager' )
|| is_tax( 'location' )
|| is_tax( 'client' )
|| is_tax( 'years' )
) $classes[] = 'total';
return $classes;
}
// Add 'transparent' body class
add_filter( 'body_class', 'transparent_body_class' );
function transparent_body_class( $classes ) {
if( is_page_template( 'page-templates/about.php' )
|| is_page_template( 'page-templates/maintenance.php' )
) $classes[] = 'transparent';
return $classes;
}
// Add page slug body class to detect the script to enqueue
add_filter( 'body_class', 'add_slug_body_class' );
function add_slug_body_class( $classes ) {
global $post;
if ( isset( $post ) ) { $classes[] = $post->post_type . '-' . $post->post_name; }
return $classes;
}
// Add Curriculum post-type taxonomies archive to specific template
add_filter( 'archive_template', 'get_custom_tax_template' );
function get_custom_tax_template( $tax_template ) {
global $post;
if( is_tax ( 'years' )
|| is_tax ( 'manager' )
|| is_tax ( 'location' )
|| is_tax ( 'client' )
) { $tax_template = dirname( __FILE__ ) . '/taxonomy-architect.php'; }
return $tax_template;
}
// Add Press post-type archive and single to specific template
add_filter( 'archive_template', 'get_archive_projects_template' );
function get_archive_projects_template( $archive_template ) {
global $post;
if( is_post_type_archive( 'press' )
) { $archive_template = dirname( __FILE__ ) . '/archive-projects.php'; }
return $archive_template;
}
add_filter( 'single_template', 'get_single_projects_template' );
function get_single_projects_template( $single_template ) {
global $post;
if( is_singular( 'press' )
|| is_singular( 'curriculum' )
) { $single_template = dirname( __FILE__ ) . '/single-freebies.php'; }
return $single_template;
}
// Add Agenda pages to spacific template
add_filter( 'page_template', 'get_agenda_page_template' );
function get_agenda_page_template( $page_template ) {
global $post;
// Get page ID by slug
$id = get_page_by_path( 'agenda' )->ID;
// Switch language
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if( is_plugin_active( 'polylang/polylang.php' ) ) {
$id_it = pll_get_post( $id, 'it' );
$id_en = pll_get_post( $id, 'en' );
// Get template
if( is_page( $id_it )
|| is_page( $id_en )
) { $page_template = dirname( __FILE__ ) . '/page-agenda.php'; }
}
return $page_template;
}
// Add Services pages to specific template
add_filter( 'page_template', 'get_custom_page_template' );
function get_custom_page_template( $page_template ) {
global $post;
if( is_page ( 'servizi' ) ) { $page_template = dirname( __FILE__ ) . '/page-services.php'; }
return $page_template;
}
// Return an ID of an attachment by searching the database
function isar_get_attachment_id_by_url( $url ) {
$parse_url = explode( parse_url( WP_CONTENT_URL, PHP_URL_PATH ), $url );
$this_host = str_ireplace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
$file_host = str_ireplace( 'www.', '', parse_url( $url, PHP_URL_HOST ) );
if ( ! isset( $parse_url[1] ) || empty( $parse_url[1] ) || ( $this_host != $file_host ) )
return;
global $wpdb;
$prefix = $wpdb->prefix;
$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM " . $prefix . "posts WHERE guid RLIKE %s;", $parse_url[1] ) );
return $attachment[0];
}
// Return ID Page by Slug
function isar_get_id_by_slug( $page_slug ) {
$page = get_page_by_path( $page_slug );
if ( $page ) {
return $page->ID;
} else {
return null;
}
}
// Translate
add_action( 'after_setup_theme', 'my_child_theme_setup' );
function my_child_theme_setup() {
load_child_theme_textdomain( 'twentyfourteen-child', get_stylesheet_directory() . '/languages' );
}
// Drop down for taxonomies
function isar_taxonomy_dropdown(
$taxonomy,
$orderby = 'date',
$order = 'DESC',
$number = '-1',
$name,
$show_option_all = null,
$show_option_none = null
) {
$args = array(
'orderby' => $orderby,
'order' => $order,
'number' => $number,
);
$terms = get_terms( $taxonomy, $args );
$name = ( $name ) ? $name : $taxonomy;
if ( $terms ) {
printf( '<select name="%s" class="postform">', esc_attr( $name ) );
if ( $show_option_all ) { printf( '<option value="0">%s</option>', esc_html( $show_option_all ) ); }
if ( $show_option_none ) { printf( '<option value="-1">%s</option>', esc_html( $show_option_none ) ); }
foreach ( $terms as $term ) { printf( '<option value="%s">%s</option>', esc_attr( $term->slug ), esc_html( $term->name ) ); }
print( '</select>' );
}
}
// Rename post format
add_filter( 'esc_html', 'rename_post_formats' );
function rename_post_formats( $safe_text ) {
if ( $safe_text == _x( 'Aside', 'Post format' ) )
return _x( 'Snippet', 'Post format', 'twentyfourteen-child' );
return $safe_text;
}
// Replace Image post format content
add_filter( 'the_content', 'isar_image_post_format_content' );
function isar_image_post_format_content($content) {
global $post;
$format = get_post_format();
if( $format == 'image' ) {
$content = '<img class="attachment-post-thumbnail wp-post-image" src="';
$content .= get_the_content();
$content .= '" alt="';
$content .= get_the_title();
$content .= '" />';
}
return $content;
}
// Append meta-box to post content
add_filter( 'the_content', 'isar_append_post_content' );
function isar_append_post_content($content) {
global $post;
if( is_singular('post') == true
|| is_singular('press') == true
|| is_singular('curriculum') == true ) {
$reading_a = get_post_meta($post->ID, '_meta_reading_a_key', true);
$reading_b = get_post_meta($post->ID, '_meta_reading_b_key', true);
$reading_c = get_post_meta($post->ID, '_meta_reading_c_key', true);
$anchor_a = get_post_meta($post->ID, '_meta_anchor_a_key', true);
$anchor_b = get_post_meta($post->ID, '_meta_anchor_b_key', true);
$anchor_c = get_post_meta($post->ID, '_meta_anchor_c_key', true);
// Print More info
if( $reading_a != '' ) {
$content .= '<div class="entry-meta-tax">';
$content .= __( 'More info:', 'twentyfourteen-child' );
$content .= '<ul>';
$content .= '<li><a href="'.$reading_a.'" target="_blank">'. $anchor_a .'</a></li>';
if ( $reading_b != '' ) { $content .= '<li><a href="'.$reading_b.'" target="_blank">'. $anchor_b .'</a></li>'; }
if ( $reading_c != '' ) { $content .= '<li><a href="'.$reading_c.'" target="_blank">'. $anchor_c .'</a></li>'; }
$content .= '</ul>';
$content .= '</div>';
}
}
return $content;
}
/**
* Automatically close comments
*/
// Close comments in single post
add_filter( 'the_posts', 'close_comments' );
function close_comments( $posts ) {
$post_type = array( 'press', 'feeds' );
if ( !is_single() ) { return $posts; }
foreach( $post_type as $value ) {
if( $value == get_post_type($posts[0]->ID) ) {
$posts[0]->comment_status = 'closed';
$posts[0]->ping_status = 'closed';
wp_update_post( $posts[0] );
}
}
return $posts;
}
// Remove single post tag from blog
add_action( 'pre_get_posts', 'isar_remove_post_from_blog_by_tag' );
function isar_remove_post_from_blog_by_tag( $query ) {
// Get tag ID by slug
$tag_by_slug = get_term_by('slug', 'about', 'post_tag');
$tag_id = $tag_by_slug->term_id;
// Language
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if( is_plugin_active( 'polylang/polylang.php' ) ):
$current_tag_id = $tag_id;
$language = get_bloginfo( 'language' );
if ( $language == 'it-IT' ) { $tag_id = pll_get_term( $current_tag_id, 'it' ); }
endif;
// Remove post within specific tag ID
if( $query->is_main_query() && $query->is_home() ) {
$query->set( 'tag__not_in', $tag_id );
}
}
// Append redirect to head
add_action('wp_head','isar_language_redirect');
function isar_language_redirect() {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if( is_plugin_active( 'polylang/polylang.php' ) ) {
if( is_post_type_archive( 'freebies' ) && pll_current_language() !== 'en' ) {
$languages = pll_the_languages( array( 'raw' => 1 ) );
foreach( $languages as $language ) {
if( $language['slug'] == 'en' ){
// Get the english language archive url
$url = $language['url'];
// Redirect to the english language archive
$redirect='<meta http-equiv="refresh" content="0; url='.$url.'" />';
echo $redirect;
}
}
}
// Show Popup
if( is_post_type_archive( 'freebies' ) ) {
$popup = '
<script>
jQuery(document).ready(function($) {
// Get MODAL language
if("en" !== $.cookie("pll_language_modal_ic") ) {
setTimeout(function() {
$("#slide").popup("show");
}, 3000);
}
});
</script>';
echo $popup;
}
}
}
// Popup cookie language
function isar_cookie_language_button( $button_class = 'hide' ) {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if( is_plugin_active( 'polylang/polylang.php' )) {
$previous_page_id = url_to_postid($_SERVER['HTTP_REFERER']);
$previous_language_pll = pll_get_post_language($previous_page_id); // works only with pages not archives
$current_language_pll = pll_current_language();
if(( $previous_language_pll != $current_language_pll && $previous_language_pll != '' ) || $previous_language_pll == '' ) { ?>
<button class="slide_open <?php echo $button_class; ?>"><?php _e( 'Language', 'twentyfourteen-child' ); ?></button>
<br />
<div id="slide" class="page-content">
<p class="">
<?php // Polylang current language
echo __( 'You are in the section', 'twentyfourteen-child' ) .' "'. pll_current_language( 'locale' ) .'" '. __( 'of the website therefore you may find some mismatch of contents with other languages.', 'twentyfourteen-child' ); ?>
</p>
<ul class="list-inline comma">
<?php // Polylang available languages
_e( 'Available contents: ', 'twentyfourteen-child' );
pll_the_languages(); ?>
</ul>
<br/>
<button class="slide_close btn-block"><?php _e( 'Close', 'twentyfourteen-child' ); ?></button>
</div>
<?php
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment