wordpress URL end with a number - wordpress

I am not sure if this is a known issue or suppose to be like this performance,
any wordpress website, if you put a number at the end of the URL, for example,
http://perishablepress.com/wordpress-multisite-mamp/
we put 1 or any numbers at the end
http://perishablepress.com/wordpress-multisite-mamp/123
We will not get the NOT FOUND page, we'll still stay at the same page, which is
http://perishablepress.com/wordpress-multisite-mamp/
Is this an error or normal ? how do we redirect to NOT FOUND PAGE instead?

It's not an error, the 123 is interpreted as a pagination parameter:
Request: wordpress-multisite-mamp/123
Query String: page=%2F123&name=wordpress-multisite-mamp
Matched Rewrite Rule: ([^/]+)(/[0-9]+)?/?$
Matched Rewrite Query: name=wordpress-multisite-mamp&page=%2F123
Posts can be paginated with <!--nextpage-->. WordPress displays the content of the last page if no further content is found (or the full post if the post is not paginated).
To redirect to the 404 page when the pagination parameter exeeds the number of pages, drop the following in your functions.php file:
add_action( 'template_redirect', 'so16179138_template_redirect', 0 );
function so16179138_template_redirect()
{
if( is_singular() )
{
global $post, $page;
$num_pages = substr_count( $post->post_content, '<!--nextpage-->' ) + 1;
if( $page > $num_pages ){
include( get_template_directory() . '/404.php' );
exit;
}
}
}

Related

Output certain product page on homepage / WooCommerce shortcode not working properly

I need to output a certain product page on the homepage. add_rewrite_rule doesn't work for homepage for any reason (
there are actually no rewrite rules for the homepage in the database, WordPress seems to use some other functions to
query the homepage):
//works fine
add_rewrite_rule( 'certainproductpage/?$',
'index.php?post_type=product&name=certainproduct',
'top'
);
//does not work
add_rewrite_rule( '', //tried everything like "/", "/?$" etc
'index.php?post_type=product&name=certainproduct',
'top'
);
After spending way too much time looking through wp / wc core code and stackoverflow I came across an alternative. I can
simply add a shortcode in the content of the page I need to be the homepage and a product page at the same
time: [product_page id=815]. Indeed it works great, but only if the shortcode is added in the admin editor or is
stored in the database (post_content). If I try to call the shortcode manually on the page template (
page-certainproductpage.php) then it outputs the product page without some necessary stuff (PayPal, PhotoSwipe and
Gallery js). Weirdly enough, if I keep the shortcode in the content (via Gutenberg / Code Editor) but don't
call the_content and only echo the shortcode then everything works fine:
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
get_header( 'shop' );
//works fine only if the same shortcode is within the certainproductpage's content
echo do_shortcode("[product_page id='815']");
//the_content();
get_footer( 'shop' );
Also when I try to add the shortcode via the_content filter hook before the do_shortcode function is applied in core's
default-filters.php ($priority < 11), then I get only the error:
NOTICE: PHP message: PHP Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/html/wp-includes/functions.php on line 5106
Unfortunately there is no stack trace logged. And the function around line 5107 is wp_ob_end_flush_all which is called on shutdown from default-filters.php
echo do_shortcode(apply_filters('the_content', "[product_page id=815]")); did not help either (same incomplete output as
with echo do_shortcode("[product_page id=815]");)
Also totally weird:
When I compare the string of the content from the editor and the string of the shortcode added programmatically it is
equal!:
add_filter( "the_content", function ( $content ){
$wtf = "<!-- wp:paragraph -->
<p>[product_page id=815]</p>
<!-- /wp:paragraph -->";
$result = $wtf === $content;
?><pre><?php var_dump($result)?></pre><?php
return $content;
}, 1 );
But if I replace return $content with return $wtf - I get the maximimum exucution time exceeded error.
So how can I properly output a product page on the homepage ("/") or how can I get the same result with the shortcode
when applied within the the_content filter as when just adding the shortcode in the (Gutenberg) editor?
Update
Tested it with a simple custom shortcode outputting only a heading tag and it works fine with the_content filter. Also tried it on an absolutely clean site with only WooCommerce and PayPal installed - with the same results. Seems to be a bug on the WooCommerce side. Gonna run it through xDebug some day this week.
Ok, found a bit of a hacky solution. I just check on every page load whether the homepage is currently queried or not. Then I get the page content and check if it already contains the shortcode. If not then the page content gets updated in the database with the shortcode appended.
//it has to be a hook which loads everything needed for the wp_update_post function
//but at the same time has not global $post set yet
//if global $post is already set, the "certainproductpage" will load content not modified by the following code
add_action( "wp_loaded", function () {
//check if homepage
//there seems to be no other simple method to check which page is currently queried at this point
if ( $_SERVER["REQUEST_URI"] === "/" ) {
$page = get_post(get_option('page_on_front'));
$product = get_page_by_path( "certainproduct", OBJECT, "product" );
if ( $page && $product ) {
$page_content = $page->post_content;
$product_id = $product->ID;
$shortcode = "[product_page id=$product_id]";
//add shortcode to the database's post_content if not already done
$contains_shortcode = strpos( $page_content, $shortcode ) > - 1;
if ( ! $contains_shortcode ) {
$shortcode_block = <<<EOT
<!-- wp:shortcode -->
{$shortcode}
<!-- /wp:shortcode -->
EOT;
$new_content = $page_content . $shortcode_block;
wp_update_post( array(
'ID' => $page->ID,
'post_content' => $new_content,
'post_status' => "publish"
) );
}
}
}
} );
I'd recommend one step at a time. First of all, does this work?
add_filter( "the_content", function ( $content ) {
$content .= do_shortcode( '[product_page id=815]' );
return $content;
}, 1 );
This should append a product page to every WordPress page/post.
If it works, then you need to limit it to the homepage only, by using is_front_page() conditional in case it's a static page:
add_filter( "the_content", function ( $content ) {
if ( is_front_page() ) {
$content .= do_shortcode( '[product_page id=815]' );
}
return $content;
}, 1 );
If this works too, then we'll see how to return a Gutenberg paragraph block, but not sure why you'd need that, so maybe give us more context

