Can I set wordpress theme via code - wordpress

I have been working on wordpress for a while. I know that we can manage themes via the admin panel and the selected theme and its configuration details are stored in the database.
Is there any way I can set the theme via my wordpress code?

Have a look at the switch_theme function on the Codex
Description
Switches current theme to new template and stylesheet names.
Accepts one argument: $stylesheet of the theme. ($stylesheet is the name of your folder slug. It's the same value that you'd use for a child theme, something like twentythirteen.) It also accepts an additional function signature of two arguments: $template then $stylesheet. This is for backwards compatibility.
Usage
<?php switch_theme( $stylesheet ) ?>

Related

Put some CSS style on single template page - Plugin WordPress

I'm developing my first WordPress plugin for an internship.
I know how to put style on the admin pages by wp_enqueue_style() and admin_enqueue_scripts. I also know wp_enqueue_scripts.
I created a custom post type to store businesses and a single template page in "templates" folder, called "single-myCPT.php".
The problem is: I don't understand how to call a css file for this single page.
The URL is like : mywebsite/myCPT/company
When I use the inspector and I go on "Network", there is no css file detected.
Sorry for my English, I'm a French guy who wants to improve that language :).
You can use wp_enqueue_style() and wp_enqueue_script() on front-end (non-admin) pages as well as admin pages.
Use the wp_enqueue_scripts action for the purpose.
function plugin_name_enqueue_frontend_style() {
wp_enqueue_style(
'plugin_name_frontend',
plugin_dir_url( __FILE__ ) . 'css/frontend.css');
}
add_action( 'wp_enqueue_scripts', 'plugin_name_enqueue_frontend_style' );
But, you should probably also include your frontend css on your admin page; you may need it.

How to find the current theme in any page of Drupal 7

