Custom template page for several permalinks wordpress - wordpress

I built a custom template page named Single Listing. permalink for this page is / homes /
But I'd like to show this custom template page for all the urls formatted like this homes/*
I am new to wordpress so any help or suggestions would be welcome.
Thanks.

Not tested, but this might be helpful for you.
add_filter( 'template_include', 'portfolio_page_template', 99 );
function portfolio_page_template( $template ) {
global $wp;
$current_url_path = home_url( $wp->request );
$url_pattern = '/(^homes[\/\w]*)/gim'
if ( is_page() && preg_match($pattern, $current_url_path)) {
$new_template = locate_template( array( 'home-page-template.php' ) );
if ( '' != $new_template ) {
return $new_template ;
}
}
return $template;
}

Related

How can I replace a custom-post-type with a custom post category in permalink without getting a 404 in wordpress?

I'm a wordpress newbie. I will try to describe my problem in the clearest way possible.
I'm trying to do two things here:
Remove CPT from a permalink.
Add a custom taxonomy type where it used to be the CPT in the permalink.
Permalinks on site used to be like this:
http://example.com/custom-post-type/post-name
I managed to remove the CPT from the permalink based on this:
how to remove custom post type from wordpress url?
Then I modify my code to add the custom taxonomy type to the permalink to be like this:
http://example.com/post-category/post-name
This is my code:
function remove_cpt_slug( $post_link, $post ) {
if ( 'custom-post-type-name' === $post->post_type && 'publish' === $post->post_status ) {
$post_tags = get_the_terms($post->ID, 'custom-post-type-name-category');
$post_link = str_replace( '/' . $post->post_type . '/', '/' . $post_tags[0]->slug . '/', $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'remove_cpt_slug', 10, 2 );
function add_cpt_post_names_to_main_query( $query ) {
// Return if this is not the main query.
if ( ! $query->is_main_query() ) {
return;
}
// Return if this query doesn't match our very specific rewrite rule.
if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
return;
}
// Return if we're not querying based on the post name.
if ( empty( $query->query['name'] ) ) {
return;
}
// Add CPT to the list of post types WP will include when it queries based on the post name.
$query->set( 'post_type', array( 'post', 'page', 'custom-post-type-name' ) );
}
add_action( 'pre_get_posts', 'add_cpt_post_names_to_main_query' );
This worked but now every post from my CPT gives a 404. How can I solve this?

Woocommerce order_review custom template on cart items update

I have develop a custom template for woocommerce checkout page and added all templates that I need to change specifically order_review.php in plugin/woocommerce/checkout/order_review.php and on order page it works perfect.
From Order page I can remove some products or add products through ajax and here is my ajax code.
ob_start();
woocommerce_order_review();
$woocommerce_order_review = ob_get_clean();
$response = array(
'cart_total' => WC()->cart->total,
'cart_item_key' => $new_key,
'fragments' => apply_filters(
'woocommerce_update_order_review_fragments',
array(
'.woocommerce-checkout-review-order-table' => $woocommerce_order_review,
)
),
);
if ( ! empty( $data ) ) {
$response['cartflows_data'] = $data;
}
return $response;
And the woocommerce_order_review(); loads woocommerce default template instead of template from my plugin.
WooCommerce does not look in plugin folders for templates by default. You need to use the filter woocommerce_locate_template in your plugin to tell WooCommerce to look in your plugin folder for templates. The solution is outlined by SkyVerge in this blog post: https://www.skyverge.com/blog/override-woocommerce-template-file-within-a-plugin/
I am including the solution here in case the post is every removed for some reason:
function myplugin_plugin_path() {
// gets the absolute path to this plugin directory
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
$_template = $template;
if ( ! $template_path ) $template_path = $woocommerce->template_url;
$plugin_path = myplugin_plugin_path() . '/woocommerce/';
// Look within passed path within the theme - this is priority
$template = locate_template(
array(
$template_path . $template_name,
$template_name
)
);
// Modification: Get the template from this plugin, if it exists
if ( ! $template && file_exists( $plugin_path . $template_name ) )
$template = $plugin_path . $template_name;
// Use default template
if ( ! $template )
$template = $_template;
// Return what we found
return $template;
}
You have to override WooCommerce Template
woocommerce/templates/checkout/review-order.php and Put this file in your theme folder themes/yourthemename/woocommerce/checkout/review-order.php

Wordpress - Setting custom template for multiple custom post types

I was wondering if anyone knew how to set up a single template for multiple custom post types. For example - I don't want to set up multiple templates that do the exact same thing.
Code
I found the following snippet while searching and it doesn't seem to work. I have placed this in functions.php in the theme I am using.
add_filter( 'single_template', function( $template ) {
$cpt = [ 'available-properties', 'leased-sold', 'norway' ];
return in_array( get_queried_object()->post_type, $cpt, true )
? 'path/to/country-single.php'
: $template;
} );
Found the answer
This seems to work great!
add_filter( 'template_include', function( $template )
{
// your custom post types
$my_types = array( 'available-properties', 'leased-sold' );
$post_type = get_post_type();
if ( ! in_array( $post_type, $my_types ) )
return $template;
return get_stylesheet_directory() . '/page-content__projects-single.php';
});

Call main theme function with plugin

We created a plugin with a new template and we want to hook the main theme function and return the main plugin function.
We tried it with:
add_filter( "page_template", "test" );
function test( $template ) {
if( 'plugin_name.php' == basename( $template ) )
$template = WP_PLUGIN_DIR . '/plugin_folder/plugin_name.php';
return $template;
}
and changed page template in theme functions with main function of plugin which runs template inside plugin:
add_filter( "page_template", "main_plugin_function" );
Is page_template the right filter to change theme template?
Thanks for help!
I think you should use template_include filter, this filter hook is executed immediately before WordPress includes the predetermined template file. This can be used to override WordPress's default template behavior.
For example
add_filter( 'template_include', 'portfolio_page_template', 99 );
function portfolio_page_template( $template ) {
if ( is_page( 'portfolio' ) ) {
$new_template = locate_template( array( 'portfolio-page-template.php' ) );
if ( '' != $new_template ) {
return $new_template;
}
}
return $template;
}
https://codex.wordpress.org/Plugin_API/Filter_Reference/template_include

Wordpress Permalink Generating 404 Page

I'm working on changing the permalink of a Custom Post Type to include the taxonomy prior to the post id. I'm capable of it displaying the taxonomy in the URL however when I go to the page I get a 404 Error. It looks like the structure of the permalink is correct however the location of the post isn't synced up w/ the database location.
Any help is appreciated and thanks in advance!
Couple of notes:
My .htaccess file has mod rewrite on.
I've added %tax% to the permalink rewrite for the CPT
I have archive turned on for the CPT
Code
function change_permalink( $link, $post ) {
if ( ‘custom-post-type-name’ == get_post_type( $post ) ) {
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, ’taxonomy-name’);
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'no-taxonomy-listed’;
return str_replace('%tax%', $taxonomy_slug, $link);
}
return $link;
}
add_filter( 'post_type_link', ‘change_permalink’, 10, 2 );
Figured it out. Needed to wp_rewrite:
add_action( 'wp_loaded', 'urban_permastructure' );
function urban_permastructure($post) {
if ( 'custom-post-type-name' == get_post_type( $post ) ) {
global $wp_rewrite;
$structure = '/cat/%tax%';
$wp_rewrite->add_rewrite_tag("%tax%", '([^/]+)', "tax-type=");
$wp_rewrite->add_permastruct('tax-type', $structure, false);
}
}

Resources