Need URL without query string in WordPress - wordpress

I think people have already given solutions for the below questions so I have tried but that is not working, so please do not mark my questions in already asked.
Instead of this url example.com/product-detail?product_id=12345 I need example.com/product-detail/12345
And below I have written script in functions.php
add_filter( 'query_vars', 'addnew_query_vars', 10, 1 );
function addnew_query_vars($vars)
{
$vars[] = 'product_id'; // var1 is the name of variable you want to add
return $vars;
}
On the next page for getting product_id I have written code:
$product_id = get_query_var( 'product_id' ) ?? "123456";
Everything working properly in localMachine but showing 404 error on Live site.
Please help me for getting this url example.com/product-detail/12345 without 404 error.

Related

Wordpress function cannot call submit_button() as it ends with "undefinied function"

i am a real very newbie in coding and in Wordpress. Trying my first test plugin to understand basics. I am able to define plugin, register it, so I can see it in plugins, activate it.
My later goal is to be able to create custom form, save user-specific data to new DB table, and then enable reading/editing it.
I tried to follow the instructions from gmazzap placed here: https://wordpress.stackexchange.com/questions/113936/simple-form-that-saves-to-database
I just am having following error from WP in time of trying to display preview of new screen having a shortcode [userform] in it:
*Fatal error: Uncaught Error: Call to undefined function submit_button() in /data/web/virtuals/131178/virtual/www/subdom/test/system/wp-content/themes/twentytwentythree-child/functions.php:14
*
My functions.php of my theme now looks like this:
<?php
add_action('init', function() {
add_shortcode('userform', 'print_user_form');
});
function print_user_form() {
echo '<form method="POST">';
wp_nonce_field('user_info', 'user_info_nonce', true, true);
?>
All your form inputs (name, email, phone) goes here.
<?php
submit_button('Send Data');
echo '</form>';
}
add_action('template_redirect', function() {
if ( ( is_single() || is_page() ) &&
isset($_POST['user_info_nonce']) &&
wp_verify_nonce($_POST['user_info_nonce'], 'user_info')
) {
// you should do the validation before save data in db.
// I will not write the validation function, is out of scope of this answer
$pass_validation = validate_user_data($_POST);
if ( $pass_validation ) {
$data = array(
'name' => $_POST['name'],
'email' => $_POST['email'],
'phone' => $_POST['phone'],
);
global $wpdb;
// if you have followed my suggestion to name your table using wordpress prefix
$table_name = $wpdb->prefix . 'my_custom_table';
// next line will insert the data
$wpdb->insert($table_name, $data, '%s');
// if you want to retrieve the ID value for the just inserted row use
$rowid = $wpdb->insert_id;
// after we insert we have to redirect user
// I sugest you to cretae another page and title it "Thank You"
// if you do so:
$redirect_page = get_page_by_title('Thank You') ? : get_queried_object();
// previous line if page titled 'Thank You' is not found set the current page
// as the redirection page. Next line get the url of redirect page:
$redirect_url = get_permalink( $redirect_page );
// now redirect
wp_safe_redirect( $redirect_url );
// and stop php
exit();
}
}
});
Note: I have not got to DB exercise, point of my question is submit_button.
As indicated in the error, the code on line 1 points to non identified function:
submit_button('Send Data');
I understood from other discussions submit_button should be core function of WP, so I should be able to call it "directly", without the need of a definition.
I tried following:
originally had very similar code within the plugin, moved to functions.php
reinstalled core of WordPress version 6.1.1
tried several different Themes (as it looked this worked for other users, I tried "classic" and "twentytwentythree" )
And still no little step further, still having same issue with error described above. What I am doing wrong?
If someone would confirm this is WP core installation issue, I am ready to reinstall WP from scratch, just trying to save some time, if there might be other cause.
Thank you for any suggestions.

In wordpress where is the code that differentiates between categories and standard pages?

This is a question for Wordpress experts:
Where, in the Wordpress machine, is the code that decides whether to route a query to the standard page or to a category? Does it try to match specific strings in the URL looking for categories and then, if it doesn't get a match, default to the code that looks for the page in the wp_posts table?
This is something I have been wondering about for a while but I have found it difficult trying to trace the path of the query through the system.
Thanks for any insights you can give!
There is a function for this you can use to hook in a page or a category.
function is_category( $category = '' ) {
}
function is_page( $page = '' ) {
}
$category / $page can be ID, name, slug or array

