-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype-text.php
More file actions
68 lines (61 loc) · 2 KB
/
Copy pathtype-text.php
File metadata and controls
68 lines (61 loc) · 2 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
<?php
/*
Plugin Name: Type Text
Description: This plugin allows you to display text like it's being typed on a typewriter.
Version: 1.0
Author: Haley Stelly
Author URI: http://stly.dev/
License: GPL-3.0-or-later
License URI: https://www.gnu.org/licenses/gpl-3.0.html
*/
// Register shortcode
function type_text_shortcode($atts, $content = null) {
// Extract shortcode attributes
$atts = shortcode_atts(
array(
'text' => '', // Default text
),
$atts,
'type_text'
);
// Sanitize and get the text
$text = sanitize_text_field($atts['text']);
// Generate a unique ID for this instance
$unique_id = uniqid('typing_');
// Output the HTML with the text and unique ID
ob_start();
?>
<span id="<?php echo esc_attr($unique_id); ?>"></span>
<script>
document.addEventListener('DOMContentLoaded', function() {
var text = "<?php echo esc_js($text); ?>";
var delay = 100; // Delay between typing each character in milliseconds
var element = document.getElementById("<?php echo esc_js($unique_id); ?>");
var index = 0;
// Function to type the text
function type() {
if (index < text.length) {
element.innerHTML += text.charAt(index);
index++;
setTimeout(type, delay);
}
}
// Initialize Intersection Observer
var observer = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry) {
// If the section comes into view, start typing
if (entry.isIntersecting) {
type();
observer.unobserve(entry.target);
}
});
}, { threshold: 0.5 }); // 50% of the element must be in view
// Start observing the target element
observer.observe(element);
});
</script>
<?php
return ob_get_clean();
}
add_shortcode('type_text', 'type_text_shortcode');
?>