WordPress

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

Andrei Surdu Andrei Surdu ·

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';
}

Comments

Share your thoughts and join the conversation

Loading comments...

Leave a Comment

Your email will not be published

Comments are moderated and will appear after approval

0/2000