Modificare i permessi dei file e il proprietario di un sito di staging

Alcune configurazioni di Hosting usano un’impostazione insolita del proprietario e dei permessi di un sito WordPress.

Ad esempio, la tua configurazione di Hosting potrebbe usare PHP-FPM che gira come account utente, quindi quando PHP accede al file system crea i file con il proprio nome. WordPress, d’altra parte, potrebbe richiedere permessi diversi che portano alle immagini caricate nella gallery con il nome del web server. Quindi, dopo aver creato un sito di staging, puoi ricevere errori di permessi.

WP Staging crea un sito di staging con i permessi WordPress predefiniti di 755 per i file e 644 per le cartelle. Il proprietario è l’utente del processo PHP che ha creato il sito di staging.

Per risolvere puoi modificare il proprietario e i permessi di un sito di staging dopo averlo creato usando questa funzione e questo filtro:

PHP
<?php

function changePermissionsAndOwner() {

$directory = '/path/to/your/directory';
$permissions = 0755;
$owner = 'username';
$group = 'groupname';

    // Check if the path is a directory
    if (is_dir($path)) {
        // Create a RecursiveDirectoryIterator object
        $directoryIterator = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
        // Create a RecursiveIteratorIterator object
        $iterator = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::SELF_FIRST);

        // Iterate through each file and directory in the tree
        foreach ($iterator as $item) {
            // Change the permissions of the current item
            chmod($item, $permissions);
            // Change the owner and group of the current item
            chown($item, $owner);
            chgrp($item, $group);
        }

        // Change the permissions of the root directory
        chmod($path, $permissions);
        // Change the owner and group of the root directory
        chown($path, $owner);
        chgrp($path, $group);
    } else {
        echo "The path is not a directory.";
    }
}

add_action('wpstg.clone_first_run', 'changePermissionsAndOwner', 10);

Questo verrà eseguito al primo login al sito di staging e cambierà tutti i file con i permessi corrispondenti.

Updated on Maggio 23, 2026

Rene Hermenau

Autore: Rene Hermenau

About the author: René Hermenau is the founder of WP STAGING. He works on WordPress backups, staging, migrations, database handling, and safe deployment workflows.