-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquarecandy-acf-seo.php
664 lines (612 loc) · 24.4 KB
/
squarecandy-acf-seo.php
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
<?php
/*
Plugin Name: Square Candy ACF SEO Plugin
Plugin URI: https://github.com/squarecandy/squarecandy-acf-seo
GitHub Plugin URI: https://github.com/squarecandy/squarecandy-acf-seo
Description: provides basic SEO meta fields, defaults and per post overrides
Version: 1.4.3
Author: Square Candy Design
Author URI: http://squarecandydesign.com
License: GPL3
License URI: https://www.gnu.org/licenses/gpl-3.0.txt
Text Domain: squarecandy-acf-seo
*/
// don't let users activate w/o ACF
register_activation_hook( __FILE__, 'squarecandy_acf_seo_activate' );
function squarecandy_acf_seo_activate() {
if ( ! function_exists( 'acf_add_options_page' ) || ! function_exists( 'get_field' ) ) {
// check that ACF functions we need are available. Complain and bail out if they are not
wp_die(
'The Square Candy ACF SEO Plugin requires ACF
(<a href="https://www.advancedcustomfields.com">Advanced Custom Fields</a>).
<br><br><button onclick="window.history.back()">« back</button>'
);
}
}
add_action( 'init', 'squarecandy_acf_seo_init', 1 );
function squarecandy_acf_seo_init() {
// Add SEO Options Page
if ( function_exists( 'acf_add_options_page' ) ) {
acf_add_options_page(
array(
'page_title' => 'SEO & Meta Data Default Settings',
'menu_title' => 'SEO Settings',
'menu_slug' => 'squarecandy-acf-seo-settings',
'capability' => 'edit_theme_options',
'redirect' => false,
)
);
}
if ( function_exists( 'acf_add_local_field_group' ) ) :
acf_add_local_field_group(
array(
'key' => 'group_5914613407e76',
'title' => 'SEO Settings',
'fields' => array(
array(
'key' => 'field_5914613d4d0d5',
'label' => 'Default Meta Description',
'name' => 'default_meta_description',
'type' => 'textarea',
'instructions' => 'Add a default description for you site. This will appear in google results under the title and URL.',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'maxlength' => 330,
'rows' => 3,
'new_lines' => '',
),
array(
'key' => 'field_591461db4d0d6',
'label' => 'Default Social Media Image',
'name' => 'default_social_media_image',
'type' => 'image',
'instructions' => '1200 x 630',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'return_format' => 'id',
'preview_size' => 'squarecandy-acf-seo-thumb',
'library' => 'all',
'min_width' => 1200,
'min_height' => 630,
'min_size' => '',
'max_width' => '',
'max_height' => '',
'max_size' => '',
'mime_types' => '',
),
array(
'key' => 'field_591479e548b4e',
'label' => 'Twitter Handle',
'name' => 'twitter_handle',
'type' => 'text',
'instructions' => 'Be sure to visit the <a href="https://cards-dev.twitter.com/validator">twitter card validator</a> to setup validation for twitter cards for this site.',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '@exampletwitteruser',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_59147a4a48b4f',
'label' => 'Post Types',
'name' => 'seo_post_types',
'type' => 'text',
'instructions' => 'Comma separated list of Post Types to add Custom SEO field options to',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => 'post,page',
'placeholder' => 'post,page,custom_type',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_59147ataxonomies',
'label' => 'Taxonomies',
'name' => 'seo_taxonomies',
'type' => 'text',
'instructions' => 'Comma separated list of Taxonomies to add Custom SEO field options to',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => 'category,post_tag',
'placeholder' => 'category,post_tag,custom_taxonomy',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_shuef27googlean8b4f',
'label' => 'Google Analytics',
'name' => 'googleanalytics',
'type' => 'text',
'instructions' => 'Google Analitics Tracking ID',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '000000000-1',
'prepend' => 'UA-',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_favicon27438b0b502',
'label' => 'Addition Raw Header Code (for favicon settings, etc.)',
'name' => 'favicon_header',
'type' => 'textarea',
'instructions' => '',
),
),
'location' => array(
array(
array(
'param' => 'options_page',
'operator' => '==',
'value' => 'squarecandy-acf-seo-settings',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'seamless',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
)
);
endif;
// get the array of post types to add
$types = array();
if ( function_exists( 'get_field' ) && get_field( 'seo_post_types', 'options' ) ) {
$typesdata = get_field( 'seo_post_types', 'options' );
$typesdata = explode( ',', $typesdata );
} else {
$typesdata = array( 'post', 'page' );
}
foreach ( $typesdata as $item ) {
$types[] = array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => trim( $item ),
),
);
}
// get the array of taxonomies to add
if ( function_exists( 'get_field' ) && get_field( 'seo_taxonomies', 'options' ) ) {
$taxdata = get_field( 'seo_taxonomies', 'options' );
$taxdata = explode( ',', $taxdata );
} else {
$taxdata = array( 'category', 'post_tag' );
}
foreach ( $taxdata as $item ) {
$types[] = array(
array(
'param' => 'taxonomy',
'operator' => '==',
'value' => trim( $item ),
),
);
}
// Add Seo Fields to Post/Page/Custom Edit Screen
if ( function_exists( 'acf_add_local_field_group' ) && is_admin() ) :
if ( is_admin() && empty( $_GET['post'] ) ) {
$message = __( 'publish / update to view the preview', 'acf' );
} else {
$message = squarecandy_acf_seo_google_preview_html();
}
acf_add_local_field_group(
array(
'key' => 'group_59147c153ee90',
'title' => 'SEO & Social Media',
'fields' => array(
array(
'key' => 'field_59147d1e64317',
'label' => 'SEO Meta Description',
'name' => 'seo_meta_description',
'type' => 'textarea',
'instructions' => 'Add a description for this page. This will appear in google results under the title and URL and in social media sharing contexts.',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'maxlength' => 330,
'rows' => 3,
'new_lines' => '',
),
array(
'key' => 'field_59147d2464318',
'label' => 'Social Media Image',
'name' => 'seo_social_media_image',
'type' => 'image',
'instructions' => 'Upload an image to use in social sharing contexts. It\'s a great idea to include a unique image for every page!',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'return_format' => 'id',
'preview_size' => 'squarecandy-acf-seo-thumb',
'library' => 'all',
'min_width' => 1200,
'min_height' => 630,
'min_size' => '',
'max_width' => '',
'max_height' => '',
'max_size' => '',
'mime_types' => '',
),
array(
'key' => 'field_59147d3564319',
'label' => 'Twitter Handle',
'name' => 'seo_twitter_handle',
'type' => 'text',
'instructions' => 'Override the twitter:creator meta tag if appropriate.',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '@exampletwitteruser',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_59147deb0b502',
'label' => 'SEO Title Override',
'name' => 'seo_title_override',
'type' => 'text',
'instructions' => 'Leave this blank in most cases to use the default title. If you need to override the default <title> tag for this particular page, enter your custom title here.',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => 60,
),
array(
'key' => 'field_5914921d37359',
'label' => 'Social Sharing Title Override',
'name' => 'seo_social_title_override',
'type' => 'text',
'instructions' => 'Leave this blank in most cases to use the default title. If you need to override the title on social sharing for this particular page, enter your custom title here.',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => 60,
),
array(
'key' => 'field_5914654378PREVIEW',
'label' => 'Preview',
'name' => '',
'type' => 'message',
'message' => $message,
'new_lines' => '',
'esc_html' => 0,
),
array(
'key' => 'field_canonical75483997034290',
'label' => 'Canonical URL',
'name' => 'canonical_url',
'type' => 'url',
'instructions' => 'Enter the canonical URL if appropriate. This is the "primary location" of the content. <a href="https://moz.com/learn/seo/canonicalization">More infomation</a>. Leave this blank in most cases.',
),
array(
'key' => 'field_noindex748923795647483',
'label' => 'NOINDEX',
'name' => 'noindex',
'type' => 'true_false',
'message' => 'Discourage Search Engines from Indexing this Page.',
'default_value' => 0,
'instructions' => 'This is appropriate for hidden links, thank you pages, etc. that are publicly accessible but should not be found in a Search.',
),
),
'location' => $types,
'menu_order' => 200,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
)
);
endif;
}
// Remove the "generator" tag
function squarecandy_acf_seo_remove_version() {
return '';
}
add_filter( 'the_generator', 'squarecandy_acf_seo_remove_version' );
function squarecandy_acf_seo_image_sizes() {
// Add the custom image sizes needed
add_image_size( 'squarecandy-acf-seo-facebook', 1200, 627, true );
add_image_size( 'squarecandy-acf-seo-twitter', 1024, 512, true );
add_image_size( 'squarecandy-acf-seo-thumb', 527, 275, true );
// make sure WordPress is not supplying a default <title> tag
remove_theme_support( 'title-tag' );
}
add_action( 'plugins_loaded', 'squarecandy_acf_seo_image_sizes' );
function squarecandy_acf_seo_get_data() {
$obj = get_queried_object();
// get the data
$return = array();
// title
if ( function_exists( 'get_field' ) && get_field( 'seo_title_override', $obj ) ) {
$return['head_title'] = get_field( 'seo_title_override', $obj );
} else {
if ( is_admin() && isset( $obj->post_title ) ) {
$return['head_title'] = $obj->post_title . ' — ' . get_bloginfo( 'name' );
} elseif ( is_admin() && isset( $obj->name ) ) {
$return['head_title'] = $obj->name . ' — ' . get_bloginfo( 'name' );
} else {
$return['head_title'] = wp_title( '—', false, 'right' ) . get_bloginfo( 'name' );
}
}
// social title
if ( function_exists( 'get_field' ) && get_field( 'seo_social_title_override', $obj ) ) {
$return['social_title'] = get_field( 'seo_social_title_override', $obj );
} elseif ( function_exists( 'get_field' ) && get_field( 'seo_title_override', $obj ) ) {
$return['social_title'] = get_field( 'seo_title_override', $obj );
} else {
$return['social_title'] = wp_title( '—', false, 'right' ) . get_bloginfo( 'name' );
}
global $pagenow;
// description
if ( function_exists( 'get_field' ) && get_field( 'seo_meta_description', $obj ) ) {
// if post-specific description is not empty
$return['description'] = get_field( 'seo_meta_description', $obj );
} elseif ( is_singular() || 'post.php' === $pagenow ) {
// else if we can get an excerpt for the post
// note: check with is_singular not is_single to be sure to include PAGES too https://www.engagewp.com/is_single-vs-is_singular-vs-is_page/
// note: check if we are on a post edit admin page to provide an accurate preview
if ( 'post.php' === $pagenow ) {
$excerpt = get_the_excerpt( (int) $_GET['post'] );
} else {
$excerpt = get_the_excerpt();
}
// https://wordpress.stackexchange.com/a/70924/41488
$limit = 330;
$excerpt = preg_replace( ' (\[.*?\])', '', $excerpt );
$excerpt = strip_shortcodes( $excerpt );
$excerpt = wp_strip_all_tags( $excerpt );
$excerpt = substr( $excerpt, 0, $limit );
$excerpt = substr( $excerpt, 0, strripos( $excerpt, ' ' ) );
$excerpt = trim( preg_replace( '/\s+/', ' ', $excerpt ) );
$return['description'] = $excerpt;
} elseif ( function_exists( 'get_field' ) && get_field( 'default_meta_description', 'options' ) ) {
// else if default description field is not empty
$return['description'] = get_field( 'default_meta_description', 'options' );
} elseif ( get_bloginfo( 'description' ) ) {
// else if WordPress Tagline is not empty
$return['description'] = get_bloginfo( 'description' );
} else {
// else don't display any of the description meta tags
$return['description'] = false;
}
// image
if ( function_exists( 'get_field' ) && get_field( 'seo_social_media_image', $obj ) ) {
// if post-specific social media image is not empty
$image = get_field( 'seo_social_media_image', $obj );
$facebookimage = wp_get_attachment_image_src( $image, 'squarecandy-acf-seo-facebook' );
$return['facebookimage'] = $facebookimage[0];
$twitterimage = wp_get_attachment_image_src( $image, 'squarecandy-acf-seo-twitter' );
$return['twitterimage'] = $twitterimage[0];
} elseif ( get_post_thumbnail_id() ) {
// else if WordPress Featured Image exists for this post
$image = true;
$facebookimage = wp_get_attachment_image_src( get_post_thumbnail_id(), 'squarecandy-acf-seo-facebook' );
$return['facebookimage'] = $facebookimage[0];
$twitterimage = wp_get_attachment_image_src( get_post_thumbnail_id(), 'squarecandy-acf-seo-twitter' );
$return['twitterimage'] = $twitterimage[0];
} elseif ( function_exists( 'get_field' ) && get_field( 'default_social_media_image', 'options' ) ) {
// else if default social media image is not empty
$image = get_field( 'default_social_media_image', 'options' );
$facebookimage = wp_get_attachment_image_src( $image, 'squarecandy-acf-seo-facebook' );
$return['facebookimage'] = $facebookimage[0];
$twitterimage = wp_get_attachment_image_src( $image, 'squarecandy-acf-seo-twitter' );
$return['twitterimage'] = $twitterimage[0];
} else {
// else don't display any of the image meta tags
$image = false;
$return['facebookimage'] = false;
$return['twitterimage'] = false;
}
// set the twitter card type
if ( $image ) {
$return['twitter_card'] = 'summary_large_image';
} else {
$return['twitter_card'] = 'summary';
}
// twitter handles
$return['twittersite'] = false;
$return['twitterauthor'] = false;
// if default twitter handle exists
if ( function_exists( 'get_field' ) && get_field( 'twitter_handle', 'options' ) ) {
$return['twittersite'] = get_field( 'twitter_handle', 'options' );
$return['twitterauthor'] = $return['twittersite'];
}
// if post specific twitter handle exists, override the author handle
if ( function_exists( 'get_field' ) && get_field( 'seo_twitter_handle', $obj ) ) {
$return['twitterauthor'] = get_field( 'seo_twitter_handle', $obj );
if ( ! $return['twittersite'] ) {
$return['twittersite'] = get_field( 'seo_twitter_handle', $obj );
}
}
// permalink
if ( is_tax() || is_category() || is_tag() ) :
$return['url'] = get_term_link( $obj );
else :
$return['url'] = get_permalink( $obj );
endif;
// if the canonical url is set add it to the array
if ( function_exists( 'get_field' ) && get_field( 'canonical_url', $obj ) ) {
$return['canonical'] = get_field( 'canonical_url', $obj );
} else {
$return['canonical'] = false;
}
// if noindex is set add it to the array
if ( function_exists( 'get_field' ) && get_field( 'noindex', $obj ) ) {
$return['noindex'] = get_field( 'noindex', $obj );
} else {
$return['noindex'] = false;
}
// if favicon_header is set add it to the array
if ( function_exists( 'get_field' ) && get_field( 'favicon_header', 'option' ) ) {
$return['favicon_header'] = get_field( 'favicon_header', 'option' );
} else {
$return['favicon_header'] = false;
}
// return the array
return $return;
}
// remove the default canonical and shorlink from <head> - we want to use only our overrides.
remove_action( 'wp_head', 'rel_canonical' );
remove_action( 'wp_head', 'wp_shortlink_wp_head' );
function squarecandy_acf_seo_google_preview_html() {
$preview = "<style>@import url('https://fonts.googleapis.com/css?family=Roboto');.rc,.rc h3{max-width:600px;width:100%;font-weight:400}.rc,.rc cite,.rc h3,.st{font-weight:400}.rc{margin:1em;color:#222;display:block;font-family:Roboto,arial,sans-serif;font-size:13px;line-height:15.6px}.rc h3{font-size:18px;height:21px;line-height:21.6px;margin:0;overflow:hidden;padding:0;text-overflow:ellipsis;white-space:nowrap}.rc a{color:#1a0dab;text-decoration:none}.rc a:hover{color:#1a0dab;text-decoration:underline}.rc cite{color:#006621;display:inline;font-size:14px;line-height:16px;font-style:normal}.st{color:#545454;font-size:13px;line-height:18.2px;word-wrap:break-word}</style>";
$data = squarecandy_acf_seo_get_data();
$url = get_permalink( $_GET['post'] );
$url = str_replace( 'http://', '', $url );
$preview .= '<div class="rc"><h3 class="r"><a href="javascript:;">' . $data['head_title'] . '</a></h3>
<div class="s"><div class="f" style="white-space:nowrap"><cite>' . $url . '</cite></div>
<span class="st">' . $data['description'] . '</span></div>
</div>';
return $preview;
}
// add the tags to the HTML <head>
function squarecandy_acf_seo_hook_header() {
// get the data
$data = squarecandy_acf_seo_get_data();
// output the code
?>
<title><?php echo $data['head_title']; ?></title>
<?php if ( $data['description'] ) : ?>
<meta name="description" content="<?php echo esc_attr( $data['description'] ); ?>">
<meta name="twitter:description" content="<?php echo esc_attr( $data['description'] ); ?>">
<meta property="og:description" content="<?php echo esc_attr( $data['description'] ); ?>">
<?php endif; ?>
<!-- Twitter Card data -->
<meta name="twitter:title" content="<?php echo esc_attr( $data['social_title'] ); ?>">
<meta name="twitter:card" content="<?php echo $data['twitter_card']; ?>">
<?php if ( $data['twittersite'] ) : ?>
<meta name="twitter:site" content="<?php echo $data['twittersite']; ?>">
<?php endif; ?>
<?php if ( $data['twitterauthor'] ) : ?>
<meta name="twitter:creator" content="<?php echo $data['twitterauthor']; ?>">
<?php endif; ?>
<?php if ( $data['twitterimage'] ) : ?>
<meta name="twitter:image:src" content="<?php echo esc_url( $data['twitterimage'] ); ?>">
<?php endif; ?>
<!-- Open Graph data -->
<meta property="og:title" content="<?php echo esc_attr( $data['social_title'] ); ?>">
<meta property="og:type" content="article" />
<meta property="og:url" content="<?php echo esc_url( $data['url'] ); ?> ">
<meta property="og:site_name" content="<?php esc_attr( bloginfo( 'name' ) ); ?>">
<?php if ( $data['facebookimage'] ) : ?>
<meta property="og:image" content="<?php echo esc_url( $data['facebookimage'] ); ?>">
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="627">
<?php endif; ?>
<?php if ( $data['canonical'] ) : ?>
<!-- Canonical URL (points to primary source) -->
<link rel="canonical" href="<?php echo esc_url( $data['canonical'] ); ?>">
<?php endif; ?>
<?php if ( $data['noindex'] ) : ?>
<!-- Discourage Search Engines from Indexing this Page -->
<meta name="robots" content="noindex, follow">
<?php endif; ?>
<?php if ( $data['favicon_header'] ) : ?>
<!-- Code added from Square Candy SEO (favicon code field) -->
<?php echo $data['favicon_header']; ?>
<?php endif; ?>
<?php
}
add_action( 'wp_head', 'squarecandy_acf_seo_hook_header' );
// add google analtyics to the header
function squarecandy_googleanatlyics_header() {
if (
WP_DEBUG !== true &&
substr( $_SERVER['SERVER_NAME'], 0, 3 ) !== 'dev' &&
function_exists( 'get_field' ) &&
get_field( 'googleanalytics', 'options' )
) {
$googleanalytics = get_field( 'googleanalytics', 'options' );
// this doesn't go in the <head> or at the end of the document, so can't use wp_enqueue
// phpcs:disable WordPress.WP.EnqueuedResources
echo "<!-- squarecandy_acf_seo googleanalytics -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src='https://www.googletagmanager.com/gtag/js?id=UA-" . $googleanalytics . "'></script>
<script>window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}gtag('js', new Date());
gtag('config', 'UA-" . $googleanalytics . "');</script>";
} elseif (
function_exists( 'get_field' ) &&
! get_field( 'googleanalytics', 'options' )
) {
echo '<!-- OOPS - enter your google analytics UA account number on the settings page - squarecandy_acf_seo googleanalytics -->';
} else {
echo '<!-- squarecandy_acf_seo googleanalytics will go here on the live site. -->';
}
}
add_action( 'wp_head', 'squarecandy_googleanatlyics_header', 9999 );