Wordpress add_rewrite_rule With 3 Variables - wordpress

I am having trouble getting Wordpress to assign the proper values to variables using the add_rewrite_rule() function. My goal is to have a url that looks like this: www.mywebsite.com/catalog/category1/category2/item-slug.
Here is my code from the functions.php file:
add_filter( 'query_vars', 'query_vars_new' );
function query_vars_new($query_vars){
$query_vars[]='category1';
$query_vars[]='category2';
$query_vars[]='catalog_item';
return $query_vars;
}
add_action( 'init', 'rewrite_init' );
function rewrite_init(){
add_rewrite_rule('catalog(/([^/]+))?(/([^/]+))?/?','index.php?page_id=94&category1=$matches[1]&category2=$matches[2]&catalog_item=$matches[3]','top');
}
So the goal is to have three variables $category1, $category2, and $catalog_item with the values from the corresponding url segments. However, when I test this the first two variables are set to the same value.
For example, www.mywebsite.com/catalog/clothes/shirts/polo-shirt should return:
$category1="clothes";
$category2="shirts";
$catalog_item="polo-shirt";
But instead I get:
$category1="/clothes";
$category2="clothes";
$catalog_item="/polo-shirt";
There must be something wrong with my add_rewrite_rule function, but I can't figure it out.

Thanks to the incredibly underwhelming response to this question I ended up just making it work without the help of the Wordpress variables. I just configured Wordpress to allow the variables in the url, then used PHP to fetch the URL and get the different URL segments and analyze them. Not quite as pretty as the Wordpress method, but at least it works.

Related

How can I exploit the wordpress template hierarchy to render a different post template depending on category name/slug?

Apologies if this appears simple to some, but I have scaled high and low and I'm not finding a solution here to my problem, which is:
I have a website set up with Wordpress in which posts can fall under one of three categories: reviews, views, news - the slugs associated with each of these category names are the same.
Currently, calling up the web page of any individual post classified under any of these categories will see it rendered by the file single.php.
However, I want to make a slight adaption to the rendering of the post when it falls within the 'reviews' category. I have copied and renamed the original single.php file to single-post-reviews.php (no custom posts here, I will just confirm and I would like, if possible, to avoid child-theming here - not good practice, I know), but I am not seeing the new rendering from my new file.
I've also tried renaming to single-reviews.php which hasn't worked either - so could someone tell me what exactly I'm missing here?
Thanks,
WordPress Template Hierarchy for Single Posts doesn't factor in the current post category (likely due to the fact you can have multiple categories). Due to this, you have 2 viable options to your problem.
1) You can modify single.php to check for the post category, and if it's categorized under reviews, do something. This makes sense if you're just adding a small amount of markup in one or two places, or even hiding a few lines conditionally.
2) You can override the page template that's loaded based on the post's category using the single_template filter. Because I don't know exactly what you're doing, I'm going to elaborate more on this method. Take the following function:
add_filter( 'single_template', 'so51913799_review_template' );
function so51913799_review_template( $single_template ){
global $post;
if( has_category( 'reviews' ) ){
$single_template = get_stylesheet_directory() . '/single-post-reviews.php';
}
return $single_template;
}
If you put it in your functions.php file, it will make use of the has_category() function (I prefer this to in_category() since in_category just returns has_category anyways) and if it matches, it will update the $single_template variable to single-post-reviews.php. This assumes that the file is inside your /wp-content/themes/ACTIVE-THEME/ directory.

Replacing a core WordPress function

