Home › Forum › Plugin e temi di WP › Far scegliere all’utente il table prefix
-
AutorePost
-
-
29 Ottobre 2010 alle 16:30 #15438giangel84Partecipante
Salve a tutti Sto realizzando un plugin e sto cercando di capire come posso risolvere questo quesito:
Affinchè tale plugin sia compatibile con la funzione multiblog/multisito, è necessario che il table_prefix sia sempre quello del dominio principale.
C’è un modo per mettere all’interno di una variabile esclusivamente il table prefix del dominio principale?
volendo usare questo:
$db_prefix = $GLOBALS;
ottengo sempre il table prefix del dominio che si sta amministrando.
Pertanto se si tenta l’installazione da un sotto-blog, il plugin non funzionerebbe correttamente.
In alternativa vorrei fare in modo che in fase di installazione l’utente possa scegliere il proprio table prefix, prima di proseguire con la creazione delle tabelle necessarie al funzionamento del plugin.
Consigli sulla procedura corretta da adottare?
Grazie a tutti!
-
29 Ottobre 2010 alle 18:19 #78111wollyAmministratore del forum
function wll_speech_install () {
global $wpdb;
global $wll_speech_db_version;
$table_name = $wpdb->prefix . “wollyspeech”;
if($wpdb->get_var(“show tables like ‘$table_name'”) != $table_name) {
$sql = “CREATE TABLE ” . $table_name . ” (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time bigint(11) DEFAULT ‘0’ NOT NULL,
nome tinytext NOT NULL,
cognome tinytext NOT NULL,
nickname tinytext NOT NULL,
email tinytext NOT NULL,
telefono tinytext NOT NULL,
url tinytext NOT NULL,
argomento tinytext NOT NULL,
speech text NOT NULL,
quando tinytext NOT NULL,
approvato tinytext NOT NULL,
UNIQUE KEY id (id));”;
require_once(ABSPATH . ‘wp-admin/includes/upgrade.php’);
dbDelta($sql);
add_option(“wll_speech_db_version”, $wll_speech_version);
}
-
29 Ottobre 2010 alle 21:07 #78113giangel84Partecipante
Non per fare il guasta feste, anzi, apprezzo l’esempio di aiuto ma non c’è nessuna differenza tra:
$table_name = $wpdb->prefix
e quello che ho detto io
$db_prefix = $GLOBALS;
poichè se tento di eseguire il plugin dal "blog1" il prefix avrà come valore "wp_" e andrebbe bene
ma se tento di farlo dal sotto-blog "blog2" il prefix avrà come valore "wp_2_" e non andrebbe bene poichè il plugin lavora con la tabella utenti "user"
Se impostassi sempre "wp_user" al posto di
$table_prefix . "user"
non avrei problemi di funzionamento, ma chi per sicurezza, cambia il prefix???
Io in parte ho risolto il problema, ho pensato di fare cosà¬:
if ($table_prefix <> 'wp_') {
$new_prefix="";
for ($i=0;$i<strlen($table_prefix);$i++){
if (!is_numeric($table_prefix{$i}))
$new_prefix=$new_prefix.$table_prefix{$i};
}
$new_prefix = substr($new_prefix, 0, -1);
$table_prefix = $new_prefix;
//validate prefix
$table_users = $table_prefix . "users";
$admin_exist = $wpdb->get_var("SELECT ID FROM $table_users WHERE user_login='admin'");
if ($admin_exist) {
$install = true;
} else {
$install = false;
}
Funziona, ma non so se è la maniera corretta.
Infatti darebbe come risultato false se i prefix fossero (ad esempio) "blog1_" "blog2_" ecc...
Allo stesso modo non funzionerebbe se il prefix fosse "blog_1_" e "blog_2_"
Ma funzionerebbe solo se fosse "blog_" e "blog_2_" proprio come fa wordpress normalmente con il prefix standard e i sottoblog.
Infatti la funzione non fa altro che controllare se la stringa table_prefix contiene un numero.
Se ne ha uno lo toglie ed elimina l'ulitmo carattere, ovvero l'underscore...
L'ideale sarebbe poter inserire la suddetta funzione, per l'installazione automatica.
Se la funzione genera false, allora lasciare all'utente la possibilità di inserire autonomamente il prefix...
Solo che a questo punto tale controllo non andrebbe fatto in fase di installazione del plugin, o in fase di attivazione....ma quando si apre per la prima volta la parte amministrativa...
Giusto?
Credevo ci fossero modi più semplici, del tipo "prendi solo il prefix del blog principale o della tabella utenti".
Mi sembra uno spreco di codice scritto cosà¬.
-
30 Ottobre 2010 alle 10:48 #78115wollyAmministratore del forum
Non avevo capito che il tuo problema fosse quello di utilizzare al tabella wp_users in modalità network e infatti ti ho postato il codice standard per creare tabelle tramite plugin.
Adesso vedo come fare a recuperare il table_prefix base.
-
30 Ottobre 2010 alle 12:56 #78117giangel84Partecipante
Grazie Wolly e scusami se sono stato poco chiaro fin dall’inizio, non specificando che dovevo lavorare con la tabella users.
Mi sarebbe di grande aiuto riuscire a recuperare il table_prefix base!
Sono sicuro che un metodo c’è ma nel codex non l’ho trovato.
Il problema è che il table prefix può essere scelto dall’utente in fase di installazione e quindi, potrebbe essere una stringa qualsiasi, pertanto il metodo di confronto da me adottato avrebbe poco senso.
Sono sicuro che il 95% dei blog installati abbiano tutti il prefix standard “wp_” ma vorrei scrivere un codice compatibile con il 100% dei blog wordpress.
Grazie ancora per l’aiuto!
-
30 Ottobre 2010 alle 13:23 #78118giangel84Partecipante
Ho trovato la soluzione!!!
La posto per tutti (cosଠWolly può confermare se è corretto il metodo):
Per recuperare il table prefix base, basta fare cosà¬.
global $wpdb;
$user_table = $wpdb->users;
$new_prefix = substr($user_table, 0, -5);
$table_prefix = $new_prefix;
In questo modo
$user_table conterrà la stringa con il nome completo di prefisso della tabella users.
ad esempio se il prefix base fosse "myblog12_3_" oppure semplicemente "wp_"
$user_table diventa "myblog_12_3_users" oppure semplicemente "wp_users"
Infatti lo scopo è quello di ottenere sempre il prefix di base, indipendentemente dal nome.
a questo punto per ottenere il prefix base è sufficiente eliminare "users" dalla stringa, che sono gli ultimi 5 caratteri.
Lo si fà con la funzione:
$new_prefix = substr($user_table, 0, -5);
a questo punto $new_prefix sarà sempre il prefisso di base.
Ora non credo che qualcuno si metta a modificare anche il nome della tabella users, per cui tale funzione sarà compatibile con il 99,9% dei blog wordpress.
Sono sicuro che ci siano altri metodi ma con questo ho eliminato tanto tanto codice inutile.
(Come ho trovato la soluzione? Analizzando il file /wp-admin/install.php)
-
30 Ottobre 2010 alle 13:29 #78119giangel84Partecipante
Ah ovviamente tale codice serve solo se si deve conoscere il prefisso di base.
Ad esempio se il mio plugin dovesse creare una tabella nel blog principale e lavorare su quella anche con i sotto-blog.
Se dovete scrivere un plugin che lavora solo sulla tabella users, invece di usare la funzione qui sopra e scrivere
$table_users = $table_prefix . “users”, la si può ricavare richiamando la global $wpdb;
e utilizzando
$table_users = $wpdb->users;
a buon rendere.
-
30 Ottobre 2010 alle 14:00 #78121wollyAmministratore del forum
trovato: $wpdb->base_prefix
-
31 Ottobre 2010 alle 12:40 #78133giangel84Partecipante
Grazie mille!! questo è ancora meglio!!
-
31 Ottobre 2010 alle 13:27 #78135wollyAmministratore del forum
direi di si:) una riga contro millemila
-
-
AutorePost
- Devi essere connesso per rispondere a questo topic.