Service de modèle dynamique, changez theme_root en utilisant add_filter du thème actuel
-
-
Comment détectez-vous sur quels appareils les utilisateurs accèdent à votreblog?How do you detect on which device users access your blog?
- 0
- 2011-03-24
- kaiser
-
Cela sefait à l'aide de PHP,de scripts JSet d'une chaîne d'agent utilisateur.Détection dunavigateur PHP sauvegardéepar la détection d'objets JS (car l'agent utilisateurn'estpasfiableet la détection dunavigateurn'estpas unebonnepratique).That is done with the help of PHP ,JS scripts and user agent string. PHP browser detection backed up by JS object detection(as user agent is unreliable and browser detection, not a good practice).
- 0
- 2011-03-24
- OpenOne
-
4 réponses
- votes
-
- 2011-03-25
Vouspouvez également écrire votreproprefonction d'aliasget_template_part simple:
Ce qui suit autorise 3 sous-dossierspour lesparties demodèle qui setrouvent dans un dossier racine dethèmenommé devices .<?php // STYLESHEETS function print_device_styles( $client = 'desktop' ) { $client = apply_filters( 'set_theme_client', $client ); wp_enqueue_style( $client.'-css' ); } add_action( 'wp_head', 'print_device_styles', 11 ); // TEMPLATE PARTS function get_device_template_part( $args = array( 'file' => NULL, 'suffix' => 'default', 'client' => 'desktop', 'media' => 'screen' ) { if ( ! $args['file'] ) wp_die( sprintf( __('You have to specify a file name if you want to load a template part with the %1$s function.', 'textdomain', '<pre>get_device_template_part()</pre>' ); $template_path = user_trailingslashit( get_stylesheet_directory().'/devices/templates-'.$args['client'] ); $ui_path = user_trailingslashit( get_stylesheet_directory().'/ui/css-'.$args['client'] ); $ui_suffix = '.css'; // could be switched between '.dev.css' & '.css' // add styles & template directory if ( is_condition_mobile() ) { $args['client'] = 'mobile'; $args['screen'] = 'handheld'; } elseif ( is_condition_tablet() ) { $args['client'] = 'tablet'; $args['screen'] = 'handheld'; } // register styles // wp_register_style( 'mobile-css', /theme_root/ui/css-mobile/mobile.css, false 'handheld' ); wp_register_style( $args['client'].'-css', $ui_path.$args['client'].$ui_suffix, false, $args['screen'] ); // Requires PHP 5.3+ (for lower versions, use a plain function). add_filter( 'set_theme_client', function('set_theme_client') { return $args['client'];} ); // {$template}-{$suffix}.php if ( file_exists( $template_path.$args['file'].'-'.$args['suffix'].'.php' ) ) { require( $template_path.$args['file'].'-'.$args['suffix'].'php' ); } // {$template}.php elseif ( file_exists( $template_path.$args['file'].'.php' ) ) { require( $template_path.$args['file'].'.php' ); } // {$template}-default.php elseif ( file_exists( $template_path.$args['file'].'-default.php' ) ) { require( $template_path.$args['file'].'-default.php' ); } } // CALL THEM in a template file // This will require a file named {$template}-{$suffix}.php from your devices folder // based on your conditional functions that detect your devices // If not found, it will search for a file named {$template}.php // and if it wasn't found it will search for a file named {$template}-default.php get_device_template_part( array( 'file' => 'nav', 'suffix' => 'main' ) ); ?>
N'hésitezpas à ajouter la détection conditionnelle de votre appareil à: https://gist.github.com/886501You could also write your own simple get_template_part alias function:
The following allows 3 subfolders for template parts that sit in a theme root folder named devices.<?php // STYLESHEETS function print_device_styles( $client = 'desktop' ) { $client = apply_filters( 'set_theme_client', $client ); wp_enqueue_style( $client.'-css' ); } add_action( 'wp_head', 'print_device_styles', 11 ); // TEMPLATE PARTS function get_device_template_part( $args = array( 'file' => NULL, 'suffix' => 'default', 'client' => 'desktop', 'media' => 'screen' ) { if ( ! $args['file'] ) wp_die( sprintf( __('You have to specify a file name if you want to load a template part with the %1$s function.', 'textdomain', '<pre>get_device_template_part()</pre>' ); $template_path = user_trailingslashit( get_stylesheet_directory().'/devices/templates-'.$args['client'] ); $ui_path = user_trailingslashit( get_stylesheet_directory().'/ui/css-'.$args['client'] ); $ui_suffix = '.css'; // could be switched between '.dev.css' & '.css' // add styles & template directory if ( is_condition_mobile() ) { $args['client'] = 'mobile'; $args['screen'] = 'handheld'; } elseif ( is_condition_tablet() ) { $args['client'] = 'tablet'; $args['screen'] = 'handheld'; } // register styles // wp_register_style( 'mobile-css', /theme_root/ui/css-mobile/mobile.css, false 'handheld' ); wp_register_style( $args['client'].'-css', $ui_path.$args['client'].$ui_suffix, false, $args['screen'] ); // Requires PHP 5.3+ (for lower versions, use a plain function). add_filter( 'set_theme_client', function('set_theme_client') { return $args['client'];} ); // {$template}-{$suffix}.php if ( file_exists( $template_path.$args['file'].'-'.$args['suffix'].'.php' ) ) { require( $template_path.$args['file'].'-'.$args['suffix'].'php' ); } // {$template}.php elseif ( file_exists( $template_path.$args['file'].'.php' ) ) { require( $template_path.$args['file'].'.php' ); } // {$template}-default.php elseif ( file_exists( $template_path.$args['file'].'-default.php' ) ) { require( $template_path.$args['file'].'-default.php' ); } } // CALL THEM in a template file // This will require a file named {$template}-{$suffix}.php from your devices folder // based on your conditional functions that detect your devices // If not found, it will search for a file named {$template}.php // and if it wasn't found it will search for a file named {$template}-default.php get_device_template_part( array( 'file' => 'nav', 'suffix' => 'main' ) ); ?>
Feel free to add your conditional device detection to: https://gist.github.com/886501-
Merci.Question supplémentaire,est-ce unebonnepratique d'utiliser OOphppour laplupart du code sur lesthèmes wordpress?Thanks. Additional question, Is it a good practice to use OO php for most of the code on wordpress themes?
- 0
- 2011-03-25
- OpenOne
-
@OpenOne Oui,la POOest unebonnepratique.J'aiplusieurs classes dansmonframework,quigénèrentmabase àinit.L'extensionestfacile sij'aibesoin d'ajoutspour unthème spécifiqueet queje n'aipas àm'occuper de l'écriture de lafonctionnalité debaseencoreet encore (réutilisation).Deplus,les classes sontplusfaciles àtransporteret gardent votreespace denomspropre.J'utilise lesfonctionspubliques uniquementpour lesbalises demodèle.Et laplupart desbalises demodèlene font rien que d'appeler desfonctions de classe.@OpenOne Yes, OOP is good practice. I have several classes in my framework, that generate my base at init. Extending is easy if i need some additions for a specific theme and i don't have to take care of writing the base functionality again and again (re-useability). Also classes are easier portable and keep your namespace clean. I use public functions only for template tags. And most template tags do nothing than calling class functions.
- 0
- 2011-03-26
- kaiser
-
Merci.Jepense étendre les objets actuelspour lespériphériques (bureau/mobile)et lespréférences (HTML4/5) document.Je voudrais utiliser des classespourtout afficher.Le résultat sembletrèsnet,maisje ne suispas sûr queje le rendetrop complexe (pour l'utilisateur).Thanks. I am thinking about extending the current objects for devices(desktop/mobile) and preferences(HTML4/5) document. I would like to use classes to output everything. The result looks very clean, but am not sure if i am making it too complex(for the user).
- 0
- 2011-03-27
- OpenOne
-
@OpenOne Jetez un œil à ceci: http://www.sencha.com/products/touch/- c'estgratuitpour les développeurset ça a l'airplutôtbien.Si vous voulez queje regarde,je vous donneraimonemailet vouspourrezme l'envoyer.@OpenOne Take a look at this: http://www.sencha.com/products/touch/ - it's free for developers and looks pretty good. If you want me to take a look i'll give you my email and you can send it to me.
- 0
- 2011-03-27
- kaiser
-
- 2011-03-25
Engénéral,lespluginset lesthèmes ont lesmêmespossibilités. Mais à certainsmoments,il y a unegrande différence. Lesthèmes viennentplustardet ont doncmoins depuissance. Plus: denombreusesfonctionsne fonctionnent quepour lesplugins.
Ce que vousessayez defaire,c'est demodifier le comportement debase. Dans certainsendroits,corepropose des hookspourmêmeen échanger complètement despartieset dans d'autresil offre des hookspour étendre sesfonctionnalités. Maisgardez à l'esprit que lenoyaune veutjamais vraiment que son comportement soit vraiment changéen ce qui concerne lesthèmes (voir les sous-dossierset
get_template_part()
). Jeprendrais donc du reculpour changer dethème `` à l'intérieur '' d'unthème (c'est commemodifier la requêteprincipale)et chercher une autre approche:// inside one of your template files: do_action( 'main_nav_hook' ); // inside your functions.php define( 'THEME_DESKTOP_DIR', user_trailingslashit( get_stylesheet_directory().'/desktop' ) ); define( 'THEME_MOBILE_DIR', user_trailingslashit( get_stylesheet_directory().'/mobile' ) ); define( 'THEME_TABLET_DIR', user_trailingslashit( get_stylesheet_directory().'/tablett' ) ); function add_main_nav_desktop() { // do stuff require_once( THEME_DESKTOP_DIR.'template_nav.php' ); } function add_main_nav_mobile() { // do stuff require_once( THEME_MOBILE_DIR.'template_nav.php' ); } function add_main_nav_tablett() { // do stuff require_once( THEME_TABLET_DIR.'template_nav.php' ); } // Hook the actions if ( is_condition_mobile() ) { add_action( 'main_nav_hook', 'add_main_nav_mobile' ); } elseif ( is_condition_tablet() ) { add_action( 'main_nav_hook', 'add_main_nav_tablet' ); } else { add_action( 'main_nav_hook', 'add_main_nav_desktop' ); }
Il s'agit d'une approchetrès simpleet basique,maiselle aplus de chances defonctionner.
Generally Plugins and Themes have the same possiblities. But at some points, there's a big difference. Themes come later and therefore have less power. Plus: a lot of functions only work for plugins.
What you're trying to do is modifying core-behavior. In some places core offers hooks to even completely exchange parts of it and in others it offers hooks to extend its functionality. But keep in mind that the core never really wants its behavior to be really changed when it comes to themes (see subfolders and
get_template_part()
). So i would step back from switching themes 'inside' a theme (it's like modding the main query) and look for some other approach:// inside one of your template files: do_action( 'main_nav_hook' ); // inside your functions.php define( 'THEME_DESKTOP_DIR', user_trailingslashit( get_stylesheet_directory().'/desktop' ) ); define( 'THEME_MOBILE_DIR', user_trailingslashit( get_stylesheet_directory().'/mobile' ) ); define( 'THEME_TABLET_DIR', user_trailingslashit( get_stylesheet_directory().'/tablett' ) ); function add_main_nav_desktop() { // do stuff require_once( THEME_DESKTOP_DIR.'template_nav.php' ); } function add_main_nav_mobile() { // do stuff require_once( THEME_MOBILE_DIR.'template_nav.php' ); } function add_main_nav_tablett() { // do stuff require_once( THEME_TABLET_DIR.'template_nav.php' ); } // Hook the actions if ( is_condition_mobile() ) { add_action( 'main_nav_hook', 'add_main_nav_mobile' ); } elseif ( is_condition_tablet() ) { add_action( 'main_nav_hook', 'add_main_nav_tablet' ); } else { add_action( 'main_nav_hook', 'add_main_nav_desktop' ); }
This is a really simple and basic approach, but it's more likely to work.
-
Enfait,mon approcheprécédente était similaire à celle-ci,en faisant desinclusionsbasées sur des appareils.Mais aucuneméthode de détection denavigateur/objetn'estparfaite,ce quinousprésente le risque -pas de sortie.Existe-t-il également unebase de données d'informations sur les appareilspresque complète,bien entretenueet ouverte?Actually My previous approach was similar to this, doing some includes based on devices. But no Browser/object detection method is perfect, which presents us the risk - no output. Also is there a near complete, well maintained, open, device information database out there?
- 0
- 2011-03-27
- OpenOne
-
- 2012-01-14
J'ai découvert que
add_filter('template', ...)
etadd_filter('stylesheet', ...)
doivent être appelésen mêmetempspourçamarche ... dumoins dansmon cas.I found out that
add_filter('template', ...)
andadd_filter('stylesheet', ...)
must be called at the same time for it to work... at least in my case. -
- 2011-03-24
Avez-vousessayé de raccorder lefiltretemplate_directory?Ilexiste également des hookspour chacun des éléments demodèleet stylesheet_directory.
Have you tried hooking the template_directory filter? There are also hooks for each of the individual template elements and stylesheet_directory.
-
Oui,j'aiessayé ça.Utiliser add_filter sur `template_directory`et`template_directory_uri`ne sert à rien,même si `get_template_directory (et uri)` affiche lenouvelemplacement.Deplus,cela casse unpeu lethème actuel - Lors du rechargement de la section desthèmes d'administration,il revient authèmepar défaut - vingt dix.Yes i did try that. Using add_filter on `template_directory` and `template_directory_uri` does no good, Even though `get_template_directory(and uri)` displays the new location. Additionally, it kinda breaks the current theme - On reloading the admin themes section, It switches back to the default theme - twentyten.
- 0
- 2011-03-24
- OpenOne
Certainsplugins utilisent le
'template'
,'option_template'
et'option_stylesheet'
pour servir dynamiquement desmodèles wordpress (alternatifs). Parexemple,ServeDefaultToIESix
de Nathan Rice. Parexemple -Le code ci-dessusne fonctionne qu'àpartir d'unplugin wordpress. Ce dontj'aibesoin,c'est depasser à un autremodèle, situé dans l'un des sous-dossiers duthème actuel (modèle). Exemples: afficher un autrethème HTML5,proposer aux utilisateursmobiles une versionminimale du site ..etc.
J'aiessayé d'utiliser 'theme_root'et 'theme_root_uri' comme ci-dessous. Mais celane fonctionnepas.
Est-ce labonnefaçon deprocéder? Ouest-ce que quelqu'un connaît lamanièrepossible de lefaire? Merci d'avance.