-
Notifications
You must be signed in to change notification settings - Fork 0
/
enable-shortcodes.php
41 lines (37 loc) · 1.43 KB
/
enable-shortcodes.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
<?php
//enable [sitemap] shortcode
function sitemap_shortcode_handler( $atts ){
return "\n<div id='charta-vitae'></div>\n"; # put the map here
}
add_shortcode( 'sitemap', 'sitemap_shortcode_handler' );
# conditionally add javascript to header when shortcode found on page
# thanks to: http://beerpla.net/2010/01/13/wordpress-plugin-development-how-to-include-css-and-javascript-conditionally-and-only-when-needed-by-the-posts/
add_filter('the_posts', 'conditionally_add_scripts_and_styles'); // the_posts gets triggered before wp_head
function conditionally_add_scripts_and_styles($posts){
if (empty($posts)) return $posts;
$shortcode_found = false; // use this flag to see if styles and scripts need to be enqueued
foreach ($posts as $post) {
if (stripos($post->post_content, '[sitemap]') !== false) {
$shortcode_found = true; // bingo!
break;
}
}
if ($shortcode_found) {
// enqueue here
$dir = '/wp-content/plugins/charta-vitae';
wp_enqueue_script('CVmain',"$dir/bundle.js");
wp_enqueue_style('CVstyle',"$dir/charta.css");
}
return $posts;
}
add_filter('script_loader_tag', 'add_type_attribute' , 10, 3);
function add_type_attribute($tag, $handle, $src) {
// if not your script, do nothing and return original $tag
if ( strpos($handle,'CV') === false ) {
return $tag;
}
// change the script tag by adding type="module" and return it.
$tag = "<script type='module' src='" . esc_url( $src ) . "'></script>\n";
return $tag;
}
?>