How to set the Theme programatically in WordPress? - php

I got situation like I am using multiple themes in my php website and also integrate a wordpress blog.
For example this is my site URL: http://example.com
There I want to switch Themes by passing a query parameter like:
http://example.com?mytheme=red_theme
http://example.com?mytheme=blue_theme
etc.
Currently my activated theme in WordPress is like blue_theme and my WordPress blog URL is like:
http://example.com/blog?mytheme=red_theme
e.g.: red_theme should be display just like preview.
Otherwise if I go through this URL:
http://example.com/blog
Then the default theme (blue_theme) should be display.
I can adjust it in core PHP but i don't know how to do it with WordPress.

In WORDPRESS, you can set Theme Programmatically, Based on Device, like different theme on mobile and different theme on Desktop. Write below code in functions.php of your Default theme
function use_mobile_theme() {
// Chech device is mobile or not
if(wp_is_mobile()){
return 'theme19388'; // set theme name here, which you want to open on mobile
}
else {
return 'milano'; // set theme name here, which you want to open on other devices, like desktop
}
}
add_filter( 'stylesheet', 'use_mobile_theme' );
add_filter( 'template', 'use_mobile_theme' );

Related

Wordpress dynamic child page without 404

I want to create a subpage, e.g. /test/ and everything that I enter after /test/ should have one specific template and indexing, no 404 error.
I wanted to make this with virtual pages, but it's too many url's to add. (tried here - Wordpress fake/virtual folder)
I've got my template page-pagename.php which works. Now I need to add that every child of test does not return 404.
I think I have already searched the entire internet and cannot find a solution to this task
What you could fiddle with is with grabbing the main query on 'template_include'. You add an action like this. You can add it to your child theme in functions or in your custom plugin.
add_action( 'template_include', 'custom_router' );
Then in the custom router function, you can check the parameters that you are requesting ('test') and redirect to a template of your choice. Also add in functions.php or in custom plugin. Place a template file in the relevant path.
function custom_router( $query ) {
global $wp_query;
if($wp_query->query['name'] == 'test') :
var_dump($wp_query->query['name']);
var_dump($wp_query->query['page']);
return dirname( __FILE__ ) . '/some-template.php';
endif;
}
I tested the code in the latest wordpress version with a custom plugin and the default theme btw.

Change WordPress Profile Gravatar to generated robohash

By default, WordPress admin section allows us to choose from the following Gravatar options:
Mystery Person, Blank, Gravatar Logo, Identicon (Generated), Wavatar (Generated), MonsterID (Generated), Retro (Generated)
However, when I visit https://en.gravatar.com/site/implement/images/. I can see an option for generated robohash. All I have to do is replace wavatar in all image URLs with robohash.
https://secure.gravatar.com/avatar/b7556ca086c0d99f2000b73e8f4ce4ea?s=96&d=wavatar&r=g
// will become
https://secure.gravatar.com/avatar/b7556ca086c0d99f2000b73e8f4ce4ea?s=96&d=robohash&r=g
I can do it on my own vanilla PHP website because I have full control over the HTML on a page. However, I don't know how to use robohash avatars on a site with WordPress installation.
First, I thought that I can use JavaScript to select all images on a page and then replace wavatar with robohash but it seems very unWordPress like and error prone.
Is there a clean and efficient way of doing this like using add_filter() etc.?
Thanks.
I have changed a default to my custom rest of you need to manage.There below code will change default gravatar:
add_filter( 'avatar_defaults', 'set_new_gravatar' );
function set_new_gravatar($avatar_defaults) {
$myavatar = 'http://example.com/wp-content/uploads/2017/01/new-default-gravatar.png'; // response image URL
$avatar_defaults[$myavatar] = "Default Gravatar";
return $avatar_defaults;
}
There is plugin for this : https://wordpress.org/plugins/robohash-avatar/

WordPress independent functions.php

