Determine if the site or post editor screen is currently used with PHP

Sometimes we may need to determine if the current block editor is used to edit the full site or a particular single post/page. While these two are similar, they have mechanisms that are different and you may need to detect which one is currently in use. For example, the editor has different data stores on each of these screens.

The following function will return site if it’s a full-site editing or post if it’s a single post/page editing.

function zwp_get_editor_screen()
{
    $filename = isset($_SERVER["SCRIPT_FILENAME"])
        ? sanitize_file_name(
            basename(
                wp_unslash(
                    $_SERVER["SCRIPT_FILENAME"]
                ),
                '.php'
            )
        )
        : null;

    if ('post' === $filename || 'post-new' === $filename) {
        return 'post';
    }

    return 'site';
}

Member since January 2, 2019

Fullstack Web Developer with more than 12 years of experience in web development. Adept in all stages of advanced web development. Knowledgeable in the user interface, backend, testing, and debugging processes. Bringing forth expertise in design, installation, testing, and maintenance of web systems. Working exclusively and professionally with WordPress since 2010.

Comments

    Your email address will not be published.