Cool plugin, random line
-
<?php
/*
Plugin Name: Random Line
Description: Выводит случайный блок "Форматированный" из указанного поста. Блок появляется в случайном месте при прокрутке страницы и исчезает через 5 секунд.
Version: 1.0
Author: Onik Artushyan
*/
add_action('admin_menu', function() {
? ? add_menu_page('Random Line Settings', 'Random Line', 'manage_options', 'random-line-settings', function() {
? ? ? ? if (!current_user_can('manage_options')) return;
? ? ? ? if (isset($_POST['random_line_post_id'])) update_option('random_line_post_id', sanitize_text_field($_POST['random_line_post_id']));
? ? ? ? echo '<div class="wrap"><h1>Random Line Settings</h1>
? ? ? ? ? ? ? <form method="post"><label for="random_line_post_id">ID поста:</label>
? ? ? ? ? ? ? <input type="number" id="random_line_post_id" name="random_line_post_id" value="' . esc_attr(get_option('random_line_post_id', 1)) . '">
? ? ? ? ? ? ? ' . submit_button() . '</form></div>';
? ? }, 'dashicons-text', 100);
});
add_shortcode('random_line', function() {
? ? $post = get_post(get_option('random_line_post_id', 1));
? ? if ($post && preg_match_all('/<pre.*?>(.*?)<\/pre>/s', $post->post_content, $matches)) {
? ? ? ? $blocks = array_filter(array_map('trim', $matches[1]));
? ? ? ? if (!empty($blocks)) {
? ? ? ? ? ? $random_block = strip_tags($blocks[array_rand($blocks)]);
? ? ? ? ? ? return '<div class="random-line">' . esc_html($random_block) . '</div>';
? ? ? ? }
? ? }
? ? return '';
});
add_action('wp_head', function() {
? ? echo '<style>
? ? ? ? .random-line {
? ? ? ? ? ? position: fixed;
? ? ? ? ? ? background: #e3f2fd;
? ? ? ? ? ? padding: 10px;
? ? ? ? ? ? border-radius: 5px;
? ? ? ? ? ? box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
? ? ? ? ? ? z-index: 9999;
? ? ? ? ? ? animation: fadeInOut 5s ease-in-out;
? ? ? ? ? ? display: none;
? ? ? ? }
? ? ? ? @keyframes fadeInOut {
? ? ? ? ? ? 0%, 100% { opacity: 0; }
? ? ? ? ? ? 20%, 80% { opacity: 1; }
? ? ? ? }
? ? </style>';
});
add_action('wp_footer', function() {
? ? echo '<script>
? ? ? ? document.addEventListener("DOMContentLoaded", function() {
? ? ? ? ? ? setTimeout(function() {
? ? ? ? ? ? ? ? var line = document.querySelector(".random-line");
? ? ? ? ? ? ? ? if (line) {
? ? ? ? ? ? ? ? ? ? line.style.top = Math.random() * 80 + 10 + "%";
? ? ? ? ? ? ? ? ? ? line.style.left = Math.random() * 80 + 10 + "%";
? ? ? ? ? ? ? ? ? ? line.style.display = "block";
? ? ? ? ? ? ? ? ? ? setTimeout(function() { line.style.display = "none"; }, 5000);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }, 2000);
? ? ? ? });
? ? </script>';
});
?>
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- You must be logged in to reply to this review.