Custom permalink load template - php

I created functions below to customize the permalink of a specific category. Everything is working right!
When accessing the post with this permalink example.com.br/negocios/name-post everything is ok.
But if I access these same posts through example.com.br/name-post it also loads, being that it was for 404 or redirected to the created permalink.
add_filter( 'pre_post_link', 'idinheiro_custom_permalink', 10, 3 );
function idinheiro_custom_permalink( $permalink, $post ){
$category = get_the_category($post->ID);
$slug = $category[0]->slug;
$tag = '%postname%';
if ( !empty($category) && $slug === 'negocios' ) {
$permalink = "/$slug/$tag";
}
return $permalink;
}
add_filter( 'generate_rewrite_rules', 'idinheiro_custom_rewrite_rules' );
function idinheiro_custom_rewrite_rules( $wp_rewrite ) {
$new_rules = [
'^negocios/([^/]+)/?$' => 'index.php?name=$matches[1]',
];
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite->rules;
}

Related

Remove cpt slug and add custom taxonomy in url

I need to change the custom post type url, this code works it but is also affecting other cpts causing 404 in all other cpt, how can I make it only for the one I need.
Here is my code
// Rewrite urls of resources and put category on url
function change_custom_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = get_the_terms( $post->ID, 'resource_type' );
if(get_post_type($post->ID) == 'resource' ) {
if( $terms ){
return home_url( "/".$terms[0]->slug."/".$post->post_name );
}else{
return home_url( "/resource/".$post->post_name );
}
}
}
return $post_link;
}
add_filter( 'post_type_link', 'change_custom_post_link', 1, 3 );
and this is the rewrite
function resource_rewrite_rules() {
add_rewrite_rule(
'^(.*)/(.*)/?$',
'index.php?post_type=resource&name=$matches[2]',
'top'
);
}
add_action( 'init', 'resource_rewrite_rules' );
Why are you doing this? That's too tricky.
You may use this plugin to create and modify CPT: CPT plugin WordPress
Or refer to docs: https://developer.wordpress.org/reference/functions/register_post_type/

How to add multiple categories in function WordPress

Wordpress - functions.php
I'm using this function to automatically add custom structure in url for posts in a specific categorie.
But now I would like to know how I could add multiple categories without having to copy this function over and over.
Other solution could be to have this function work for the parent post category and all child categories.
// url structure
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
// Get the category for the post
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->cat_name == "b" ) {
$cat_name = strtolower($category[0]->cat_name);
$permalink = trailingslashit( home_url('/questions/' . $post->post_name .'/' ) );
}
return $permalink;
}
add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
add_rewrite_rule(
'b/([^/]+)(?:/([0-9]+))?/?$',
'index.php?category_name=b&name=$matches[1]&page=$matches[2]',
'top' // The rule position; either 'top' or 'bottom' (default).
);
}

Wordpress: Include new template for a page using query var with rewrite endpoint

I am trying to load a new page template when a query var is appended at the end of my page url:
Original url: example.com/testpage/
with variable added to the end: example.com/testpage/amp
Then it would load up a custom php template.
This seems like a straight forward operation, but I cannot get it to work.
The url loads with the /amp variable at the end, but the template does not load. If I remove the condition "get_query_var('amp')" then it loads up the template no problem. What am I missing? Thanks :)
Here is my working code:
add_filter( 'query_vars', 'register_query_var' );
function register_query_var( $vars ) {
$vars[] = 'amp';
return $vars;
}
add_rewrite_endpoint( 'amp', EP_PAGES );
add_filter( 'template_include', 'use_amp_template', 99 );
function use_amp_template( $template ) {
global $wp_query;
if ( get_query_var( 'amp' ) && is_page() ) {
$new_template = locate_template( array( 'amptemplate.php' ) );
if ( '' != $new_template ) {
return $new_template;
}
}
return $template;
}
Found a good fix on my own. Here is the code if it would help anyone.
Adding 'amp' after a page or post will load up different templates for amp versions of the page.
example.com/samplepage/amp
or
example.com/samplepost/amp
add_filter( 'query_vars', 'register_query_var' );
function register_query_var( $vars ) {
$vars[] = 'amp';
return $vars;
}
add_rewrite_endpoint( 'amp', EP_PAGES | EP_PERMALINK );
add_filter( 'template_include', 'use_amp_template', 99 );
function use_amp_template( $template ) {
global $wp_query;
if(isset( $wp_query->query['amp'] ) && is_page()){
$new_template = locate_template( array( 'amppagetemplate.php' ) );
if ( '' != $new_template ) {
return $new_template;
}
}
if(isset( $wp_query->query['amp'] ) && is_single()){
$new_template = locate_template( array( 'ampposttemplate.php' ) );
if ( '' != $new_template ) {
return $new_template;
}
}
return $template;
}

