[wp-hackers] changing a theme via wpdb class

Andrew Nacin wp at andrewnacin.com
Sun Apr 29 21:51:54 UTC 2012


On Apr 29, 2012 3:51 PM, "Christopher Ross" <cross at thisismyurl.com> wrote:
>
> oops, I forgot an arg.
>
> add_action( 'template_redirect' , 'change_theme_manually' );
> function change_theme_manually() {
>
>    if ( 'twentyeleven' != get_current_theme() )
>        switch_theme( 'twentyeleven', 'style.css' );
>
> }

So the first problem here is that by using a front-end hook to writer to
the database, you risk running write queries on the frontend. That is
something you always want to avoid.

I would use a hybrid approach. On init, use the stylesheet and template
filters suggested by John. Then use switch_theme() if is_admin().
Additionally, use map_meta_cap to disable the switch_themes cap.

The second problem is that get_current_theme() returns the theme *name* —
as in, "Twenty Eleven". (The function is also deprecated in 3.4.) You want
get_stylesheet() for "twentyeleven".

Nacin


More information about the wp-hackers mailing list