Wordpress rewrite with custom parameter doesn't work - php

I'm currently developing a plugin which takes a parameter from URL (example: example.com/product_id/1234 is converted to example.com) and passes it in WP_Query (supposedly this should be used instead of GET or POST) for later use. My code so far looks like this:
function shortcode_callback() {
echo get_query_var('product_id','not found<br>');
}
function prepare_rewrite() {
add_filter('query_vars','rewrite_add_var');
add_rewrite_rule('^product_id/([^/]*)/?$','index.php?page_id=18&product_id=$matches[1]','top');
flush_rewrite_rules();
}
function rewrite_add_var($q_vars) {
$q_vars[] = 'product_id';
return $q_vars;
}
add_action('init','prepare_rewrite');
add_shortcode('shortcode', 'shortcode_callback');
Unfortuantely, even though redirect works, the custom parameter does not exist. I've been trying to put this piece of code in functions.php file, but to no avail. I've been also trying to solve this issue by using add_rewrite_tag, but as far as I can tell it's only useful when it comes to post selection.

Related

WP v-5.5 $_GET parameter does not work in my plugin

I have developed a wordpress plugin. In my plugin I manage all pages by get parameters like (http://example.com/client-portal/?page=dashboard) and it was working till wordpress version 5.4
But new version of wordpress version 5.5 in automatically redirect http://example.com/client-portal/?page=dashboard to http://example.com/client-portal/. Get parameter vanished automatically.
I have added shortcode by this way -
//page short code for user page
add_shortcode( 'ccgclient_portal', array($this,'ccgclient_portal_shortcode_func') );
This is my shortcode function -
function ccgclient_portal_shortcode_func()
{
ob_start();
include_once 'pages/user/index.php';
return ob_get_clean();
}
And catch get parameters by -
if(isset($_GET['page']) && $_GET['page'] == 'dashboard'){
include_once 'dashboard.php';
}
I don't know what's wrong with the new version of wordpress (5.5).
Please can you help me ?
Thanks in advance.
I believe your issue is with the 'page' key, this is a post type slug and it's creating a conflict with WP in this version. This is the same as configuring the permalink to work with '?post=98979' or a similar format.
My suggestion is to try and use a different get key and see what happens.
Let me know what you get.
I have the same issue with my plugin.
My problem was not using a new key. My client defined "page" here. It is about all the old links around in the world.
Im my case I solved it like this:
add_action( 'parse_request', 'ai_parse_request', 1);
and in
function ai_parse_request( $query ) {
unset( $query->query_vars['page']);
return $query;
}
I remove the "page" parameter from the $query to avoid the 301 redirect.
I have made this "workaround" configurable as the page parameter is used actually for pagination in the blog. In you case you should only apply this if e.g. the parameter is not a number to make sure you don't break pagination globally!

Multiple pages with same URL in WordPress

I have a WordPress site that has a standard Page called Places with URL
example.com/places/
And I have several child Pages called by cities
example.com/places/city-1
example.com/places/city-2
Now, I have a custom post type Place that should indicate a single place and should have permalink like
example.com/places/place-1
But then if I go to one of the previous links with city-1, city-2 I get 404 obviously because there is no place with that permalink.
Is there a way for WordPress to drop to previous permalink. So if there is no place with that name, look for a page with it.
You could probably use the the REFERER-value from the PHP server variable $_SERVER, but it´s not very reliable and can be altered.
I am using the plugin "Permalink Finder" in one of the pages I am maintaining and that works quite well for finding changed URL. You could give it a try and see if it works for you, too.
In case somebody ever having a similar problem, it can be done by using verbose page rules. This is an example I found at WordPress Stack Exchange
https://wordpress.stackexchange.com/questions/22438/how-to-make-pages-slug-have-priority-over-any-other-taxonomies-like-custom-post
add_action( 'init', 'wpse16902_init' );
function wpse16902_init() {
$GLOBALS['wp_rewrite']->use_verbose_page_rules = true;
}
add_filter( 'page_rewrite_rules', 'wpse16902_collect_page_rewrite_rules' );
function wpse16902_collect_page_rewrite_rules( $page_rewrite_rules )
{
$GLOBALS['wpse16902_page_rewrite_rules'] = $page_rewrite_rules;
return array();
}
add_filter( 'rewrite_rules_array', 'wspe16902_prepend_page_rewrite_rules' );
function wspe16902_prepend_page_rewrite_rules( $rewrite_rules )
{
return $GLOBALS['wpse16902_page_rewrite_rules'] + $rewrite_rules;
}

Passing a php variable into a gravity forms hook

I think I'm getting really confused with return'ing and echo'ing variables.
I've got this gravity forms hook from their support...
add_filter('gform_field_value_facebook_name', 'my_custom_population_function');
function my_custom_population_function($value){
return 'boom!';
}
This works and returns 'boom!' as my form field default variable.
This is pretty straight forward for a general text string, but I am trying to return a PHP variable instead.
I am loading the facebook PHP SDK in my functions.php at a higher scope than the gravity form hook. The facebook SDK definitely works, for example I am currently echoing this in my wordpress theme files...
echo $userData['name']
But my question is, why does it not work if I try and return the above variable inside the gravity for hook?
Please see what I have tried below, but it returns nothing...
add_filter('gform_field_value_facebook_name', 'my_custom_population_function');
function my_custom_population_function($value){
return $userData['name'];
}
I've also tried something similar in my wordpress functions.php, when trying to echo a variable in a filter...
$fb_app_id = '12345678910';
// APP ID FILTER
add_action( 'fb_app_id', 'echo_fb_app_id' );
function echo_fb_app_id() {
echo $fb_app_id;
}
But this returns nothing and the scope is the same.
Can anyone please enlighten me to why I can't pass these variables around. I think thats the technical term. Thank you very much.
This is because in PHP, functions don't read global variables without the global keyword.
$fb_app_id = '12345678910';
// APP ID FILTER
add_action( 'fb_app_id', 'echo_fb_app_id' );
function echo_fb_app_id() {
global $fb_app_id; // tells PHP to use the global variable
echo $fb_app_id;
}
Try to add global $userData; to your my_custom_population_function.

Wordpress url rewriting

I have a problem concerning url-rewriting in wordpress. I am currently working on a language plugin (nearly finished also) and as a last thing, I would like to see every url altered so that it contains the current language that has been selected by the user (or a default language if the user hasn't changed the language).
I don't have a problem altering the links, the problem lies with the rewriting done by the server. Below you can find how I change the links.
public function register_filters()
{
add_filter('page_link', array(get_class(),'alter_permalink'));
add_filter('post_link', array(get_class(),'alter_permalink'));
}
public function alter_permalink($permalink)
{
$permalink = str_replace(get_option('home'), '', $permalink);
$permalink = trim($permalink, '/');
//The next line is actually a method that is being called,
//but it will return a string like this.
$lang = 'EN';
return get_option('home') . '/' . $lang . '/' . $permalink;
//This returns a link that looks something like this:
//http://somedomain.com/EN/permalink-structure
}
So as you can see, I have no problems creating the links, the problem lies with the url-rewriting on the server itself.
I have tried this method: http://shibashake.com/wordpress-theme/wordpress-permalink-add
but I didn't get that one to work either. The problem is that I just don't seem to understand how these rewriting-rules work and that I can't seem to find a decent tutorial on the subject either.
Any help would be greatly appreciated.
the filter to listen to for handling url is request.
function request_handler($vars) {
//modified $vars here
return $vars;
}
add_filter('request', 'request_handler', 11);
i have written a plugin for customize url in wp. check out the source and see how i handle it.
http://wordpress.org/extend/plugins/auto-url/

How to use wildcards in drupal

I have a website where drupal manage the contents, but another application handle the e-commerce (my customer doesnt like to change his own e-commerce)
I dont like to have the e-commerce to looks differently from the rest of the websites, so i've made a drupal Page node with php code in the body, that simply include the external app and initialize it.
It works well, but the problem is the other link the e-commerce generate:
http://example.com/shop #The Page node i've created, this work
http://example.com/shop/catalog/fruit/ #here comes the trouble!
The external app handle the url by its own, so what i need is to tell drupal to redirect all the url that begin with shop to his shop Page node... something like
http://example.com/shop/* => load http://example.com/shop
What is the best practices to do that?
If you create a module rather than a node this will be quite easy.
use hook_menu() to match the URL string
function example_menu() {
$menu = array()
$menu['shop'] = array(
'page callback' = 'example_callback';
)
}
function example_callback() {
// use arg() to get arguments.
return shop_php();
}
Creating a callback with hook menu allows you to call your own code, the value returned by the callback will be displayed in the page. When drupal sees a URL which matches shop* it will call the function example_callback. In this function you can put the code you currently have in your page node. And return the content you wish to display in the page.
After googlin around, i found the Drupal custom_url_rewrite_inbound that does exactly what i need.
I inserted the function in my /sites/default/settings.php:
function custom_url_rewrite_inbound(&$result, $path, $path_language) {
if(preg_match("/^shop(\/)/", $path, $matches)) {
$result = 'node/XX'; //XX is the ID of my Page Node with the ecommerce code.
}
}
It works like a charm!

Categories