Scripts Filters

Excluding Timelines Programmatically

To programmatically exclude timelines on a page-by-page basis or based on other conditions, we use the motionpage/scripts/bypass filter. This approach is a straightforward modification for us, given that most sites already implement functions.php or plugins for code snippets. Since version 2.1.6.

/**
 * @param number[] $value Default empty array.
 * @param int $post_id Post/Page ID.
 * @return number[] String Array of timeline IDs to bypass
 */
add_filter(
    'motionpage/scripts/bypass',
    function ($value, $post_id) {
        // Prevent timelines 12 and 82 from loading on post/page 123
        if ($post_id === 123) {
            return [12, 82];
        }

        // Prevent timelines 1, 5, and 8 from loading on post/page 456
        if ($post_id === 456) {
            return [1, 5, 8];
        }

        // Prevent timeline 3 from loading on 404 and search pages
        if (is_404() || is_search()) {
            return [3];
        }

        $current_uri = $_SERVER['REQUEST_URI'];
        $path = trim(parse_url($current_uri, PHP_URL_PATH), '/');

        // Check if the path is exactly 'blog' and bypass timeline 6
        if ($path === 'blog') {
            return [6];
        }

        // Check if the path is exactly 'blog/welcome-to-our-blog' and bypass timelines 7, 8, and 9
        if ($path === 'blog/welcome-to-our-blog') {
            return [7, 8, 9];
        }

        // Return empty array to bypass no timelines
        return $value; // OR return [];
    },
    10,
    2
);
100 %