How to add several parameters with slash in wordpress post url?

I want to add several parameters with slash not by question mark in Wordpress Post URL. Thanks in advance.
You can achieve by WordPress Standard. See below quick step and follow WordPress docx for specific functions/filters, if you get stuck.
You need to use add_rewrite_rule as per WordPress Standard to pass query string as a slash based URL
add_rewrite_rule(
'^pageslug/([^/]+)([/]?)(.*)',
//!IMPORTANT! THIS MUST BE IN SINGLE QUOTES!:
'index.php?pagename=pageslug&page_id=$matches[1]',
'top'
);
});
This filter is used to target the query string variable.
add_filter('query_vars', function( $vars ){
$vars[] = 'pagename';
$vars[] = 'page_id';
return $vars;
});
Update permalink settings
This will be used to get the value in a specific page
get_query_var( 'page_id' )

wordpress wrong category name in url redirects to the actual category

I have this url structure in my wordpress project:
www.example.com/category/subcategory/post
There is a weird issue where i'm entering a url with category & subcategory which do not exists - but with an existing post name, it redirects me to the relevant post.
so for instance:
I have a post named 'bitcoin' which is under the category 'reviews' and subcategory of 'trade'.
I go to - www.example.com/blabla/blulu/bitcoin
and I get redirected to - www.example.com/reviews/trade/bitcoin
I want to avoid this redirect from happening - any idea how or what is causing this?
I believe this is the sollution you are looking for:
function remove_redirect_guess_404_permalink( $redirect_url ) {
if ( is_404() )
return false;
return $redirect_url;
}
add_filter( 'redirect_canonical', 'remove_redirect_guess_404_permalink' );
I found this answer here, but until I get reputation 50 I cannot post this as comment.

RSS WordPress SimplePie claiming valid URL as invalid

I'm trying to load a RSS feed with Wordpress's built-in SimplePie.
include_once(ABSPATH . WPINC . '/feed.php');
$rssURL = 'http://missionstkitts.blogspot.com//feeds/posts/default';
$rss = fetch_feed($rssURL);
To debug, I used print_r($rss); and I get a WordPress error object:
WP_Error Object
(
[errors] => Array
(
[simplepie-error] => Array
(
[0] => WP HTTP Error: A valid URL was not provided.
)
)
[error_data] => Array
(
)
)
But, frustratingly, if I print $rssURL and then copy and paste it it goes straight to the correct feed. What is going on?
Since this is the first hit in google, probably worth me adding this possible solution:
For our instance - an intranet site, pulling an rss feed from another internal page, which in-turn resolves to an RFC1918 private address the feed was being blocked by Wordpress's URL checker for security reasons.
The easiest fix in my instance was to add the following to functions.php, but this does have security implications so be sure you understand it before you add it:
add_filter( 'http_request_args', function( $args ) {
$args['reject_unsafe_urls'] = false;
return $args;
} );
Further discussion and more information at - https://core.trac.wordpress.org/ticket/24646
By adding preg_match for specific urls we can minimize the amount of parsed unsafed urls:
function http_request_local( $args, $url ) {
if ( preg_match('/xml|rss|feed/', $url) ){
$args['reject_unsafe_urls'] = false;
}
return $args;
}
add_filter( 'http_request_args', 'http_request_local', 5, 2 );
So, while the above answers work, I think that there's a way to do this that is better to make sure that you're limiting the scope of where you're making the URL request to instead of allowing everything to go. So I'm proving the following information to anyone who stumbles across this just in case it helps.
This answer is useful if the resulting calls are to internal servers cross-communicating on private IPs but are still publicly accessible.
The snippet below is to be run on the site that's calling the RSS feed. The site that is providing the feed does not need this.
add_filter('http_request_host_is_external', function($bool, $host, $url){
if($url === 'https://www.example.com/news/feed/' && $host === 'www.example.com'){
return true;
}
}, 10, 3);

Resources