-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
712 lines (622 loc) · 25.7 KB
/
functions.php
File metadata and controls
712 lines (622 loc) · 25.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
<?php
/**
* Modul8 Theme Functions
*
* @package Modul8
* @since 1.0.0
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// Theme constants
define('MODUL8_THEME_VERSION', '1.0.1');
define('MODUL8_THEME_URI', get_template_directory_uri());
define('MODUL8_THEME_PATH', get_template_directory());
// Theme setup
function modul8_theme_setup() {
// Add theme support
add_theme_support('post-thumbnails');
add_theme_support('title-tag');
add_theme_support('custom-logo');
add_theme_support('responsive-embeds');
add_theme_support('html5', array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
));
// Register navigation menus
register_nav_menus(array(
'primary' => esc_html__('Primary Menu', 'modul8'),
'footer' => esc_html__('Footer Menu', 'modul8'),
'mobile' => esc_html__('Mobile Menu', 'modul8'),
));
// Add custom image sizes
add_image_size('modul8-hero', 1920, 1080, true);
add_image_size('modul8-service', 600, 400, true);
add_image_size('modul8-case-study', 800, 600, true);
// Mobile-optimized image sizes
add_image_size('modul8-mobile-hero', 768, 432, true);
add_image_size('modul8-mobile-service', 300, 200, true);
add_image_size('modul8-mobile-case-study', 400, 300, true);
}
add_action('after_setup_theme', 'modul8_theme_setup');
// Enqueue scripts and styles
function modul8_theme_scripts() {
// Enqueue styles
wp_enqueue_style('modul8-style', get_stylesheet_uri(), array(), MODUL8_THEME_VERSION);
wp_enqueue_style('modul8-custom', MODUL8_THEME_URI . '/assets/css/custom.css', array(), MODUL8_THEME_VERSION);
wp_enqueue_style('modul8-widgets', MODUL8_THEME_URI . '/assets/css/widgets.css', array(), MODUL8_THEME_VERSION);
// Enqueue FontAwesome with fallback
wp_enqueue_style('font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css', array(), '6.5.1');
wp_enqueue_style('font-awesome-fallback', 'https://use.fontawesome.com/releases/v6.5.1/css/all.css', array(), '6.5.1');
// Enqueue scripts
wp_enqueue_script('modul8-main', MODUL8_THEME_URI . '/assets/js/main.js', array('jquery'), MODUL8_THEME_VERSION, true);
// Localize script for AJAX
wp_localize_script('modul8-main', 'modul8_ajax', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('modul8_nonce')
));
}
add_action('wp_enqueue_scripts', 'modul8_theme_scripts');
// Enable SVG uploads and support
function modul8_enable_svg_upload($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter('upload_mimes', 'modul8_enable_svg_upload');
// Fix SVG display in media library
function modul8_fix_svg_display() {
echo '<style>
.attachment-266x266, .thumbnail img {
width: 100% !important;
height: auto !important;
}
.media-icon img[src$=".svg"] {
width: 100% !important;
height: auto !important;
}
</style>';
}
add_action('admin_head', 'modul8_fix_svg_display');
// Sanitize SVG uploads
function modul8_sanitize_svg($file, $filename, $mimes) {
$filetype = wp_check_filetype($filename, $mimes);
if ($filetype['ext'] === 'svg') {
$file['type'] = 'image/svg+xml';
}
return $file;
}
add_filter('wp_check_filetype_and_ext', 'modul8_sanitize_svg', 10, 3);
// Enable SVG preview in media library
function modul8_svg_media_thumbnails($response, $attachment, $meta) {
if ($response['type'] === 'image' && $response['subtype'] === 'svg+xml' && class_exists('SimpleXMLElement')) {
$path = get_attached_file($attachment->ID);
if (@file_exists($path)) {
$svg = @file_get_contents($path);
if ($svg !== false) {
$response['image'] = array(
'src' => $response['url'],
);
$response['thumb'] = array(
'src' => $response['url'],
);
// Set full size
$response['sizes'] = array(
'full' => array(
'url' => $response['url'],
'width' => 150,
'height' => 150,
'orientation' => 'landscape'
)
);
}
}
}
return $response;
}
add_filter('wp_prepare_attachment_for_js', 'modul8_svg_media_thumbnails', 10, 3);
// Elementor support
function modul8_elementor_support() {
// Check if Elementor is installed and active
if (!did_action('elementor/loaded')) {
return;
}
// Add Elementor support
add_theme_support('elementor');
// Add support for Elementor Pro features if Pro is active
if (defined('ELEMENTOR_PRO_VERSION')) {
add_theme_support('elementor-pro');
}
// Disable Elementor default fonts
add_filter('elementor/frontend/print_google_fonts', '__return_false');
// Enable SVG support in Elementor
add_filter('elementor/utils/get_the_archive_titles', '__return_false');
}
add_action('after_setup_theme', 'modul8_elementor_support');
// Allow SVG in Elementor media uploads
function modul8_elementor_svg_support() {
// Enable SVG for Elementor media control
add_filter('elementor/core/editor/editor_templates/editor-wrapper', function($template) {
return str_replace('accept="image/*"', 'accept="image/*,.svg"', $template);
});
}
add_action('elementor/init', 'modul8_elementor_svg_support');
// Register Elementor locations (only if Elementor is active)
function modul8_register_elementor_locations($elementor_theme_manager) {
if (!class_exists('\Elementor\Core\Kits\Documents\Kit')) {
return;
}
$elementor_theme_manager->register_location('header');
$elementor_theme_manager->register_location('footer');
$elementor_theme_manager->register_location('single');
$elementor_theme_manager->register_location('archive');
}
add_action('elementor/theme/register_locations', 'modul8_register_elementor_locations');
// Custom post types - currently empty but can be extended as needed
function modul8_custom_post_types() {
// Custom post types can be added here when needed
}
add_action('init', 'modul8_custom_post_types');
// Flush rewrite rules after removing custom post types (temporary - remove after first load)
function modul8_flush_rewrite_rules_once() {
if (get_option('modul8_flush_rewrite_rules') !== 'flushed_v2') {
flush_rewrite_rules(true); // Hard flush
update_option('modul8_flush_rewrite_rules', 'flushed_v2');
}
}
add_action('init', 'modul8_flush_rewrite_rules_once', 999);
// Add custom fields support
function modul8_acf_fields() {
if (function_exists('acf_add_local_field_group')) {
// Custom field groups can be added here when needed
}
}
add_action('acf/init', 'modul8_acf_fields');
// Theme customizer
function modul8_customize_register($wp_customize) {
// Site identity section
$wp_customize->add_section('modul8_identity', array(
'title' => 'Modul8 Settings',
'priority' => 30,
));
// CTA Button Text
$wp_customize->add_setting('modul8_cta_text', array(
'default' => 'Get In Touch',
'sanitize_callback' => 'sanitize_text_field',
));
$wp_customize->add_control('modul8_cta_text', array(
'label' => 'CTA Button Text',
'section' => 'modul8_identity',
'type' => 'text',
));
// Contact Email
$wp_customize->add_setting('modul8_contact_email', array(
'default' => 'hello@modul8.dev',
'sanitize_callback' => 'sanitize_email',
));
$wp_customize->add_control('modul8_contact_email', array(
'label' => 'Contact Email',
'section' => 'modul8_identity',
'type' => 'email',
));
// Logo Width
$wp_customize->add_setting('modul8_logo_width', array(
'default' => '120',
'sanitize_callback' => 'absint',
));
$wp_customize->add_control('modul8_logo_width', array(
'label' => 'Logo Width (px)',
'section' => 'modul8_identity',
'type' => 'number',
'input_attrs' => array(
'min' => 50,
'max' => 500,
'step' => 1,
),
));
// Logo Height
$wp_customize->add_setting('modul8_logo_height', array(
'default' => '40',
'sanitize_callback' => 'absint',
));
$wp_customize->add_control('modul8_logo_height', array(
'label' => 'Logo Height (px)',
'section' => 'modul8_identity',
'type' => 'number',
'input_attrs' => array(
'min' => 20,
'max' => 200,
'step' => 1,
),
));
}
add_action('customize_register', 'modul8_customize_register');
// Widget areas
function modul8_widgets_init() {
register_sidebar(array(
'name' => 'Footer Widget Area 1',
'id' => 'footer-1',
'before_widget' => '<div class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widget-title">',
'after_title' => '</h4>',
));
register_sidebar(array(
'name' => 'Footer Widget Area 2',
'id' => 'footer-2',
'before_widget' => '<div class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widget-title">',
'after_title' => '</h4>',
));
}
add_action('widgets_init', 'modul8_widgets_init');
// Include theme files safely
function modul8_include_theme_files() {
// Include simple theme setup functions
$setup_file = get_template_directory() . '/inc/simple-setup.php';
if (file_exists($setup_file)) {
require_once $setup_file;
}
// Include Elementor support
$elementor_support_file = get_template_directory() . '/inc/elementor-support.php';
if (file_exists($elementor_support_file)) {
require_once $elementor_support_file;
}
// Include admin guide
$admin_file = get_template_directory() . '/inc/admin-guide.php';
if (file_exists($admin_file)) {
require_once $admin_file;
}
}
add_action('after_setup_theme', 'modul8_include_theme_files');
// Include Elementor extensions after Elementor is loaded
function modul8_load_elementor_extensions() {
if (did_action('elementor/loaded')) {
$elementor_file = get_template_directory() . '/elementor-templates/elementor-extensions.php';
if (file_exists($elementor_file)) {
require_once $elementor_file;
// Initialize the extensions class
if (class_exists('Modul8_Elementor_Extensions')) {
new Modul8_Elementor_Extensions();
}
}
}
}
add_action('init', 'modul8_load_elementor_extensions');
// AJAX Handlers
function modul8_contact_form_handler() {
// Check if it's an AJAX request or regular form submission
$is_ajax = defined('DOING_AJAX') && DOING_AJAX;
// Enhanced debugging - log which handler is being called
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log('=== MODUL8 MAIN CONTACT FORM HANDLER CALLED ===');
error_log('Request type: ' . ($is_ajax ? 'AJAX' : 'Regular POST'));
error_log('POST data: ' . print_r($_POST, true));
}
// Get data from POST
$nonce = isset($_POST['modul8_contact_form_nonce']) ? $_POST['modul8_contact_form_nonce'] : '';
$name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
$email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
$company = isset($_POST['company']) ? sanitize_text_field($_POST['company']) : '';
$project = isset($_POST['project']) ? sanitize_textarea_field($_POST['project']) : '';
$agree = isset($_POST['agree']) ? true : false;
// Get recipient email from form data or default
$recipient_email = isset($_POST['recipient_email']) ? sanitize_email($_POST['recipient_email']) : '';
$success_message = isset($_POST['success_message']) ? sanitize_text_field($_POST['success_message']) : '';
// Debug logging
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log('Contact form submission - Name: ' . $name . ', Email: ' . $email . ', Company: ' . $company . ', Agree: ' . ($agree ? 'yes' : 'no') . ', AJAX: ' . ($is_ajax ? 'yes' : 'no'));
error_log('Recipient: ' . $recipient_email . ', Success message: ' . $success_message);
}
// Verify nonce
if (!$nonce || !wp_verify_nonce($nonce, 'modul8_contact_form')) {
if ($is_ajax) {
wp_send_json_error('Security check failed');
} else {
wp_die('Security check failed. Please refresh the page and try again.');
}
return;
}
// Validate required fields (including the agree checkbox for Elementor forms)
if (empty($name) || empty($email) || empty($project)) {
$error_msg = 'Please fill in all required fields.';
if ($is_ajax) {
wp_send_json_error($error_msg);
} else {
wp_die($error_msg . ' <a href="javascript:history.back()">Go back</a>');
}
return;
}
// Check agreement checkbox if it's present in the form
if (isset($_POST['agree']) && !$agree) {
$error_msg = 'Please agree to the terms and conditions.';
if ($is_ajax) {
wp_send_json_error($error_msg);
} else {
wp_die($error_msg . ' <a href="javascript:history.back()">Go back</a>');
}
return;
}
// Validate email
if (!is_email($email)) {
$error_msg = 'Please enter a valid email address.';
if ($is_ajax) {
wp_send_json_error($error_msg);
} else {
wp_die($error_msg . ' <a href="javascript:history.back()">Go back</a>');
}
return;
}
// Prepare email
$to = !empty($recipient_email) ? $recipient_email : get_theme_mod('modul8_contact_email', 'hello@modul8.dev');
$subject = 'New Contact Form Submission from ' . $name;
// Create HTML email body
$body = '<h3>New Contact Form Submission</h3>';
$body .= '<p><strong>Name:</strong> ' . esc_html($name) . '</p>';
$body .= '<p><strong>Email:</strong> ' . esc_html($email) . '</p>';
$body .= '<p><strong>Company:</strong> ' . esc_html($company) . '</p>';
$body .= '<p><strong>Project Description:</strong></p>';
$body .= '<p>' . nl2br(esc_html($project)) . '</p>';
// Set headers
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: ' . get_bloginfo('name') . ' <' . (defined('SMTP_FROM_EMAIL') ? SMTP_FROM_EMAIL : 'noreply@' . $_SERVER['HTTP_HOST']) . '>',
'Reply-To: ' . $name . ' <' . $email . '>'
);
// Send email
$sent = wp_mail($to, $subject, $body, $headers);
// Enhanced email debugging
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log('Email send attempt details:');
error_log('- To: ' . $to);
error_log('- Subject: ' . $subject);
error_log('- Headers: ' . print_r($headers, true));
error_log('- Body length: ' . strlen($body));
error_log('- wp_mail result: ' . ($sent ? 'SUCCESS' : 'FAILED'));
// Check if PHPMailer has any errors
global $phpmailer;
if (is_object($phpmailer) && !empty($phpmailer->ErrorInfo)) {
error_log('- PHPMailer Error: ' . $phpmailer->ErrorInfo);
}
}
// Log email attempt
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log('Email send attempt - To: ' . $to . ', Result: ' . ($sent ? 'success' : 'failed'));
}
if ($sent) {
$success_msg = !empty($success_message) ? $success_message : 'Message sent successfully! We\'ll get back to you soon.';
if ($is_ajax) {
wp_send_json_success($success_msg);
} else {
wp_die('✅ ' . $success_msg . ' <a href="' . home_url() . '">Return to homepage</a>');
}
} else {
$error_msg = 'Failed to send message. Please try again or contact us directly.';
if ($is_ajax) {
wp_send_json_error($error_msg);
} else {
wp_die('❌ ' . $error_msg . ' <a href="javascript:history.back()">Go back</a>');
}
}
}
add_action('wp_ajax_modul8_contact_form', 'modul8_contact_form_handler');
add_action('wp_ajax_nopriv_modul8_contact_form', 'modul8_contact_form_handler');
// Handle regular form submissions (non-AJAX)
function modul8_handle_regular_contact_form() {
if (isset($_POST['action']) && $_POST['action'] === 'modul8_contact_form' && !defined('DOING_AJAX')) {
modul8_contact_form_handler();
}
}
add_action('init', 'modul8_handle_regular_contact_form');
// Debug form submission
function modul8_debug_form_submission() {
if (isset($_GET['debug_form']) && current_user_can('administrator')) {
echo '<h3>Form Debug Information</h3>';
echo '<pre>';
echo 'POST Data:' . "\n";
print_r($_POST);
echo "\n" . 'GET Data:' . "\n";
print_r($_GET);
echo "\n" . 'REQUEST_METHOD: ' . $_SERVER['REQUEST_METHOD'] . "\n";
echo 'DOING_AJAX: ' . (defined('DOING_AJAX') && DOING_AJAX ? 'Yes' : 'No') . "\n";
echo 'Current URL: ' . $_SERVER['REQUEST_URI'] . "\n";
echo '</pre>';
// If this is a form submission, process it
if (isset($_POST['action']) && $_POST['action'] === 'modul8_contact_form') {
echo '<h4>Processing form submission...</h4>';
modul8_contact_form_handler();
}
// Create a test form for manual testing
?>
<div style="background: #f0f0f0; padding: 20px; margin: 20px 0; border-radius: 5px;">
<h4>Test Form (for debugging)</h4>
<form method="post" action="">
<?php wp_nonce_field('modul8_contact_form', 'modul8_contact_form_nonce'); ?>
<input type="hidden" name="action" value="modul8_contact_form">
<p><label>Name: <input type="text" name="name" value="Test User" required></label></p>
<p><label>Email: <input type="email" name="email" value="test@example.com" required></label></p>
<p><label>Company: <input type="text" name="company" value="Test Company"></label></p>
<p><label>Project: <textarea name="project" required>This is a test message.</textarea></label></p>
<p><input type="submit" value="Test Submit"></p>
</form>
</div>
<?php
wp_die();
}
}
add_action('init', 'modul8_debug_form_submission');
// Newsletter signup handler
function modul8_newsletter_signup_handler() {
// Verify nonce
if (!wp_verify_nonce($_POST['modul8_contact_form_nonce'], 'modul8_contact_form')) {
wp_die('Security check failed');
}
$email = sanitize_email($_POST['email']);
// Here you can integrate with your email service (MailChimp, ConvertKit, etc.)
// For now, we'll just store it in the database
$subscribers = get_option('modul8_newsletter_subscribers', array());
if (!in_array($email, $subscribers)) {
$subscribers[] = $email;
update_option('modul8_newsletter_subscribers', $subscribers);
wp_send_json_success('Subscribed successfully');
} else {
wp_send_json_error('Already subscribed');
}
}
add_action('wp_ajax_modul8_newsletter_signup', 'modul8_newsletter_signup_handler');
add_action('wp_ajax_nopriv_modul8_newsletter_signup', 'modul8_newsletter_signup_handler');
function category_filter_buttons_shortcode() {
$categories = get_categories(array(
'hide_empty' => false
));
if (empty($categories)) {
return '<p>No categories found.</p>';
}
// Get current category if we're on a category page
$current_category = null;
if (is_category()) {
$current_category = get_queried_object();
}
// Debug: Check what page we're on
$debug_info = '';
if (defined('WP_DEBUG') && WP_DEBUG) {
$debug_info = '<!-- Debug: is_category=' . (is_category() ? 'true' : 'false') .
', is_home=' . (is_home() ? 'true' : 'false') .
', is_page_blog=' . (is_page('blog') ? 'true' : 'false') .
', current_cat=' . ($current_category ? $current_category->term_id : 'none') . ' -->';
}
ob_start();
?>
<?php echo $debug_info; ?>
<div class="category-filter-buttons">
<?php
// Add 'active' class to All button if we're on blog home, blog page, or single post (but not category)
$all_active = (!is_category() && (is_home() || is_page('blog') || is_single())) ? ' active' : '';
?>
<a href="/blog/" class="filter-button all-button<?php echo $all_active; ?>">All</a>
<?php foreach ($categories as $category) : ?>
<?php
// Add 'active' class if this is the current category
$cat_active = ($current_category && $current_category->term_id == $category->term_id) ? ' active' : '';
?>
<a href="<?php echo esc_url(get_category_link($category->term_id)); ?>" class="filter-button<?php echo $cat_active; ?>">
<?php echo esc_html($category->name); ?>
</a>
<?php endforeach; ?>
</div>
<?php
return ob_get_clean();
}
add_shortcode('category_filter_buttons', 'category_filter_buttons_shortcode');
// Include additional theme files
require_once get_template_directory() . '/inc/page-creator.php';
// Mobile detection utility function
function modul8_is_mobile() {
return wp_is_mobile();
}
// Get responsive image sizes based on device
function modul8_get_responsive_image_size($base_size = 'medium') {
if (modul8_is_mobile()) {
switch ($base_size) {
case 'modul8-hero':
return 'modul8-mobile-hero';
case 'modul8-service':
return 'modul8-mobile-service';
case 'modul8-case-study':
return 'modul8-mobile-case-study';
default:
return 'medium';
}
}
return $base_size;
}
// Add mobile-specific body classes
function modul8_mobile_body_classes($classes) {
if (modul8_is_mobile()) {
$classes[] = 'is-mobile';
}
return $classes;
}
add_filter('body_class', 'modul8_mobile_body_classes');
/**
* SMTP Configuration for Email Delivery
* Configure your SMTP settings below
*/
// SMTP Configuration Function
function modul8_configure_smtp($phpmailer) {
$phpmailer->isSMTP();
// Gmail SMTP Settings
$phpmailer->Host = 'smtp.gmail.com'; // Gmail SMTP server
$phpmailer->SMTPAuth = true;
$phpmailer->Port = 587; // SMTP port for TLS
$phpmailer->SMTPSecure = 'tls'; // Encryption type
// SMTP Credentials - Read from wp-config.php constants for security
$phpmailer->Username = defined('SMTP_USERNAME') ? SMTP_USERNAME : 'zdravko.modul8@gmail.com';
$phpmailer->Password = defined('SMTP_PASSWORD') ? SMTP_PASSWORD : 'your-app-password';
// Sender settings
$phpmailer->From = defined('SMTP_FROM_EMAIL') ? SMTP_FROM_EMAIL : 'zdravko.modul8@gmail.com';
$phpmailer->FromName = defined('SMTP_FROM_NAME') ? SMTP_FROM_NAME : 'Modul8 Contact Form';
// Additional settings for Gmail
$phpmailer->SMTPDebug = 0; // Set to 2 for debugging, 0 for production
$phpmailer->Timeout = 30;
$phpmailer->SMTPKeepAlive = true;
$phpmailer->CharSet = 'UTF-8';
// Gmail specific settings
$phpmailer->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
}
// Apply SMTP configuration to WordPress mailer
add_action('phpmailer_init', 'modul8_configure_smtp');
/**
* Test Email Function - Add ?test_email=1 to any page URL to test
*/
function modul8_test_email() {
if (isset($_GET['test_email']) && $_GET['test_email'] == '1' && current_user_can('administrator')) {
$to = 'info@modul8.dev';
$subject = 'SMTP Test Email from Modul8';
$message = 'This is a test email to verify SMTP configuration is working correctly.';
$headers = array('Content-Type: text/html; charset=UTF-8');
$sent = wp_mail($to, $subject, $message, $headers);
if ($sent) {
wp_die('✅ Test email sent successfully! Check your inbox at ' . $to);
} else {
wp_die('❌ Test email failed to send. Check your SMTP configuration.');
}
}
}
add_action('init', 'modul8_test_email');
/**
* Enhanced error logging for email issues
*/
function modul8_log_email_errors($wp_error) {
if (WP_DEBUG && WP_DEBUG_LOG) {
error_log('WordPress Mail Error: ' . $wp_error->get_error_message());
}
}
add_action('wp_mail_failed', 'modul8_log_email_errors');
// Force enable Application Passwords (for debugging)
add_filter('wp_is_application_passwords_available', '__return_true');
add_filter('wp_is_application_passwords_available_for_user', '__return_true');
// Debug Application Passwords availability
function modul8_debug_app_passwords() {
if (isset($_GET['debug_app_passwords']) && current_user_can('administrator')) {
echo '<pre>';
echo 'WordPress Version: ' . get_bloginfo('version') . "\n";
echo 'HTTPS: ' . (is_ssl() ? 'Yes' : 'No') . "\n";
echo 'Application Passwords Available: ' . (wp_is_application_passwords_available() ? 'Yes' : 'No') . "\n";
echo 'Application Passwords Available for User: ' . (wp_is_application_passwords_available_for_user(get_current_user_id()) ? 'Yes' : 'No') . "\n";
echo 'WP_APPLICATION_PASSWORDS constant: ' . (defined('WP_APPLICATION_PASSWORDS') ? (WP_APPLICATION_PASSWORDS ? 'true' : 'false') : 'undefined') . "\n";
echo '</pre>';
wp_die();
}
}
add_action('init', 'modul8_debug_app_passwords');