Replace custom content type slug in Wordpress

In WordPress, I'm using Jetpack's portfolio custom content type, but would like to change the slug from "portfolio" to "examples". I found this example on how to do it (http://www.markwarddesign.com/2014/02/remove-custom-post-type-slug-permalink/) and this plugin (https://github.com/devinsays/no-slug-portfolio-post-types). Both as based on a post on Wordpress VIP that was linked to by the Jetpack team and now has a dead link.
Here is my code, for some reason it is not working. I have refreshed my permalinks by going to Settings > Permalinks and hitting save changes.
/**
* Remove the slug from custom post type permalinks.
*/
function vipx_remove_cpt_slug( $post_link, $post, $leavename ) {
if ( ! in_array( $post->post_type, array( 'portfolio' ) ) || 'publish' != $post->post_status )
return $post_link;
$post_link = str_replace( '/' . $post->post_type . '/', '/examples', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'vipx_remove_cpt_slug', 10, 3 );
/**
* Some hackery to have WordPress match postname to any of our public post types
* All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
* Typically core only accounts for posts and pages where the slug is /post-name/
*/
function vipx_parse_request_tricksy( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query )
|| ! isset( $query->query['page'] ) )
return;
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) )
$query->set( 'post_type', array( 'post', 'portfolio', 'page' ) );
}
add_action( 'pre_get_posts', 'vipx_parse_request_tricksy' );
Any ideas how to get this code working again?
function na_remove_slug($post_link, $post, $leavename) {
if ('POST TYPE SLUG' != $post - > post_type || 'publish' != $post - > post_status) {
return $post_link;
}
$post_link = str_replace('/'.$post - > post_type.
'/', '/', $post_link);
return $post_link;
}
add_filter('post_type_link', 'na_remove_slug', 10, 3);
function na_parse_request($query) {
if (!$query - > is_main_query() || 2 != count($query - > query) || !isset($query - > query['page'])) {
return;
}
if (!empty($query - > query['name'])) {
$query - > set('post_type', array('post', 'POST TYPE SLUG', 'page'));
}
}
add_action('pre_get_posts', 'na_parse_request');

WordPress - Ovveride plugin template in theme

I have plugin that create new post type. Also plugin set single template for it's single page.
add_filter( 'single_template', array( &$this, 'register_ipa_product_post_type_single_template' ) );
function register_ipa_product_post_type_single_template( $single_template ) {
global $post;
if ( $post->post_type == 'product' ) {
$single_template = IPA_PRODUCT_POST_TYPE_TEMPLATE_FOLDER . 'single-product.php';
}
return $single_template;
}
How i can override single-product.php in my theme.
I don't found any solutions for my question at this site.
just filter it a little later than the current function (ps if doing this within a class you need to reference it using array(&$this, 'function'). I left it out as i assume you are using the functions.php or function override....etc
add_filter( 'single_template', 'register_ipa_product_post_type_single_template', 100 );
function change_temp( $single_template ) {
global $post;
if ( $post->post_type == 'product' ) {
$single_template = 'path to your template file';
}
return $single_template;
};

Categories