site_vitrine/theme/inc/template-functions.php

347 lines
10 KiB
PHP

<?php
/**
* Template Functions
*
* @package Navier_Instruments
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* Add a pingback url auto-discovery header for single posts, pages, or attachments.
*/
function navier_pingback_header() {
if (is_singular() && pings_open()) {
printf('<link rel="pingback" href="%s">', esc_url(get_bloginfo('pingback_url')));
}
}
add_action('wp_head', 'navier_pingback_header');
/**
* Add custom body classes
*
* @param array $classes Body classes
* @return array Modified body classes
*/
function navier_body_class_filter($classes) {
// Add page slug as class
if (is_single() || is_page()) {
global $post;
if (isset($post)) {
$classes[] = $post->post_type . '-' . $post->post_name;
}
}
// Add class if no sidebar
if (!is_active_sidebar('sidebar-1')) {
$classes[] = 'no-sidebar';
}
// Add class for sticky header
$classes[] = 'has-sticky-header';
return $classes;
}
add_filter('body_class', 'navier_body_class_filter');
/**
* Modify archive title
*
* @param string $title Archive title
* @return string Modified archive title
*/
function navier_archive_title($title) {
if (is_category()) {
$title = single_cat_title('', false);
} elseif (is_tag()) {
$title = single_tag_title('', false);
} elseif (is_author()) {
$title = '<span class="vcard">' . get_the_author() . '</span>';
} elseif (is_post_type_archive()) {
$title = post_type_archive_title('', false);
} elseif (is_tax()) {
$title = single_term_title('', false);
}
return $title;
}
add_filter('get_the_archive_title', 'navier_archive_title');
/**
* Modify the "read more" link text
*
* @return string Modified read more link
*/
function navier_read_more_link() {
return '<a class="read-more" href="' . esc_url(get_permalink()) . '">' . esc_html__('Read More', 'navier-instruments') . ' <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="5" y1="12" x2="19" y2="12"></line><polyline points="12 5 19 12 12 19"></polyline></svg></a>';
}
add_filter('the_content_more_link', 'navier_read_more_link');
/**
* Custom comment template
*
* @param object $comment Comment object
* @param array $args Arguments
* @param int $depth Depth
*/
function navier_comment_callback($comment, $args, $depth) {
$tag = ('div' === $args['style']) ? 'div' : 'li';
?>
<<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class(empty($args['has_children']) ? '' : 'parent', $comment); ?>>
<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
<footer class="comment-meta">
<div class="comment-author vcard">
<?php
if (0 != $args['avatar_size']) {
echo get_avatar($comment, $args['avatar_size']);
}
?>
<?php printf('<b class="fn">%s</b>', get_comment_author_link($comment)); ?>
</div>
<div class="comment-metadata">
<a href="<?php echo esc_url(get_comment_link($comment, $args)); ?>">
<time datetime="<?php comment_time('c'); ?>">
<?php
printf(
/* translators: 1: comment date, 2: comment time */
esc_html__('%1$s at %2$s', 'navier-instruments'),
get_comment_date('', $comment),
get_comment_time()
);
?>
</time>
</a>
<?php edit_comment_link(esc_html__('Edit', 'navier-instruments'), '<span class="edit-link">', '</span>'); ?>
</div>
<?php if ('0' == $comment->comment_approved) : ?>
<p class="comment-awaiting-moderation"><?php esc_html_e('Your comment is awaiting moderation.', 'navier-instruments'); ?></p>
<?php endif; ?>
</footer>
<div class="comment-content">
<?php comment_text(); ?>
</div>
<?php
comment_reply_link(array_merge($args, array(
'add_below' => 'div-comment',
'depth' => $depth,
'max_depth' => $args['max_depth'],
'before' => '<div class="reply">',
'after' => '</div>',
)));
?>
</article>
<?php
}
/**
* Custom search form
*
* @return string Search form HTML
*/
function navier_search_form() {
$form = '<form role="search" method="get" class="search-form" action="' . esc_url(home_url('/')) . '">
<label>
<span class="screen-reader-text">' . esc_html__('Search for:', 'navier-instruments') . '</span>
<input type="search" class="search-field form-input" placeholder="' . esc_attr__('Search...', 'navier-instruments') . '" value="' . get_search_query() . '" name="s" />
</label>
<button type="submit" class="search-submit btn btn--primary">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="11" cy="11" r="8"></circle>
<line x1="21" y1="21" x2="16.65" y2="16.65"></line>
</svg>
<span class="screen-reader-text">' . esc_html__('Search', 'navier-instruments') . '</span>
</button>
</form>';
return $form;
}
add_filter('get_search_form', 'navier_search_form');
/**
* Add custom image sizes to media library
*
* @param array $sizes Image sizes
* @return array Modified image sizes
*/
function navier_custom_image_sizes($sizes) {
return array_merge($sizes, array(
'navier-hero' => __('Hero Image', 'navier-instruments'),
'navier-card' => __('Card Image', 'navier-instruments'),
'navier-thumbnail' => __('Custom Thumbnail', 'navier-instruments'),
'navier-product' => __('Product Image', 'navier-instruments'),
));
}
add_filter('image_size_names_choose', 'navier_custom_image_sizes');
/**
* Modify post class
*
* @param array $classes Post classes
* @return array Modified post classes
*/
function navier_post_class_filter($classes) {
// Add animation class
$classes[] = 'animate';
$classes[] = 'animate-fade-up';
return $classes;
}
add_filter('post_class', 'navier_post_class_filter');
/**
* Defer non-critical scripts
*
* @param string $tag Script tag
* @param string $handle Script handle
* @return string Modified script tag
*/
function navier_defer_scripts($tag, $handle) {
$defer_scripts = array('navier-main');
if (in_array($handle, $defer_scripts)) {
return str_replace(' src', ' defer src', $tag);
}
return $tag;
}
add_filter('script_loader_tag', 'navier_defer_scripts', 10, 2);
/**
* Add preload for critical assets
*/
function navier_preload_assets() {
// Preload main font
echo '<link rel="preload" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Montserrat:wght@600;700;800&display=swap" as="style">' . "\n";
}
add_action('wp_head', 'navier_preload_assets', 1);
/**
* Remove default gallery styles
*/
add_filter('use_default_gallery_style', '__return_false');
/**
* Wrap oembeds in responsive container
*
* @param string $html Embed HTML
* @param string $url Embed URL
* @param array $attr Attributes
* @param int $post_id Post ID
* @return string Modified embed HTML
*/
function navier_embed_wrapper($html, $url, $attr, $post_id) {
return '<div class="embed-responsive">' . $html . '</div>';
}
add_filter('embed_oembed_html', 'navier_embed_wrapper', 10, 4);
/**
* Custom login logo
*/
function navier_login_logo() {
$custom_logo_id = get_theme_mod('custom_logo');
$logo = wp_get_attachment_image_src($custom_logo_id, 'full');
if ($logo) :
?>
<style type="text/css">
#login h1 a, .login h1 a {
background-image: url(<?php echo esc_url($logo[0]); ?>);
height: 80px;
width: 320px;
background-size: contain;
background-repeat: no-repeat;
padding-bottom: 30px;
}
</style>
<?php
endif;
}
add_action('login_enqueue_scripts', 'navier_login_logo');
/**
* Custom login logo URL
*/
function navier_login_logo_url() {
return home_url();
}
add_filter('login_headerurl', 'navier_login_logo_url');
/**
* Custom login logo title
*/
function navier_login_logo_url_title() {
return get_bloginfo('name');
}
add_filter('login_headertext', 'navier_login_logo_url_title');
/**
* Clean up wp_head
*/
function navier_cleanup_head() {
// Remove WordPress version
remove_action('wp_head', 'wp_generator');
// Remove wlwmanifest link
remove_action('wp_head', 'wlwmanifest_link');
// Remove RSD link
remove_action('wp_head', 'rsd_link');
// Remove shortlink
remove_action('wp_head', 'wp_shortlink_wp_head');
// Remove REST API link
remove_action('wp_head', 'rest_output_link_wp_head');
// Remove oEmbed discovery links
remove_action('wp_head', 'wp_oembed_add_discovery_links');
}
add_action('init', 'navier_cleanup_head');
/**
* Add schema.org structured data
*/
function navier_schema_organization() {
if (!is_front_page()) {
return;
}
$schema = array(
'@context' => 'https://schema.org',
'@type' => 'Organization',
'name' => get_bloginfo('name'),
'url' => home_url(),
'logo' => '',
'address' => array(
'@type' => 'PostalAddress',
'addressLocality' => 'Senlis',
'addressRegion' => 'Oise',
'postalCode' => '60300',
'addressCountry' => 'FR',
),
'contactPoint' => array(
'@type' => 'ContactPoint',
'telephone' => get_theme_mod('navier_phone', '+33 3 44 00 00 00'),
'email' => get_theme_mod('navier_email', 'contact@navier-instruments.com'),
'contactType' => 'customer service',
),
);
$custom_logo_id = get_theme_mod('custom_logo');
if ($custom_logo_id) {
$logo = wp_get_attachment_image_src($custom_logo_id, 'full');
if ($logo) {
$schema['logo'] = $logo[0];
}
}
echo '<script type="application/ld+json">' . wp_json_encode($schema) . '</script>' . "\n";
}
add_action('wp_head', 'navier_schema_organization');