I am trying to replace the core WP function wp_delete attachment. In my functions.php file I am adding the following code:
add_action( 'init', 'remove_my_action' );
function remove_my_action(){
remove_action( 'delete_attachment', 'wp_delete_attachment' );
add_filter('delete_attachment','wp_delete_attachment',10,2);
}
And then the copy of the replaced function goes here with edited code.
This does not work. I get the error: Cannot redeclare wp_delete_attachment(). I've tried a number of other methods, but can't get it to work.
The bottom line is that I need to add some code to the middle of wp_delete_attachment function. If I can replace that function with my version somehow or add the code to the existing function without editing the actual code in the wp-includes/post.php file (so that version updates don't override my code), I would be satisfied with either. How can this be done? All the options I found through questions have not solved the issue. Thanks!!
You'll need to name your copy of wp_delete_attachment to a unique name. Perhaps namespace it with your site name like function my_site_wp_delete_attachment().
Also, I believe you'll need to use add_action instead of add_filter.
remove_action( 'delete_attachment', 'wp_delete_attachment' );
add_action( 'delete_attachment', 'my_site_wp_delete_attachment');

add_action not working in wordpress

I am setting a WP that it should redirect all the request made to sub-directory to file inside that directory.
For example if there a visit on example.com/link/me then it should send the user to /link/index.php?me
To achieve this, I am trying to add rewrite rule in theme function file.
Here's what my code looks like:
function moin_add_rewrite_rules() {
add_rewrite_rule(
'^([^/]*)/(link)/(?:[a-z][a-z0-9_]*)?$',
'/link/index.php?$matches[1]',
'top'
);
}
add_action( 'init', 'moin_add_rewrite_rules' );
It is not working, visiting example.com/link/meshows a 404 (as link is outside WP).
Is there any problem with regex code? or anywhere else? What could be other possible solution?
Looking at the example in the Codex, the matched expression doesn't have the leading /.
Stripping the leading bit out of your regex, then, I think this should work (sorry, I don't have a test environment handy):
add_rewrite_rule('^link/([a-z][a-z0-9_]*)/?$','/link/index.php?$matches[1]','top');
Alternatively, you could do something similar in your .htaccess file.
As a side note, I think as it's currently coded, you should be using $matches[3] not $matches[1], as the value you're interested in is the third parameter in parentheses (the Codex notes "capture group data starts at 1, not 0").

What's "function_exists" in Wordpress

Im very new to WordPress. I was going through Smooth Slider WP Plugin and saw
if ( function_exists( 'get_smooth_slider_category' ) ) { get_smooth_slider_category('Uncategorized'); }
This pretty much gives what I wanted, but not quite. This pulls all the content in the category and what Im after is just the image URL.
My question is whats "function_exists" in wordpress? and I checked get_smooth_slider_category in functions.php file but couldnt find any. Can someone please explain how function_exists works?
function_exists is a PHP function, not limited to WordPress.
From the manual "Checks the list of defined functions, both built-in (internal) and user-defined, for function_name."
It returns true or false on whether or not the function exists. So you can either create a new function before it that does something slightly different, or prevent an error if it doesn't exist (normally because the required file hasn't been included).
This is a PHP function that checks if the passed in name matches any defined functions (either internal, or user defined).
It is a way to check if a function is "available" before calling it.

Wordpress URL rewrite not working/registering

I've been trying to get my head around Wordpress URL rewrites, but I'm having no luck.
What I want to do:
I building a custom plugin where a user can build products from various options. The options collectively build a code which refers to the unique product the customer has built.
The code might be something like 140-3-WPA-ABC-2.
The plugin will appear on a single dedicated page:
http://wordpress-site/configurator/
I want a customer with a prexisiting code to be able to enter it into the url like this:
http://wordpress-site/configurator/140-3-WPA-ABC-2/
Whereupon, the plugin gets the variable, and uses it to build the correct product.
Problem
It should be fairly simply but I can't get anything to work using the Wordpress URL rewriting rules, I can't even get anything to seemingly get registered as a Wordpress query var.
I've been trying the following in the main plugin initialisation code:
add_filter( 'query_vars', 'conf_query_vars' );
add_action( 'init', 'cong_rewrites' );
function conf_query_vars($query_vars){
$query_vars[] = 'product_code';
return $query_vars;
}
function conf_rewrites(){
add_rewrite_rule(
'configurator/([^/]+)/?$',
'index.php?product_code=$matches[1]',
'top'
);
}
If I then try and open http://wordpress-site/configurator/140-3-WPA-ABC-2/ I get a page not found error. Echoing query_vars seems to show the variable 'product_code' is not created.
ps I've tried flushing the rewrite cache. Apologies for cross-posting to Wordpress.stackexchange.com - but seems programming question better here?
Try this, I had the same problem.
add_action('init', function() {
add_rewrite_endpoint('sponsor', EP_ALL);
});
add_filter('request', function($args) {
print_r($args);
return $args;
});
I think you should just integrate the function add_rewrite_endpoint.

Resources