I'm trying to make my first plugin and I got stuck. Here is the idea.
When my plugin is activated it will create one post type and two taxonomies for that post type (in my case the post type name is 'Ads').
Also, I created two template pages, one to display the listing of all ads post type articles and the other for a single page for the same post type.
Now my problem is how to tell WordPress to look for the templates from a plugin folder rather than the theme folder when the plugin is active.?
Is it something I can do in the plugin file or I have to create another file for this purpose?
This should do what you are looking for:
First, this hook to tell WordPress which is your single CPT template in your plugin
From this answer you get the single_template hook and how to load it.
Define a constant to replace "plugin_dir_path( FILE )" if you use it elsewhere in your plugin, like this:
define('YOUR_PLUGIN_DIR_PATH', trailingslashit(plugin_dir_path( __FILE__ )) );
https://wordpress.stackexchange.com/questions/17385/custom-post-type-templates-from-plugin-folder
function load_single_ad_template( $template ) {
global $post;
if ( 'ads' === $post->post_type && locate_template( ['single-ads.php'] ) !== $template ) {
/*
* This is an 'ads' post
* AND a 'single ad template' is not found on
* theme or child theme directories, so load it
* from our plugin directory from inside a /templates folder.
*/
return YOUR_PLUGIN_DIR_PATH . 'templates/single-ads.php';
}
return $template;
}
add_filter( 'single_template', 'load_single_ad_template', 10, 1 );
And then for the ads archive template, the 'archive_template' hook, like this:
function load_archive_ads_template( $archive_template ) {
global $post;
if ( is_post_type_archive ( 'ads' ) ) {
$archive_template = YOUR_PLUGIN_DIR_PATH . 'templates/archive-ads.php';
}
return $archive_template;
}
add_filter( 'archive_template', 'load_archive_ads_template', 10, 1 ) ;
Official documentation:
https://developer.wordpress.org/reference/hooks/type_template/
https://codex.wordpress.org/Plugin_API/Filter_Reference/archive_template
This is untested, but should work, however, let me know.
Related
I created a Custom Post Type, and it has some categories in it.
I created forms with User Frontend Pro for CPT categories.
I’m looking for a solution for assigning the custom templates to that records to show them on the website.
For example;
Assume that I have a CPT named Project.
The project post type has two categories; “Business” and “Ideas”
Users can post their entries with forms to these categories and their posts listed under the account dashboard.
The issue is that;
Business categories should be shown with the category-business.php custom template, and in a similar way, the Ideas category should be shown with category-ideas.php.
How can I add custom template to CPT categories?
P.S. I tried the template hierarchy solution that is in the WP documents, but it's not working. Because there is a custom view function for content. I looking for a code snippet that adds the template page attributes to custom posts automatically, if it is possible.
Assuming that you are just using the default WordPress category you can the pre_get_posts Hook to include the project as part of the category page if they are not showing by default
function include_project_to_cat_pages( $query ) {
if ( $query->is_category() && $query->is_main_query() ) {
$query->set( 'post_type', array( 'post', 'project' ) );
}
}
add_action( 'pre_get_posts', 'include_project_to_cat_pages' );
I solved that issue with a filter.
add_filter( 'single_template', function( $template ) {
global $post;
$cat = get_the_category()[0];
if ( $post->post_type === 'project' && $cat->slug === 'business') {
$locate_template = locate_template( "custom-business-template.php" );
if ( ! empty( $locate_template ) ) {
$template = $locate_template;
}
}
return $template;
} );
Thank you to all contributors and the community.
I've been pulling my hair out with this all day, please forgive the short description, I just need to validate my sanity!!
As the title says, I'm trying to create two or three different single-product layouts within woocommerce. The minimum is trying to achieve would be to have multiple single-product folders each with their own name and configurations.
No matter which way I try to override the single-product.php and make this file use logic to check for the product_cat and give out templates accordingly, I either the page not loading or what I write is skipped over and the default is loaded.
So far I've been through the following methods multiple times, trying to piece together what may be outdated code or otherwise causing all the fuss:
WooCommerce - How to create multiple single product template based on category?
Woocommerce single product - template by categories
Creating a different template file for certain Product Categories - Wordpress/Woocommerce?
I was more hoping someone may know something about this that I'm obviously missing as there are many articles out there on what to try and most claim success but I'm unable to do so.
[Update] using template_include code from #helgatheviking
No success just yet but here's where I'm up to;
File structure
team-shops is the category I'm trying to get
/mytheme/woocommerce/single-product.php - no changes
/mytheme/woocommerce/content-single-product.php
/mytheme/woocommerce/single-product-team-shops.php - changed line 37 to<?php wc_get_template_part( 'content', 'single-product-team-shops' ); ?>
/mytheme/woocommerce/content-single-product-team-shops.php - added additional id to #product-id (line 39)
/mytheme/woocommerce/single-product-team-shops/ folder with all single product files to change.
As I said above this isn't working but hopefully with what I've provided the problem may be more obvious.
Thanks again for any help :)
[Think I've got it]
Ok so I think I've something that works, at least for now it seems to, still have some further testing to do but any thoughts more than welcome, this is what I've got so far along with a single-product-team-shops folder in my theme
add_filter( 'woocommerce_locate_template', 'so_25789472_locate_template', 10, 3 );
function so_25789472_locate_template( $template, $template_name, $template_path ){
$term_id = 2854;
$taxonomy_name = 'product_cat';
$term_children = get_term_children( $term_id, $taxonomy_name );
foreach ( $term_children as $child ) {
// on single posts with mock category and only for single-product/something.php templates
if( is_product() && has_term( $child, 'product_cat' ) && strpos( $template_name, 'single-product/') !== false ){
// replace single-product with single-product-mock in template name
$mock_template_name = str_replace("single-product/", "single-product-team-shops/", $template_name );
// look for templates in the single-product-mock/ folder
$mock_template = locate_template(
array(
trailingslashit( $template_path ) . $mock_template_name,
$mock_template_name
)
);
// if found, replace template with that in the single-product-mock/ folder
if ( $mock_template ) {
$template = $mock_template;
}
}}
return $template;
}
Use a single-product-custom.php template for any product in the "custom" category:
add_filter( 'template_include', 'so_43621049_template_include' );
function so_43621049_template_include( $template ) {
if ( is_singular('product') && (has_term( 'custom', 'product_cat')) ) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-custom.php';
}
return $template;
}
NB: If you use the same action hooks in your single-product-custom.php template you will get the same look as the default single-product.php. You could 'rename' all the hooks and then could add existing functions (such as those for add to cart buttons, etc) to the new hooks in order to achieve a totally custom look.
I'm currently using archive.php as a generic listing for categories, tags, authors, etc. My theme have 4 custom post type and i want each post type have separate archive template.
How to add separate archive template for each custom post type?
I have a code but not working is there i m missing some thing?
add_filter( 'template_include', 'wpsites_cpt_archive_page_template', 99 );
function wpsites_cpt_archive_page_template( $template ) {
if ( is_post_type_archive(array( first-post-type ) ) ) {
$new_template = locate_template( array( 'your-cpt-template.php' ) );
if ( '' != $new_template ) {
return $new_template ;
}
}
return $template;
}
checkout wordpress template files docs,
you don't need to use conditionals for separating post type template,
you can simply create archive-{your-post-type}.php file to have different archive template for each post type.
for taxonomy;
taxonomy-{taxonomy}-{term}.php
taxonomy-{taxonomy}.php
tag-{slug}.php
tag-{id}.php
category-{slug}.php
category-{ID}.php
Additionally, if your theme supports standard loop hooks like Genesis Framework, You can use conditional to build custom loop depending on Post type.
lets say you have custom post types (CTP).. cpt1, cpt2, cpt3 ...
then you just need to create archive templates files respectively like that
archive-cpt1.php , archive-cpt2.php , archive-cpt3.php .
So I want to include fusion builder's post editor on some custom post types that I've created. There is a fix to this go to fusion-core > admin > class-pagebuilder.php and edit line 53.
var $allowed_post_types = array('page','post','avada_faq','avada_portfolio', 'add my custom types here');
But anytime there is an update this will be deleted and I'd like to not have to worry about this, or worry about it! So is there anyway i can create a plugin helper, or add something to my functions.php file that wont get replaced every time there is an update.
the instance is create line 22 of the file fusion-core.php
so to overide this, you can try something like this :
add_action ("plugins_loaded", function () {
// unregister habitual call
remove_action( 'plugins_loaded', array( 'Fusion_Core_PageBuilder', 'get_instance' ) );
// call of the instance
// you have to change the path of the fusion-core plugin
if( ! get_option( 'avada_disable_builder' ) ) {
if ( is_admin() ) {
require_once( path of the fusion-core plugin . 'admin/class-pagebuilder.php' );
$instance = Fusion_Core_PageBuilder::get_instance();
$instance->allowed_post_types[] = "custom post type";
}
}
}, 9); // priority 9 to be called before the line of fusion-core.php
For me, I found that there was no need to remove the action...and in fact, it did not work when I did so. Instead, I simply called the same action and changed the values. I also found that I only needed the "UI instance". Here's what worked for me. I've now added the page builder successfully to ten different post types on my site see below:
if ( is_admin() ) {
require_once('path to plugin directory/fusion-core/admin/class-pagebuilder.php' );
require_once('path to plugin directory/fusion-core/admin/page-builder/classes/class-ui.php' );
$ui = Fusion_Core_PageBuilder_UI::get_instance();
$ui->settings['allowed_post_types'][] = 'your_custom_post_type';
}
How can i disable the comments form for a specific category in Wordpress.
And i mean with that every post the user will publish it in this category will not has a comment form content.
Rather than doing this in functions.php, you could create two template files. The first template file is your standard theme template, the second would be identical, except it wouldn't contain the comment code section.
Simply name the template file that doesn't have the comment code block category_{id}.php and upload to your theme folder. The ID is the ID of the category you want to disable comments on.
More information on category specific templates here https://developer.wordpress.org/themes/basics/template-hierarchy/#category
More information about the comment template here https://codex.wordpress.org/Function_Reference/comments_template
If you still want to do this via functions.php, see this blog post http://spicemailer.com/wordpress/disable-hide-comments-posts-specific-categories/ which uses the following code snippet
add_action( 'the_post', 'st_check_for_closed' );
function st_check_for_closed()
{
global $post;
$my_post_cat = wp_get_post_categories($post->ID);
$disabled_cat = array( "1", "3"); // this is he array of disabled categories. Feel free to edit this line as per your needs.
$my_result = array_intersect($my_post_cat,$disabled_cat);
if (empty ( $my_result ) )
{
return;
}
else {
add_filter( 'comments_open', 'st_close_comments_on_category', 10, 2 );
add_action('wp_enqueue_scripts', 'st_deregister_reply_js');
}
}
function st_deregister_reply_js()
{
wp_deregister_script( 'comment-reply' );
}
function st_close_comments_on_category ($open, $post_id)
{
$open = false;
}