Wordpress Custom Page Template based on URL Rewrite - wordpress

I've a problem with creating Custom Page Template based on URL Rewrite.
This is my rewrite rule:
add_rewrite_rule( '([^/]+)/var1', 'index.php?category_name=$matches[1]&var1=yes', 'top' );
And this is how I adding custom template to this url:
if ( get_query_var( 'var1' ) ) {
include( get_template_directory() . '/cat-var1.php' );}
It works, but when I scroll down the page I see content from parent page in this case it's current category.
So page show content from page var1 and after it content from parent page, how to delete content of parent page?
This is my full code:
function prefix_movie_rewrite_rule() {
add_rewrite_rule( '([^/]+)/var1', 'index.php?category_name=$matches[1]&var1=yes', 'top' );}
add_action( 'init', 'prefix_movie_rewrite_rule' );
function prefix_register_query_var( $vars ) {
$vars[] = 'var1';
return $vars;
}
add_filter( 'query_vars', 'prefix_register_query_var' );
function prefix_url_rewrite_templates() {
if ( get_query_var( 'var1' ) ) {
include( get_template_directory() . '/cat-var1.php' );
}
}
add_action( 'template_redirect', 'prefix_url_rewrite_templates' );
Regards

Related

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: body_class producing "home" class on custom url

The plugin I'm writng adds a custom url and handles it with a custom template using this:
public function url_for_calendar() {
add_rewrite_rule( 'calendario', 'index.php?upcoming-events=true', 'top' );
}
public function template_for_calendar( $path ) {
if ( get_query_var( 'upcoming-events' ) ) {
$template = get_template_directory() . '/upcoming-events.php';
return $template;
}
return $path;
}
public function upcoming_event_query_var( $vars ) {
$vars[] = 'upcoming-events';
return $vars;
}
add_action( 'init', array( $this, 'url_for_calendar' ) );
add_filter( 'template_include', array( $this, 'template_for_calendar' ) );
add_filter( 'query_vars', array( $this, 'upcoming_event_query_var' ) );
Things go as planned, but the problem I'm having is that on my header, where I have:
<body <?php body_class(); ?>>
body_class() ends up adding "home" to the list of classes. I read the docs for "is_home" and for "body_class", but I still don't understand why would it add the "home" class to this particular URL. Should I remove it (and add pertinent classes) with the body_class filter, or is there a better practice to accomplish this?
Using WP 4.1.
Actually, is_home() adds the class "blog".
The class "home" is added for the front page (is_front_page()).
If the settings are configured for a static front page and this code is run there, then you will get "home" as a body class.

How does one replace WooCommerce default placeholder image for shop categories

I've tried the following code, which im using successfully with product thumbnails but for categories haven't found a way to do it.
add_action( 'init', 'mw_replace_woocommerce_placeholders' );
function mw_replace_woocommerce_placeholders() {
add_filter('woocommerce_placeholder_img', 'custom_woocommerce_placeholder_img');
function custom_woocommerce_placeholder_img( $src ) {
$src = '<span class="thumb-placeholder"><i class="icon-camera"></i></span>';
return $src;
}
}
Any tips?
This should work
add_action( 'init', 'custom_placeholder' );
function custom_placeholder() {
if ( is_product_category() ){
add_filter('woocommerce_placeholder_img_src','custom_placeholder_img');
function custom_placeholder_img($src) {
$upload_dir = wp_upload_dir();
$uploads = untrailingslashit( $upload_dir['baseurl'] );
$src = $uploads . '/your/directory/custom_placeholder.jpg';
return $src;
}
}
}
Or you could replace the placeholder located at "wp-content/plugins/woocommerce/assets/images/placeholder.png" with your own.
The line:
if ( is_product_category() ){
in functions.php makes the theme reliant on the plugin, so if you deactivate woocommerce for testing, it breaks the site. At least that's what happened to me.

rewrite url with custom plugin with permalink

I have developed plugin and uses shortcode to display.
So, there is two type of items to display like
if get title in url, display single item and if not all items so i have added into if else in condition.
So for that title, i have passed
add_query_arg( array('title' =>sanitize_title($data->get_title())), get_permalink($post_id) );
now url is generated by this is domain.com/post-slug/?title=nameoftitle
but i want to url like domain.com/post-slug/nameoftitle
I have tried following code but not working
function update_rewrite_rules() {
add_rewrite_tag( '%title%', '([^/]*)' );
add_rewrite_rule(
$newVarRegex,
'index.php?pagename=$matches[1]&title=$matches[2]',
'top'
);
}
add_action( 'init', 'update_rewrite_rules' );
but its not working
I got solution of it. working code is:
function js_update_rewrite_rules() {
add_rewrite_tag( '%title%', '([^/]*)' );
$rewrite_rules = get_option( 'rewrite_rules' );
$newVarRegex = '^([^/]*)/title/([^/]*)/?';
// check if the rule exists
if ( !isset( $rewrite_rules[$newVarRegex] ) ) {
add_rewrite_rule(
$newVarRegex,
'index.php?pagename=$matches[1]&title=$matches[2]',
'top'
);
flush_rewrite_rules();
}
}
function js_on_activation() {
js_update_rewrite_rules();
}
register_activation_hook( __FILE__, 'js_on_activation' );
function js_on_init() {
js_update_rewrite_rules();
}
add_action( 'init', 'js_on_init' );

Woocommerce product ID in url

I'm looking for a way to put a Woocommerce product ID in a URL, e.g.:
domain.com/product-category/id/product-title
The ID is the product's post ID, e.g. "12092". Automatically created by WooCommerce in Wordpress.
Can this be done in a way? Either easily through the Wordpress permalinks settings or in a different way through hacking my way into the files.
This nearly works:
<?php
add_filter('post_type_link', 'wpse33551_post_type_link', 1, 3);
function wpse33551_post_type_link( $link, $post = 0 ){
if ( $post->post_type == 'product' ){
return home_url( 'p/' . $post->ID );
} else {
return $link;
}
}
add_action( 'init', 'wpse33551_rewrites_init' );
function wpse33551_rewrites_init(){
add_rewrite_rule(
'product/([0-9]+)?$',
'index.php?post_type=product&p=$matches[1]',
'top' );
}
?>
But the output is:
domain.com/p/45
I'd like it to be:
domain.com/p/45/title-of-the-post
Thanks!
Why so complicated? Go to settings -> permalinks and click "Shop based URL" and save. Now you can see that the permalink structure in the "Custom structure". It should looks like this now:
/shop/%product_cat%/
Now add the post_id to the permalink setting like this:
/shop/%product_cat%/%post_id%/
Save it and go ahead.
You can use $post->post_name to get the slug.
Use a more permissive regular expression to accept any url that ends with /p/[id][anything]
<?php
add_filter('post_type_link', 'wpse33551_post_type_link', 1, 3);
function wpse33551_post_type_link( $link, $post = 0 ){
if ( $post->post_type != 'product' )
return $link;
return home_url( 'p/' . $post->ID . '/' . $post->post_name );
}
add_action( 'init', 'wpse33551_rewrites_init' );
function wpse33551_rewrites_init(){
add_rewrite_rule(
'p/([0-9]+)?.*?$',
'index.php?post_type=product&p=$matches[1]',
'top'
);
}
?>
You might need to 'reset' your permalinks in the settings menu for WordPress to accept the new rule.

Resources