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

As a seasoned WordPress developer with expertise in various tech stacks and languages, I bring years of experience to every project I handle. My passion for coding and dedication to delivering exceptional work ensures that each project I take on is of the highest quality. I specialize in creating custom themes, developing plugins, and building full-scale web systems. By staying up-to-date with the latest industry trends and best practices, I incorporate cutting-edge solutions into my work.

Comments

    Your email address will not be published. Required fields are marked *