<?php
/**
* Dolfansnyc functions and definitions
*
* @link https://developer.wordpress.org/themes/basics/theme-functions/
*
* @package Dolfansnyc
*/
if ( ! defined( 'DOLFANSNYC_VERSION' ) ) {
// Replace the version number of the theme on each release.
define( 'DOLFANSNYC_VERSION', '1.0.0' );
}
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* Note that this function is hooked into the after_setup_theme hook, which
* runs before the init hook. The init hook is too late for some features, such
* as indicating support for post thumbnails.
*/
function dolfansnyc_setup() {
/*
* Make theme available for translation.
* Translations can be filed in the /languages/ directory.
* If you're building a theme based on Dolfansnyc, use a find and replace
* to change 'dolfansnyc' to the name of your theme in all the template files.
*/
load_theme_textdomain( 'dolfansnyc', 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' );
// This theme uses wp_nav_menu() in one location.
register_nav_menus(
array(
'menu-1' => esc_html__( 'Primary', 'dolfansnyc' ),
)
);
/*
* Switch default core markup for search form, comment form, and comments
* to output valid HTML5.
*/
add_theme_support(
'html5',
array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
'style',
'script',
)
);
// Set up the WordPress core custom background feature.
add_theme_support(
'custom-background',
apply_filters(
'dolfansnyc_custom_background_args',
array(
'default-color' => 'ffffff',
'default-image' => '',
)
)
);
// Add theme support for selective refresh for widgets.
add_theme_support( 'customize-selective-refresh-widgets' );
/**
* Add support for core custom logo.
*
* @link https://codex.wordpress.org/Theme_Logo
*/
add_theme_support(
'custom-logo',
array(
'height' => 250,
'width' => 250,
'flex-width' => true,
'flex-height' => true,
)
);
}
add_action( 'after_setup_theme', 'dolfansnyc_setup' );
/**
* Set the content width in pixels, based on the theme's design and stylesheet.
*
* Priority 0 to make it available to lower priority callbacks.
*
* @global int $content_width
*/
function dolfansnyc_content_width() {
$GLOBALS['content_width'] = apply_filters( 'dolfansnyc_content_width', 640 );
}
add_action( 'after_setup_theme', 'dolfansnyc_content_width', 0 );
/**
* Register widget area.
*
* @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
*/
function dolfansnyc_widgets_init() {
register_sidebar(
array(
'name' => esc_html__( 'Sidebar', 'dolfansnyc' ),
'id' => 'sidebar-1',
'description' => esc_html__( 'Add widgets here.', 'dolfansnyc' ),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
)
);
}
add_action( 'widgets_init', 'dolfansnyc_widgets_init' );
/**
* Enqueue scripts and styles.
*/
function dolfansnyc_scripts() {
wp_enqueue_style( 'dolfansnyc-style', get_stylesheet_uri(), array(), DOLFANSNYC_VERSION );
wp_style_add_data( 'dolfansnyc-style', 'rtl', 'replace' );
// Enqueue elevate-defaults CSS file first
wp_enqueue_style( 'dolfansnyc-elevate-defaults', get_template_directory_uri() . '/elevate-defaults.css', array(), DOLFANSNYC_VERSION );
// Enqueue additional CSS file with dependency on elevate-defaults
wp_enqueue_style( 'dolfansnyc-custom', get_template_directory_uri() . '/dolfansnyc.css', array( 'dolfansnyc-elevate-defaults' ), DOLFANSNYC_VERSION );
wp_enqueue_style( 'font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css', array(), '6.4.0' );
wp_enqueue_script( 'dolfansnyc-navigation', get_template_directory_uri() . '/js/navigation.js', array(), DOLFANSNYC_VERSION, true );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
}
add_action( 'wp_enqueue_scripts', 'dolfansnyc_scripts' );
/**
* Implement the Custom Header feature.
*/
require get_template_directory() . '/inc/custom-header.php';
/**
* Custom template tags for this theme.
*/
require get_template_directory() . '/inc/template-tags.php';
/**
* Functions which enhance the theme by hooking into WordPress.
*/
require get_template_directory() . '/inc/template-functions.php';
/**
* Customizer additions.
*/
require get_template_directory() . '/inc/customizer.php';
/**
* Load Jetpack compatibility file.
*/
if ( defined( 'JETPACK__VERSION' ) ) {
require get_template_directory() . '/inc/jetpack.php';
}
/**
* Load WooCommerce compatibility file.
*/
if ( class_exists( 'WooCommerce' ) ) {
require get_template_directory() . '/inc/woocommerce.php';
}
/**
* Extract the first image from post content
*
* @param int $post_id The post ID
* @return string|false The image URL or false if no image found
*/
function dolfansnyc_get_first_image_from_content($post_id = null) {
if (!$post_id) {
$post_id = get_the_ID();
}
$post = get_post($post_id);
$content = $post->post_content;
// Look for img tags
preg_match_all('/<img[^>]+src=([\'"])?([^\'"\s>]+)/i', $content, $matches);
if (!empty($matches[2])) {
return $matches[2][0];
}
// Look for WordPress image shortcodes
preg_match('/\[gallery.*ids=["\']([^"\']+)["\'].*\]/i', $content, $gallery_matches);
if (!empty($gallery_matches[1])) {
$image_ids = explode(',', $gallery_matches[1]);
if (!empty($image_ids[0])) {
$image_url = wp_get_attachment_image_src($image_ids[0], 'large');
if ($image_url) {
return $image_url[0];
}
}
}
return false;
}
/**
* Get post thumbnail or first image from content or placeholder
*
* @param string $size Image size
* @param int $post_id The post ID
* @return string HTML for the image or placeholder
*/
function dolfansnyc_get_post_image($size = 'large', $post_id = null) {
if (!$post_id) {
$post_id = get_the_ID();
}
// First, check for featured image
if (has_post_thumbnail($post_id)) {
return get_the_post_thumbnail($post_id, $size);
}
// If no featured image, try to get first image from content
$first_image_url = dolfansnyc_get_first_image_from_content($post_id);
if ($first_image_url) {
return '<img src="' . esc_url($first_image_url) . '" alt="' . esc_attr(get_the_title($post_id)) . '" class="attachment-' . $size . ' size-' . $size . ' wp-post-image">';
}
// If no image found, return default theme image
$default_image_url = get_template_directory_uri() . '/images/dolfansnyc-black.jpg';
return '<img src="' . esc_url($default_image_url) . '" alt="' . esc_attr(get_the_title($post_id)) . '" class="attachment-' . $size . ' size-' . $size . ' wp-post-image">';
}
/**
* Remove the excerpt ellipsis
*/
function dolfansnyc_excerpt_more($more) {
return '';
}
add_filter('excerpt_more', 'dolfansnyc_excerpt_more');
/**
* Modify posts per page based on device type
*/
function dolfansnyc_modify_posts_per_page($query) {
// Only modify the main query on the front page
if (!is_admin() && $query->is_main_query() && is_home()) {
// Check if mobile parameter is set
if (isset($_GET['mobile']) && $_GET['mobile'] === '1') {
$query->set('posts_per_page', 5);
} else {
// Default desktop posts per page
$query->set('posts_per_page', 20);
}
}
}
add_action('pre_get_posts', 'dolfansnyc_modify_posts_per_page');
/**
* Add mobile detection script
*/
function dolfansnyc_mobile_detection_script() {
if (is_home()) {
?>
<script>
(function() {
function isMobile() {
return window.innerWidth <= 768;
}
function updatePostsForDevice() {
const currentUrl = new URL(window.location);
const isMobileDevice = isMobile();
if (isMobileDevice) {
currentUrl.searchParams.set('mobile', '1');
} else {
currentUrl.searchParams.delete('mobile');
}
// Only reload if the URL would actually change
if (currentUrl.search !== window.location.search) {
window.location.href = currentUrl.toString();
}
}
// Check on page load
updatePostsForDevice();
// Check on window resize
let resizeTimer;
window.addEventListener('resize', function() {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(updatePostsForDevice, 250);
});
})();
</script>
<?php
}
}
add_action('wp_head', 'dolfansnyc_mobile_detection_script');
/**
* Register ACF fields for social media links on About page
*/
function dolfansnyc_register_acf_fields() {
if (function_exists('acf_add_local_field_group')) {
// Social Media Links field group
acf_add_local_field_group(array(
'key' => 'group_social_media_links',
'title' => 'Social Media Links',
'fields' => array(
array(
'key' => 'field_social_media_instructions',
'label' => 'Instructions',
'name' => 'social_media_instructions',
'type' => 'message',
'message' => '<div style="background: #f0f6fc; border-left: 4px solid #0073aa; padding: 12px; margin-bottom: 20px;"><strong>Note:</strong> Leaving these fields blank will result in the default hardcoded values being used. Adding a link here will override the default values.</div>',
'new_lines' => 'wpautop',
'esc_html' => 0,
),
array(
'key' => 'field_facebook_url',
'label' => 'Facebook URL',
'name' => 'facebook_url',
'type' => 'url',
'instructions' => 'Enter the Facebook page URL',
'required' => 0,
'default_value' => 'https://www.facebook.com/dolfansnyc',
'placeholder' => 'https://www.facebook.com/dolfansnyc',
),
array(
'key' => 'field_twitter_url',
'label' => 'Twitter URL',
'name' => 'twitter_url',
'type' => 'url',
'instructions' => 'Enter the Twitter profile URL',
'required' => 0,
'default_value' => 'https://x.com/dolfansnyc',
'placeholder' => 'https://x.com/dolfansnyc',
),
array(
'key' => 'field_instagram_url',
'label' => 'Instagram URL',
'name' => 'instagram_url',
'type' => 'url',
'instructions' => 'Enter the Instagram profile URL',
'required' => 0,
'default_value' => 'https://www.instagram.com/dolfansnyc/',
'placeholder' => 'https://www.instagram.com/dolfansnyc/',
),
array(
'key' => 'field_youtube_url',
'label' => 'YouTube URL',
'name' => 'youtube_url',
'type' => 'url',
'instructions' => 'Enter the YouTube channel URL',
'required' => 0,
'default_value' => 'https://www.youtube.com/@dolfansnyc',
'placeholder' => 'https://www.youtube.com/@dolfansnyc',
),
),
'location' => array(
array(
array(
'param' => 'page',
'operator' => '==',
'value' => '2',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
));
// Weekly Dolphins Watch Parties field group
acf_add_local_field_group(array(
'key' => 'group_watch_party_details',
'title' => 'Weekly Dolphins Watch Parties',
'fields' => array(
array(
'key' => 'field_watch_party_instructions',
'label' => 'Instructions',
'name' => 'watch_party_instructions',
'type' => 'message',
'message' => '<div style="background: #f0f6fc; border-left: 4px solid #0073aa; padding: 12px; margin-bottom: 20px;"><strong>Note:</strong> These fields allow you to override the default watch party details shown in the sidebar. Leave blank to use the default values.</div>',
'new_lines' => 'wpautop',
'esc_html' => 0,
),
array(
'key' => 'field_venue_name',
'label' => 'Venue Name',
'name' => 'venue_name',
'type' => 'text',
'instructions' => 'Enter the venue name (e.g., "Slattery\'s Midtown Pub")',
'required' => 0,
'default_value' => 'Slattery\'s Midtown Pub',
'placeholder' => 'Slattery\'s Midtown Pub',
),
array(
'key' => 'field_venue_address',
'label' => 'Venue Address',
'name' => 'venue_address',
'type' => 'text',
'instructions' => 'Enter the full venue address',
'required' => 0,
'default_value' => '8 E 36th St., New York, NY 10016',
'placeholder' => '8 E 36th St., New York, NY 10016',
),
array(
'key' => 'field_venue_phone',
'label' => 'Venue Phone Number',
'name' => 'venue_phone',
'type' => 'text',
'instructions' => 'Enter the venue phone number',
'required' => 0,
'default_value' => '(212) 683-6444',
'placeholder' => '(212) 683-6444',
),
array(
'key' => 'field_venue_map_link',
'label' => 'Google Maps Link',
'name' => 'venue_map_link',
'type' => 'url',
'instructions' => 'Enter the Google Maps link for the venue',
'required' => 0,
'default_value' => 'https://maps.app.goo.gl/komgmLFZJGSSp4s88',
'placeholder' => 'https://maps.app.goo.gl/komgmLFZJGSSp4s88',
),
),
'location' => array(
array(
array(
'param' => 'post',
'operator' => '==',
'value' => '2',
),
),
),
'menu_order' => 1,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
));
}
}
add_action('acf/init', 'dolfansnyc_register_acf_fields');
// Add this temporarily to test if ACF is active
function dolfansnyc_debug_acf() {
if (function_exists('acf_add_local_field_group')) {
error_log('ACF is active and working');
} else {
error_log('ACF is not active');
}
}
add_action('init', 'dolfansnyc_debug_acf');