As the title says,
I need to know the name of the theme being used in any drupal page.
For example we can set a default theme and a admin theme for our drupal site basically. For now lets consider only this part and don't get into user specific themes.
So in the above case the home page will load using default_theme and when you do to any admin pages, they will load in admin_theme(seven/bartik).
Now my question is I need to know what theme is being loaded when I navigate to any page of the site. May it be a node/add or node/edit or admin/* or page/* or any page.
Simply when I go to any page I shoudl know the theme name being used in that page and in which hook I can write my conditions.
Based on that I want to do some changes.
I tried using globals $theme, path_to_theme, drupal_get_path etc
When I use them ,they always return the default theme name and not the theme being used in a specific page.
I wrote exit statement for $variables in hook_preprocess_html and it returns value by exiting only if the page is normal users page. But not in admin pages like sitename/admin/content etc.,
Any one who can help.
Thanks in advance
Create a custom module, and put your code in function hook_init(){} in your .module.
No arguments.
function example_init() {
global $theme;
print $theme.'<br />'; // current theme name
print $GLOBALS['theme'].'<br />'; // current theme name
$theme_path = drupal_get_path('theme', $GLOBALS['theme']);
print $theme_path.'<br />'; // path to current theme
}

Is there some way to insert custom archive loop to any theme from plugin?

I'm coding plugin that create custom post type with custom fields. Now I need to build custom archive/category/tag templates which should contain custom fields.
The problem is that I couldn't insert template part inside archive loop from plugin.
Is there some hook to insert custom loop inside activated theme? Something like this:
add_filter('the_content', 'change_content_in_single_post');
Now I'm using this hook:
add_filter( 'template_include', 'change_whole_cpt_archive_template' );
... but when I use template_include hook it changes whole template and I need to do something like the_content filter. That hook get template of current theme and replace standart content template with my custom template - make it part of activated theme. It makes my CPT single page template compatible with any wp theme. Now I need to replace standart archive loop with my custom loop. For example: on archive page show posts without images... It must be something that replace result of standard get_template_part() function with custom template from plugin. That's what I`m searching for...
Anyway, maybe you know some better ways to make plugin (archive template) compatible with wp themes?
Huge thanks for any help!
you need to introduce some logic to only run on your cpt archive page.
add_filter( 'template_include', 'change_all_archive_template' );
function change_all_archive_template($template){
if ( is_post_type_archive('cptname') ) {
$template= 'find the template!';
//if( file_exists ) --> look in theme 1st??
//else --> load from plugin..
}
return $template;
}
There are quite a few plugins that use this technique to look in the active theme 1st and if not found, find the file in your plugin.
I found solution in woocommerce plugin files.
So... the best way to build custom archive template from plugin is to use template_include hook and in custom template file set header and footer from activated theme:
<?php get_header();
// custom archive loop
get_footer(); ?>
To make it more compatible with any wp themes use stanard wordpress and bootstrap classes.

Using Jetpack Portfolio Project in WordPress child theme does not call archive custom template

I have a child theme that uses the new Jetpack Portfolio Project custom post type and wish to modify archive.php to display custom results.
I'm using: WordPress v3.9.2; Theme: Child of Point, Jetpack is installed with Custom Content Types enabled, and Portfolio Projects selected in the Settings. (No other plugins that implement portfolio functionality are installed.)
According to the Codex:
Template Files
In the same way single posts and their archives can be displayed using
the single.php and archive.php template files, respectively,
single posts of a custom post type will use single-{post_type}.php
and their archives will use archive-{post_type}.php
and if you don't have this post type archive page you can pass BLOG_URL?post_type={post_type}
where {post_type} is the $post_type argument of the
register_post_type() function.
My understanding is that if you create files called single-jetpack-portfolio.php and archive-jetpack-portfolio.php within the child theme, WordPress will automatically use those files in place of single.php and archive.php respectively.
However, my child theme successfully calls single-jetpack-portfolio.php, but completely ignores archive-jetpack-portfolio.php, instead calling archive.php in the child.
I am stuck for a solution.
From the codex above, adding to the URL "?post_type=jetpack-portfolio" does cause the child theme to correctly use archive-jetpack-portfolio.php, but should I need to be manually modifying every single URL to explicitly specify this? Should WordPress not automatically be detecting this, as it does for the single-jetpack-portfolio.php file? How can I solve this?
I have tried:
Resetting the permalinks in case it was related to that (changing the option in Settings and saving and back again)
Adding an archive.php file to the child in addition to archive-jetpack-portfolio.php (I initially didn't have an archive.php in the child, so it used the parent's archive.php)
Publishing a new Jetpack portfolio project and updating an existing page (I read somewhere that publishing something might trigger Wordpress to see the changes)
Thanks in advance for any help.
I had the same problem described by the OP. When I visited mydomain.com/portfolio it would use the custom archive template. When I tried to view a project type it defaulted to the regular archive.php. I'm wondering if OP was viewing a project type page without realizing it.
My solution was to create a taxonomy template file. After playing around with it I figured out that
taxonomy.php
taxonomy-jetpack-portfolio-type.php
taxonomy-jetpack-portfolio-type-{name-of-project-type}.php
all worked correctly, depending on how specific you wanted to get.
There's more info at the wordpress codex: http://codex.wordpress.org/Template_Hierarchy#Custom_Taxonomies_display
Hope this helps someone.
I will be working on this the next days.
You should try this in the child archive.php first lines:
<?php
if( is_post_type_archive('jetpack-portfolio') )
get_template_part('content', 'jetpack-portfolio');
elseif( is_tax('jetpack-portfolio-type') || is_tax('jetpack-portfolio-tag') )
get_template_part('archive', 'jetpack-portfolio');
else continue;
?>

Can my WordPress custom templates be in the plugin folder or only in the theme folder?

A WordPress theme I am developing has an integrated custom post type called "albums" which utilizes a few custom templates (archive-albums.php, content-albums.php, etc.). What I want to do is transfer this functionality, along with the template files, into a plugin for the sake of portability.
I transferred the CPT code from the functions.php with success, but when I try to move the template files from the theme folder to the plugin folder, things fall apart. I feel like it should be simple to somehow register the templates so WordPress knows to load them.
Can my WordPress custom templates be in plugin folder or only theme folder?
Things are falling apart because when you move those files, you're violating WP's native template hierarchy. You'll need to explicitly declare the location of those files. Using the archive as an example, you could add something like this to functions.php (to tell WP to look elsewhere):
add_filter('template_include', 'include_album_template', 1);
function include_album_template($template_path) {
if(get_post_type() == 'albums') {
if(!is_single()) {
$theme_file = 'path-to-your-plugin-directory';
$template_path = $theme_file;
}
}
return $template_path;
}
Obviously you'd use your own path, and I wrote this hastily so you might want to refactor.
I have the same issue. I'm already using add_filter ('template_include', ...) problem is that I need to specify a file to return, and in this case being it,index.php. This raises an issue with the theme not running entirely as if installed via themes folder, because what I need is to have WP selecting the appropriate file to render without any conditional logic from my part. So if it is a post it will select the single.php and so on. Another problem raised with this method is that in header.php the call get_header (); ignores the local file header.php and loads the default theme installed file instead.

Resources