Wordpress Homepage Redirect to other page

SO I have this problem when I go to my main website, for example, www.mysite.com I want to redirect it to www.mysite.com/home I'm using this plugin 'link. it works but not always, I don't understand why so then I wrote a little script in the header file
$GetURL = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"
if ($GetURL === 'www.mywebsite.com') {
header('Location: www.mywebsite.com/home');
}
But still, it doesn't redirect. what could be a problem?
You can set your page as Front Page is Setting -> Reading or
add_action('template_redirect', 'function(){
if(is_front_page()){
wp_redirect(get_permalink('your_page_id'), 301)
}
});
Maxim's answer helped me redirecting the homepage to a custom post type archive, but it contains two typos. First, there must not be an apostrophe before "function"; second, the wp_redirect command needs to be closed with a semicolon.
This is a version of the code which, put into functions.php, points the front page to the archive of the post type 'project':
add_action(
'template_redirect',
function( $post_type ){
$post_type = 'project';
if ( is_front_page() ){
wp_redirect( get_post_type_archive_link( $post_type ) );
}
}
);
Obviously, you can use get_permalink() to redirect to a static page as well.

redirect specific page category if not logged in

I'm sure this was working but now it's not. I added categories to pages with:
function add_categories_to_pages() {
register_taxonomy_for_object_type( 'category', 'page' );
}
add_action( 'init', 'add_categories_to_pages' );
then tagged a range of pages with a 'client' category and added to functions:
add_action( 'template_redirect', 'client_redirect_to_login' );
function client_redirect_to_login() {
$category_slug = 'client';
global $pages;
if ( ! is_user_logged_in() && in_category( $category_slug, $pages ) ) {
wp_redirect( site_url( '/login' ) );
exit();
}
}
the intention, simply enough, is that not logged in users trying to directly access any of the 'client' pages is redirected to my custom /login page. being returned directly to the page they were trying to access would be a bonus but this, THIS, used to work. there's a dozen pages and growing, so restricting by category is easier than by page ID array - but i can't see what i'm doing wrong.
all advice greatly appreciated!
From global variables pages in codex $pages will return:
The content of the pages of the current post. Each page elements contains part of the content separated by the <!--nextpage--> tag.
And you need either post/page ID or object when using in_category() function as a second argument, as noted here.
You can try to get the category of the visited page by setting:
$current_cat = get_the_category();
And then check
in_category( $category_slug, $current_cat );
inside the conditional.

Woocommerce: how to jump directly to product page when category only holds one item

I have a top menu which permits the display of product categories.
In this case, the name of the application I am selling.
When this menu item is clicked, it shows the contents of the category - as it should do.
However, as this category only contains one item, I want to jump straight to the product page, instead of displaying a category page with one item.
Here is a link to the page in question : boutique.zimrahapp.com/categorie-produit/app/
I have not been able to find either a hook or a template where I can adjust the output or do a redirect.
Has this kind of thing already been done ?
The above code didn't work for me. I have a solution that works, but it redirects all single archive results. In my case I wanted to also redirect single tags.
/* Redirect if there is only one product in the category or tag, or anywhere... */
function redirect_to_single_post(){
global $wp_query;
if( is_archive() && $wp_query->post_count == 1 ){
the_post();
$post_url = get_permalink();
wp_safe_redirect($post_url , 302 );
exit;
}
}
add_action('template_redirect', 'redirect_to_single_post');
I had asked this same question here: Woocommerce: How to automatically redirect to the single product if there is only one product on a category page?
WooCommerce redirects a search query with only one result to that result. You can see how they are doing it here.
Modifying their code you get something like this:
function so_35012094_template_redirect() {
global $wp_query;
// Redirect to the product page if we have a single product
if ( is_product_category() && 1 === $wp_query->found_posts ) {
$product = wc_get_product( $wp_query->post );
if ( $product && $product->is_visible() ) {
wp_safe_redirect( get_permalink( $product->id ), 302 );
exit;
}
}
}
add_action( 'template_redirect', 'so_35012094_template_redirect' );
Untested, so watch out for copy/paste fails. Always use WP_DEBUG so you can figure out what went wrong.

How to change search query string to permalink in Wordpress?

I'm having a problem with my Wordpress site when using search. This is probably due to qTranslate plugin.
So I have permalink turned on and when I use search field, I get URL with ?s= query string like this:
www.mydomain.com/en/?s=test
The above URL works just fine but if I have many search results that show pagination buttons, links on those buttons cause 404 errors. Link for second page has the following URL:
www.mydomain.com/en/page/2/?s=test
This throws 404 error but if I manually modify the URL like this:
www.mydomain.com/en/search/test/page/2/
then the pagination works.
So I would like to force Wordpress to use /search/ permalink instead of /?s= query string but not sure how to do that.
you can do it by the following function, paste this into your theme functions.php
function change_search_url_rewrite() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'change_search_url_rewrite' );
then the search url will be like search/test instead of ?s=test,this function is quit simple

Resources