Replacing only parts of an archive and single page template of WordPress - wordpress

I am having a bit of trouble here understanding how to do the following. I have searched for weeks now but cannot seem to find what I am looking for.
I have a custom post type 'product' and want to change which template gets loaded for the single product page as well as the archive for the products. I am using the following code to load include and load templates.
add_filter('template_include', function() {
if (is_post_type_archive('product')) {
$templatefilename = 'archive-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
return $template;
}
if ('product' == get_post_type() ){
$templatefilename = 'single-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
return $template;
}
});
The problem I am having is that it replaces the current theme's template instead of just the inner part of the content and archive areas.
Here is what I want to achieve:
Create a custom post type 'product' in a plugin - DONE (Was kinda easy!)
When opening a single product only change the content part. - I can do this with the_content filter hook. Simple enough. Any other suggestions is welcome.
When I go to the archive view for the 'product' custom post type I don't want to have it load the theme's default archive (list) view but instead a grid view from my plugin which I cannot seem to get right. I only want to change the inner part of the template, not the whole page.
I have created this plugin a few weeks ago using only shortcodes which works good but want to see if I can do it without the use of shortcodes by means of creating the custom post type and changing the inner template parts of the current active theme.
Can anybody steer me into the right direction here?
If I create a theme I can do what I am looking for but I want to create this into a plugin instead without adding or making changes to the active theme. The plugin should handle what is needed.
The same issue is discussed here but what I want is to develop something that is theme independent. No changes should be made in theme files and no theme files should be copied to the plugin.
WP - Use file in plugin directory as custom Page Template?

Recently I also had the same problem. Here's how I worked it out.
template_include filter accepts a parameter which is the selected template that you want to override (this what you are missing in your code).
I don't know but sometimes the filter hook need higher priority to work like 9999. But first check if it work with default priority, if don't change it.
I assume your both archive and single product template both have include get_header() and get_footer() which can be used for default selected theme (Or if the theme has different setup, setup accordingly).
This is simplified code:
add_filter('template_include', function($default_template) {
if (is_post_type_archive('product')) {
$templatefilename = 'archive-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
$default_template = $template;
} else if ('product' == get_post_type() ) {
$templatefilename = 'single-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
$default_template = $template;
}
// Load new template also fallback if both condition fails load default
return $default_template;
}, 9999); // set priority, only if not worked with default one

The best option in this case is to provide a shortcode to the user. So they can place it on any page that they want (or that you auto generate). That way you will place your content inside their theme.
Something like this:
add_shortcode( 'slotsl-game', 'embed_game' );
/**
* Print the game
* #return false|string
*/
function embed_game(){
ob_start();
$game = get_post();
include_once SLOTSL_PLUGIN_DIR . 'templates/slotsl-single-game.php';
return ob_get_clean();
}

Related

Custom product page layout by category ( Woocommerce )

I'm new to php and Woocommerce. I know this question has already been asked but i can't figure out how to make it work ... Right now, i have 3 different categories on my Woocommerce shop ( i'm developping using Understrap ). I've created my basic product page layout, everything works fine. Now, i'd like to create a custom layout for my products that are in my "cleaning" category.
What i did for now is add this to my functions.php child theme :
add_filter('template_include', 'cleaning_single_product_template_include', 10);
function cleaning_single_product_template_include($template)
{
if (is_product() && (has_term('cleaning', 'product_cat'))) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-cleaning.php';
}
return $template;
}
Then i've created a single-product-cleaning.php file inside my woocommerce folder. For now, i've just pasted an <h1>LOREM</h1> to see if it shows up on the page, but it doesn't ..
Now in the code i've added to my functions.php, if i replace the content of my function with a simple
echo "test"; die();
It works, the "test" message appears on my cleaning product pages. But if i let the code i've written as is, then it just display the regular product page from single-product ... Any idea what i am missing here ?
EDIT : i've found this thread on stackoverflow where he seems to have found a solution (see his last comment) but i don't understand what code he moved or how he solved it ..
Finally fixed! For those having the same problem, i've just modified a little bit my functions.php function like this :
add_filter('template_include', 'cleaning_single_product_template_include', 50, 1);
function cleaning_single_product_template_include($template)
{
if (is_singular('product') && (has_term('cleaning', 'product_cat'))) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-cleaning.php';
}
return $template;
}

How to remove wordpress date archive pages?