I have a fresh WordPress and bbPress installed on an internal server.
While I was setting up bbPress I wanted to test the functionalities like creating a forum, topic, etc. When doing these stuff in the back-end (dashboard) there didn't seem to be any problems but when I did it from the front-end I kept getting
ERROR: Are you sure you wanted to do that?
I searched for a solution and found this.
add_filter( 'bbp_verify_nonce_request_url', 'my_bbp_verify_nonce_request_url', 999, 1 );
function my_bbp_verify_nonce_request_url( $requested_url )
{
return 'http://localhost:8888' . $_SERVER['REQUEST_URI'];
}
I simply changed the hard-coded URL to what our internal server is set up to and it fixed the problem.
Now my question: is it possible for me to add this solution to a functions.php that is independent of the theme being used? I asked this because I have 2 concerns:
The current theme might get updated and will overwrite the file
I'm aware that the solution to this is simply create a child theme but my second concern prevents me from doing this.
The WordPress administrator might change themes and so both the functions.php file on the main theme and the child theme will stop working
How could I add the solution above so that I don't have to worry about the theme being updated and/or replaced with a new theme in the future? I don't want to keep adding this solution every time the administrator decides to change themes.
If you can't put it in a theme, put it in a Plugin. If you're worried that the plugin will get de-activated make it a Must Use Plugin.
It's dead simple to create a plugin. Create a file plugin-name.php and place it in a directory wp-content/plugins/plugin-name/. That file should contain the following code:
<?php
/*
Plugin Name: Name Of The Plugin
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version: The Plugin's Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2
*/
add_filter( 'bbp_verify_nonce_request_url', 'my_bbp_verify_nonce_request_url', 999, 1 );
function my_bbp_verify_nonce_request_url( $requested_url )
{
return 'http://localhost:8888' . $_SERVER['REQUEST_URI'];
}
If you want it to be a must use plugin, put it in wp-content/mu-plugins/ instead of wp-content/plugins.

Wordpress conditional permalink

I have a specific page on my wordpress website example.com/download/ that I already have a page for. I would like to add some sort of php code to enable the following functionality.
If there is a subpath after the /download/<name>, I would like to fetch a file from some directory based on <name>
If no subpath is specified (/download/) I want to display the page that is already written
Is this possible with Wordpress hooks/filters?
I have a feeling it has something to do with the WP_Rewrite class, but I am not sure exactly what to write, nor where to put it without WP or theme updates wiping the code
Yes, you have to add a custom rewrite rule. You can use your theme's functions.php to add the new functionality.
function custom_rewrite_rule() {
add_rewrite_rule('^download\/([^/]+)\/?','your_url_to_the_downloader_file.php?download=$matches[1]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
To make it work, first you need to go to Admin -> Settings -> Permalinks and click the save button so the new rule is added.

Fishpig_Wordpress Magento extension. Theme a custom category

I have an installed addon for magento called Fishpig. It essentially runs wordpress through magento allowing both to be used on the main website. The WP install is being used for a blog, and i've the whole initial theme set up for it by altering the magento files as required. What i'm looking for though is a way to change the theme if i look under a certain category related to the representative for the site.
Is there a way to set a different template if i were to choose the category? Will i need to add if statements to the category WP layout file?
If you want to change the whole theme based on the current WordPress category (or any conditions), you would need to listen to for an event and then change the theme programmtically. The most general event that would work would be 'controller_action_predispatch' however if you wanted to only change the theme for WordPress category pages, you would be better suited to use 'controller_action_predispatch_wordpress_post_category_view'.
Attach an event observer method to the event of your choosing and then use the following code:
$_category = Mage::registry('wordpress_category');
if (!$_category) {
return $this;
}
$_categoryId = (int)$_category->getId();
if ($_categoryId === 1) {
Mage::getDesign()
->setPackageName('default')
->setTheme('default');
}
else if ($_categoryId === 2) {
Mage::getDesign()
->setPackageName('default')
->setTheme('default');
}
return $this;
You would need to modify to code to set the correct package/theme (the code below enables the default package and default theme) to match the package/theme you want to set.

Categories