I'm trying to give a pretty url to my custom CPT (Orders). This is the way im trying:
add_filter('post_type_link', 'custom_post_type_link', 1, 3);
function custom_post_type_link( $link, $post = 0 ){
if ( $post->post_type == 'orders' ){
$order_code = 1121000+$post->ID;
return home_url( 'orders/' . $order_code);
} else {
return $link;
}
}
Assume that post ID is 32, This code creates the following url:
http://www.example.com/orders/1121032
Now i wanna write a code to display the post with post ID=32 in above url.
I heard somewhere that i should use custom query vars. But i don't know how to use it.
This is the rest of code i've written
add_filter('query_vars', 'custom_add_query_vars');
function custom_add_query_vars($qVars){
$qVars[] = "code";
return $qVars;
}
add_action( 'init', 'custom_rewrites_init' );
function custom_rewrites_init(){
add_rewrite_rule(
'orders/([0-9]+)?$',
'index.php?post_type=orders&code=$matches[1]',
'top' );
}
After adding custom rewrite rules, wordpress needs to be told to activate the new rule.
Use this method after the add_rewrite_rule function call.
$wp_rewrite->flush_rules();
Warning: flush_rules is expensive, you definitely don't want to call it on every request. Typically you would put the custom_rewrites_init and flush_rules in a plugin register_activation_hook function.
If you're lazy, you can just add it to your code once, make a request to the website (which will rewrite the .htaccess rewrite rules), then comment the flush_rules method out.
Related
I'm adding rewrite rules to my PHP script which is included in a WordPress page with the permalink kb
So I can visit domain.com/kb and the page is displayed.
function wdm_add_rewrite_rules() {
add_rewrite_rule( '^kb/([^/]+)/?$', 'kb?kb_cat=$matches[1]&kb_seq=1', 'top');
}
add_action('init', 'wdm_add_rewrite_rules');
But when I visit the page with additional strings in the url, I get a 404.
So when I visit domain.com/kb is shows the correct page, and then visiting domain.com/kb/84/92, it shows a 404
I just need to be able to read the additional url params in my PHP script, such as $_GET["kb_cat"]
function wdm_add_rewrite_rules() {
add_rewrite_rule( '^kb$', 'index.php?kb_cat=$matches[1]&kb_seq=1', 'top');
}
add_action('init', 'wdm_add_rewrite_rules');
to take it a step further and use the parameters:
function add_query_vars_filter( $vars ){
$vars[] = "kb_cat";
$vars[] = "kb_seq";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
and then load a custom template file:
function include_custom_template($template){
if(get_query_var('kb_cat') && get_query_var('kb_seq')){
$template = get_template_directory() ."/my-custom-template.php";
}
return $template;
}
add_filter('template_include', 'include_custom_template');
Once adding to your functions.php go to Settings > Permalinks and hit 'save changes' to reset the flush rules
try this:
function wdm_add_rewrite_rules() {
add_rewrite_rule( '^kb\/([^\/]+)\/?([^\/]+)$', 'kb?kb_cat=$matches[1]&kb_seq=1', 'top');
}
add_action('init', 'wdm_add_rewrite_rules');
You can check the regular expression on https://regex101.com/ or any other websites like this online for the matches
I am working on a way to disable a specific plugin on a certain product page. I've cobbled this together from things I found online and the plugins code itself but its not working. Curious to have some fresh eyes have a look and let me know what might be failing. The post id of the product is 2679320. The actions I have set to remove are the ones referenced in the plugin wp_enqueue_scripts. Here is the code I'm trying by loading to snippets:
function remove__construct() {
global $post;
$ids = array(2679320);
if(in_array($post->ID,$ids)):
remove_action(‘wp_enqueue_scripts’,array($this,’enqueue_scripts’));
remove_action(‘plugins_loaded’,array($this,’load_txt_domain’),99);
remove_action(‘wp_footer’,array($this,’get_popup_markup’));
remove_filter( ‘pre_option_woocommerce_cart_redirect_after_add’, array($this,’prevent_cart_redirect’),10,1);
endif;
}
add_action(‘wp_head’, ‘remove__construct’, 1);
Any ideas why this isn't working? What did I miss? Anyone have better way to do this?
You can use Plugin Organizer. It allows you to selectively disable a plugin on a page or a complete post type.
There are 2 ways to disable plugin.
The first way is to create a custom plugin that removes the action that used to initialize your target plugin. The second way is to remove actions and filters which add scripts, styles and makes changes on a page.
Either way you choose, you have to remove actions after they've been added and before they actually worked. That means that for the first way in most cases you have to use plugins_loaded hook which can't be used in your functions.php (the first hook which can be used in functions.php is load_textdomain hook). in case you want to disable the plugin on certain pages you have to somehow get the current post ID, which isn't so easy because global $post variable is not available yet (The earliest hook with $post is wp).
Parameters for your remove_action depend on plugin add_action. The main point here is that all parameters of your remove_action must be the same as add_action parameters. Here are some examples :
add_action('plugins_loaded', 'init_function_name');
remove_action('plugins_loaded', 'init_function_name');
add_action('plugins_loaded', 'init_function_name', 100);
remove_action('plugins_loaded', 'init_function_name', 100);
class Plugin_Classname {
public static function init() {
add_action( 'plugins_loaded', array( __CLASS__, 'on_init' ) );
}
}
remove_action( 'plugins_loaded', array( 'Plugin_Classname', 'on_init' ) );
class Plugin_Classname {
public function __construct(){
add_action('plugins_loaded', array($this, 'init'), 99);
}
public static function get_instance(){
if(self::$instance === null){
self::$instance = new self();
}
return self::$instance;
}
}
remove_action('plugins_loaded', array( Plugin_Classname::get_instance() , 'init'), 99);
Let's begin with the easiest way. Assume that removing scripts and styles is enough. Then you have to find wp_enqueue_scripts hooks in the plugin source. Eq.:
class Xoo_CP_Public{
protected static $instance = null;
public function __construct(){
add_action('plugins_loaded',array($this,'load_txt_domain'),99);
add_action('wp_enqueue_scripts',array($this,'enqueue_scripts'));
add_action('wp_footer',array($this,'get_popup_markup'));
add_filter( 'pre_option_woocommerce_cart_redirect_after_add', array($this,'prevent_cart_redirect'),10,1);
}
public static function get_instance(){
if(self::$instance === null){
self::$instance = new self();
}
return self::$instance;
}
}
As we need global $post variable we gonna use wp hook. Place the code below in functions.php:
function disable_plugin() {
global $post;
$ids = array( 2679320 ); // Disable plugin at page with ID = 2679320
if( in_array( $post->ID ,$ids ) ) {
remove_action('wp_enqueue_scripts',array( Xoo_CP_Public::get_instance(),'enqueue_scripts'));
remove_action('plugins_loaded',array(Xoo_CP_Public::get_instance(),'load_txt_domain'),99);
remove_action('wp_footer',array(Xoo_CP_Public::get_instance(),'get_popup_markup'));
remove_filter( 'pre_option_woocommerce_cart_redirect_after_add', array(Xoo_CP_Public::get_instance(),'prevent_cart_redirect'),10,1);
}
}
add_action( 'wp', 'disable_plugin' );
What if we want to remove an action is used to initialize this plugin? Let's take a look at add_action:
add_action('plugins_loaded','xoo_cp_rock_the_world');
In this case we can't use plugins_loaded hook because add_action is being called without priority parameter. If it's being called with priority parameter we could just create disable-plugin.php file in /wp-content/plugins folder and place this code there:
function disable_plugin() {
remove_action('plugins_loaded', 'xoo_cp_rock_the_world', 100);
}
add_action('plugins_loaded','disable_plugin');
But it's useless in this case without priority parameter. Yet we can cheat! We don't have to use any hooks and call remove_action directly. We should call it after target plugin add_action was called. Plugins are loaded in alphabetical order so if we named our plugin 'zzz-disable-plugin.php` with this lines of code:
/* Plugin Name: zzz-disable-plugin */
remove_action('plugins_loaded', 'xoo_cp_rock_the_world');
The target plugin will be disabled. At all pages though. I haven't find a way to get ID of current page on such an early hook. But we can use URI :
/* Plugin Name: zzz-disable-plugin */
if( 'product/polo' == trim( $_SERVER[ 'REQUEST_URI' ], '/' ) ) {
remove_action('plugins_loaded', 'xoo_cp_rock_the_world');
}
I have an existing buddy press user profile link: e.g. https://example.com/members/joseph-bada
I need to make https://example.com/members/joseph-b an exact duplicate of it.
this my code simplified version:
add_filter('wp', 'custom_rewrite_rule');
function custom_rewrite_rule() {
global $wp_query, $wp_rewrite;
$slug = $wp_query->query_vars['name'];
if($slug==='joseph-bada') {
add_rewrite_rule('^members/joseph-b/?', 'members/joseph-bada', 'top');
$wp_rewrite->flush_rules();
}
}
but if i browse https://example.com/members/joseph-b - i get 404 error
UPDATE:
even after adding this in functions.php
add_action('init', 'custom_test');
function custom_test() {
global $wp_rewrite;
add_rewrite_rule('^members/joseph-b', 'members/joseph-bada', 'top');
$wp_rewrite->flush_rules();
}
https://example.com/members/joseph-b is still 404
can someone please point out what im missing?
UPDATE: i learned that https://example.com/index.php?bbp_user=joseph-bada&edit=1
leads to https://example.com/members/joseph-bada
so now i tried this:
add_action('init', 'custom_test');
function custom_test() {
global $wp_rewrite;
add_rewrite_rule('^members/joseph-b', 'https://example.com/index.php?bbp_user=joseph-bada&edit=1', 'top');
$wp_rewrite->flush_rules();
}
still no avail 404 though..
Disclaimer: I'm not a WP developer, but here goes.
It looks as though the $query part of the add_rewrite_rule needs to be explicit in its setting (as shown in the below example with the index.php string).
function custom_rewrite_rule() {
add_rewrite_rule('^nutrition/?([^/]*)/?','index.php?page_id=12&food=$matches[1]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
I also notice that you are doing an add_filter as opposed to add_action.
i gave up using add_rewrite_rule wordpress functionality.. instead, i studied buddypress plugin and how it works.. i discovered the following filters:
function custom_bp_domain_filter($domain, $user_id) {
$change_slug = is_joseph_bada_user_id($user_id);
if ($change_slug) {
$domain = trailingslashit(bp_get_root_domain() . '/' . 'members/joseph-b');
}
return $domain;
}
add_filter('bp_core_get_user_domain', 'custom_bp_domain_filter', 10, 2);
this part above is a filter wherein im gonna check if the user id being displayed belongs to members/joseph-bada. if it is, change it to members/joseph-b
function custom_bp_after_slug_filter($after_member_slug) {
if ($after_member_slug==='joseph-b') {
$after_member_slug = 'joseph-bada';
}
return $after_member_slug;
}
add_filter('bp_core_set_uri_globals_member_slug', 'custom_bp_after_slug_filter');
this above part determines if the URI is now members/joseph-b.. we dont have a user with that, instead it is 'joseph-bada'
the returned value here is the user to be displayed so we need to return 'joseph-bada' if the param value is 'joseph-b'
when members/joseph-b is browsed.. it will display the profile of members/joseph-bada
I'm trying to send a variable in the url. As I have the permalinks activated, I'm trying to add a rewrite rule, as said on a post I've read here...
Well nothing is working. This is the code I have on functions.php
add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('query_vars','wp_insertMyRewriteQueryVars');
add_filter('init','flushRules');
// Remember to flush_rules() when adding rules
function flushRules(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
$newrules = array();
$newrules['view-article/(.+)'] = 'index.php?pagename=view-article&aid=$matches[1]';
$finalrules = $newrules + $rules;
return $finalrules;
}
// Adding the var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars)
{
array_push($vars, 'aid');
return $vars;
}
//Stop wordpress from redirecting
remove_filter('template_redirect', 'redirect_canonical');
and this is the call to get the variable.
$aid = urldecode($wp_query->query_vars['aid']);
What am I doing wrong? :(
My wordpress version is 4.0 (The latest)
Try the following code:
add_action('init', 'flush_urls');
function flush_urls()
{
// Execute flush_rules() on every request can
// slow down your entire website
if('clean' !== get_option('flush_urls', ''))
{
flush_rewrite_rules();
add_option('flush_urls', 'clean');
}
}
add_action('init', 'register_rewrite_rules', 0);
function register_rewrite_rules()
{
add_rewrite_tag('%aid%', '([^/]+)');
add_rewrite_rule(
"view-article/([^/]+)/?",
'index.php?pagename=view-article&aid=$matches[1]',
"top"
);
}
Finally in your template-redirect or an any later action/hook you should do the following:
global $wp_query;
echo $wp_query->query_vars['pagename'];
echo $wp_query->query_vars['aid'];
In case you cannot see these variables inside the $wp_query, then try to save again the permalinks in your WordPress Dashboard. This will clean out the urls cache.
I need to pass a URL variable to my category.php file.
Currently my category page is at http://example.com/category-slug/
I am using the SEO plugin to rewrite http://example.com/category/category-slug and remove the /category/ part.
Also, the settings formy permalinks are set to this option in the settings menu: http://example.com/sample-post/
Now I need to be able to pass a variable in the URL like:
http://example.com/category-slug/?type=VALUE
or
http://example.com/category-slug/VALUE
where "type" is the name of the variable and VALUE is its value
I have tried using this piece of code in my functions.php file:
<?php
add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
$qvars[] = 'type';
return $qvars;
}
global $wp_query;
if (isset($wp_query->query_vars['type']))
{
print $wp_query->query_vars['type'];
}
?>
However, when I try to open http://example.com/category-slug/?type=something or http://example.com/category-slug/something I get "nothing found" and "Page not found" pages.
While I see this has been discussed over and over, none of the solutions seem to work for my case.
How do I properly pass a variable to a category page?
First of all, you code will never reach the if statement, as you return from the function before.
I also don't know which SEO tool you are using, but there is one function that goes with the "query_vars" filter: add_rewrite_rule()
I would recommend to write a little plugin which does the rewriting of the category permalink. Something like this (untested, but similar to a plugin I use):
// Flush added rewrite rules on activation
function category_permalink_rewrite_activate() {
category_permalink_rewrite_set_rewrite_rules();
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'category_permalink_rewrite_activate' );
// Remove rewrite rule for event archives
function category_permalink_rewrite_deactivate() {
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'category_permalink_rewrite_deactivate' );
// Add rewrite rule for category permalink on init
add_rewrite_rule( '^category-(.*)/(.*)', 'index.php?category_name=$matches[1]&type=$matches[2]', 'top' );
kaufunction category_permalink_rewrite_set_rewrite_rules() {
}
add_filter( 'init', 'category_permalink_rewrite_set_rewrite_rules' );
// Register the custom query var so WP recognizes it
function category_permalink_rewrite_add_query_vars( $vars ) {
$vars[] = 'type';
return $vars;
}
add_filter( 'query_vars', 'category_permalink_rewrite_add_query_vars' );