How to override WordPress title using plugin custom template - wordpress

I am trying to display a custom page using a plugin. It only shows when a URL is matched. The problem is, this page is 404 normally, since no pages or posts exists with that address. So it shows the 404 page title Page not found.
Here is what I am doing
add_filter('template_include', [$this, 'pageTeamplate']);
public function pageTeamplate($templates, $content=''){
global $wp;
if($wp->query_vars['pagename'] != 'forehand-news')
return $templates;
get_header();
wp_enqueue_style('baseplugin-frontend');
wp_enqueue_script('baseplugin-frontend');
$response = wp_remote_request("http://backoffice.localhost/app/news",
array(
'method' => 'GET'
)
);
//var_dump($response);
//echo $response;
$divData = wp_remote_retrieve_body($response);
echo "<script>window.news = $divData </script>";
echo '<div id="vue-frontend-app"></div>';
get_footer();
}
The page shows and its content, but the title remains as Page not found.
How can I change the title or what is the best way to show a custom plugin page against a URL?

Add the following code snippet to your active theme's functions.php file:
function modify_404_page_title( $title_parts ) {
if ( is_404() ) {
$title_parts['title'] = 'ADD 404 TITLE TEXT HERE';
}
return $title_parts;
}
add_filter( 'document_title_parts', 'modify_404_page_title' );

Related

wordpress update permalinks structure

I need to change my blog archive pagination url from:
/blog/2
to blog/page/2
This is our current permalink setup under Settings
Custom Structure - /blog/%postname%/
I don't have access to the server, htaccess, or the template files so trying to accomplish this inside functions.php
I am trying to use paginate_links filter to update the 'format' of my url string but does the $link argument have all the details of the paginate_links function, base, format..ect. I tried to use var_dump to see the $link array/object but nothing prints out to the screen.
// define the paginate_links callback
function filter_paginate_links( $link ) {
// Blog page
if ( !is_front_page() && is_home() ) {
var_dump($link);
return $link;
}
return $link;
};
// add the filter
add_filter( 'paginate_links', 'filter_paginate_links', 10, 1 );

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

How can I redirect users on the new 404 page without plugin?

I have a 404 page in my theme but I am not using that page. I have created a new 404 page in WordPress using wpbakery page builder. I need to know how can I redirect users on the new 404 page without a plugin?
You can use plugin 404page.
Or some code adapted from this plugin:
add_filter(
'404_template',
static function () {
global $wp_query;
$wp_query = new WP_Query();
$wp_query->query('page_id='.$pageID);
$wp_query->the_post();
$template = get_page_template();
rewind_posts();
add_filter(
'body_class',
static function ($classes) {
if (!in_array('error404', $classes, true)) {
$classes[] = 'error404';
}
return $classes;
}
);
return $template;
},
999
);
Crate 404page in the admin.
create a custom page template for that page.
add your custom 404 content
open 404.php file in your theme.
add this below code at the top of that file.
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".home_url('/404page/'));
exit();
try to find something that not found and you will be redirected to your custom 404 page
also, you can try this action hooks for redirect to custom 404 page. put this code in your function.php file. this is the replace option of above point number 5)
add_action( 'template_redirect', 'redreict_to_custom_404_page' );
function redreict_to_custom_404_page(){
// check if is a 404 error
if( is_404() ){
wp_redirect( home_url( '/404page/' ) );
exit();
}
}
Or if you want to create 404 page with WP bakery
Create a private 404page and build with WP bakery in the admin.
open 404.php file and get content of 404page by code below
$page_id = 123; // 404page id
$page = get_post( $page_id );
$content = $page->post_content;
echo $content;

I want to change the 404 page title of a wordpress theme

I have a website that uses worpdress with catch box theme, and I want to change the 404 page title.
I looked for the "Nothing found for" sentence on the PHP files on the Editor but I did not find nothing.
I searched for the get_header(); method in editor to change the title but I did not find it.
see the title that I want to change
Have been already answered here: https://wordpress.stackexchange.com/questions/30873/how-to-change-404-page-title
Add the following to theme functions.php file
function theme_slug_filter_wp_title( $title ) {
if ( is_404() ) {
$title = 'ADD 404 TITLE TEXT HERE';
}
// You can do other filtering here, or
// just return $title
return $title;
}
// Hook into wp_title filter hook
add_filter( 'wp_title', 'theme_slug_filter_wp_title' );
You can use filter hook
function theme_slug_filter_wp_title( $title ) {
if ( is_404() ) {
$title = 'ADD 404 TEXT HERE';
}
return $title;
}
add_filter( 'wp_title', 'theme_slug_filter_wp_title' );
try this one for wordpress version 4.4+
file : function.php
function theme_slug_filter_wp_title($title_parts)
{
if (is_404()) {
$title_parts['title'] = 'My Custom Title Text';
}
return $title_parts;
}
add_filter('document_title_parts', 'theme_slug_filter_wp_title');

Wordpress: map existing page content instead of a copy

I have a page with several subpages in wordpress. The page should display the same content as the second subpage. When I edit the page, there is an option to copy the content from the subpage into it, but then I would have to maintain two instances of the text. Is there a possibility to map existing page content into a page? I'm using Wordpress.com so I cannot edit any files/links on the server.
You could use a shortcode that fetches the content of a page specified. In functions.php:
add_action( 'init', 'so20477735_init', 11 );
function so20477735_init()
{
add_shortcode( 'duplicate_page', 'so20477735_shortcode_callback' );
}
function so20477735_shortcode_callback( $atts, $content = null )
{
extract( shortcode_atts( array(
'page_id' => 0
), $atts ) );
$page_data = get_page( $page_id );
$return = '';
if( ! is_null( $page_data ) )
$return .= $page_data->post_content;
return $return;
}
Example usage:
[duplicate_page page_id="12"]
EDIT
I missed the fact you're using wp.com. You should be able to do something similar with the display posts shortcode:
http://en.support.wordpress.com/display-posts-shortcode/

Resources