I have developed my own wordpress theme, and learning all kinds of programming stuff, but my priority is the content, not the programming knownledge. I need to know how to remove archive date listing from wordpress?
Question 1: The google search results displayed like this: virmodrosti.com/2017/05/
I don't want any kind of date archive option, how do you disable that?
I also don't use any kind of plugin, and always like to do it on my own.
Question 2: I don't know why older entries doesn't work anymore
virmodrosti.com/zdravje/ this page works fine
virmodrosti.com/zdravje/page/2/ it redirects to 404 error page
I only choose option in wordpress to hide that annoying /category/ with dash . inside editor at permanlinks, Category base. Maybe somehow these stuff is kinda fighting with each other and doesn't work properly.
Thank you.
This is code from Digital Nomad theme I maintain:
function digitalnomad_remove_date_archives() {
//if we are on date archive page
if ( is_date() ) {
// theme sets alternatine archive page with table like list of all posts
$archive_page = get_option( 'digitalnomad_archive_page' );
if ( $archive_page ) {
// redirs to alternatine archive page if configured (good for SEO)
wp_redirect( esc_url( get_page_link( $archive_page ) ) );
die();
} else {
// otherwise error 404 is displayed
global $wp_query;
$wp_query->set_404();
}
}
}
add_action( 'template_redirect', 'digitalnomad_remove_date_archives' );
Use the smart Archive Page Remover wordpress plugin
or visit your theme's functions.php file
then insert this code
/* Register template redirect action callback */
add_action('template_redirect','makes_remove_wp_archives');
/* Remove archive*/
function makes_remove_wp_archives(){
// if we are on category or tag or date or author archive
if(is_category()|| is_tag()||is_author()){
global $wp_query;
$wp_query->set_404();
}
}

Change Wordpress feed <link> for one specific tag

I have an external page that reads a RSS feed for a tag. I can't change anything on the external page, so the challenge is to change the RSS feed on my side to match the requirements.
On the external page the 3 latest posts for a tag are shown, and at the end of the section (note: not after each post but after all 3 posts) there is a "View all" link. This link receives its value from the element in the feed, which is by default set to my blog homepage, e.g. http://myblog.com). For this specific tag the link should be http://myblog.com/tag/myspecialtag.
The requirement is that the "View all" link links to the tag page instead of the homepage.
My idea was to add a condition to the element to change the URL for this specific category. I tried to change the feed template as recommended here: Customizing feeds, but for some reason it doesn't change the template at all. The code I tried is the following:
remove_all_actions( 'do_feed_rss2' );
add_action( 'do_feed_rss2', 'change_feed_rss2', 10, 1 );
function change_feed_rss2( $for_comments ) {
$rss_template = get_template_directory() . '/feeds/feed-custom_rss2.php';
if( file_exists( $rss_template ) )
load_template( $rss_template );
else
do_feed_rss2( $for_comments ); // Call default function
}
Of course I created the custom feed template and stored it in the feeds directory in my theme.
As this didn't work I tried looking into filters/hooks but I couldn't find anything helpful regarding my issue. Any ideas on how to best solve this issue?
I came up with the following solution:
I created a new custom page template custom_rss-feed.php and copied the code from wp-includes/feed-rss.php into it. I assigned this template to a new page. Additionally I added a filter to get_bloginfo_rss like the following:
function alter_bloginfo_rss($value, $key) {
if ( $key === "url" &&
is_page_template("custom_rss-feed.php")
) {
return $value . "/my/custom/url";
}
return $value;
}
add_filter("get_bloginfo_rss", "alter_bloginfo_rss", 10, 2);

execute do_shortcode in functions.php

I'm trying to run a do_shortcode in functions.php with no luck
I'm using Types plugin http://wp-types.com/ for creating custom post types and custom fields.
What I'm trying do is adding a custom column in admin for view all custom posts that displays a thumbnail from a custom field.
This is what I got so far, but it seems that the shortcode doesn't work inside functions.php
// add a column for custom post type (products)
add_filter('manage_product_posts_columns', 'add_thumbnail_column');
add_action('manage_product_posts_custom_column', 'add_thumbnail_content', 10, 2);
function add_thumbnail_column($defaults)
{
$newSlice = array('thumbnail' => 'Image preview');
$counter = 2;
$array_head = array_slice($defaults,0,$counter);
$array_tail = array_slice($defaults,$counter);
$defaults = array_merge($array_head, $newSlice);
$defaults = array_merge($defaults, $array_tail);
return $defaults;
}
function add_thumbnail_content($column_name, $post_ID)
{
// this one works when putting into post content
echo do_shortcode('[types field="square-picture" id="' . $post_ID . '" size="thumbnail"]' . '[/types]');
}
Can anyone help please?
In the Wordpress notes for the function it says
"If there are no shortcode tags defined, then the content will be
returned without any filtering. This might cause issues if a plugin is
disabled as its shortcode will still show up in the post or content."
Types may be conditionally declaring their short code only when you are on the frontend. What may be happening is that, in the admin, the short code is not defined and you are simply getting a false return. While on the frontend, the shortcode is defined and you get the results you intended.

is there a way (plugin) to insert html/css/images ect automatically into every wordpress post?

is there a way (plugin) to insert html/css/images ect.. automatically into every wordpress post? most of my posts are going to be very similar so is there a plugin that will automatically insert the pre written html/css/ ect in every post as opposed to me entering it manually every time.
Thank you in advance ;-)
You can write your own simple function for this, see my example below:
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
global $post_type;
if( $post_type == 'post') { /* Or your custom post type, pages etc. */
$content = 'Your custom HTML/CSS content here';
}
return $content;
}
Place this in functions.php and it will be the default content of every new post/page/custom post type you create.
For a list of available post types, please refer to the Codex
You could use a plugin such as Ad injection, it will allow you to do what you need without having to alter / amend / ad any code to the templates or files

Resources