Community Forums › Forums › Archived Forums › Design Tips and Tricks › Placing custom 404.php code into a plugin?
Tagged: Custom 404, function, genesis, plugin
- This topic has 4 replies, 2 voices, and was last updated 5 years, 6 months ago by ilkweb.
-
AuthorPosts
-
July 9, 2019 at 6:29 am #492217ilkwebParticipant
Hi there.
I'm creating a multisite using the Business Pro theme by SEO Themes.
I've used some of the already available plugins for modifying the Error 404 pages, but none of these go far enough. They don't modify the document title (as in meta title), for example.
What would I need to do to modify the document title?
Here's the 404.php code from the Genesis parent theme:
<?php /** * Genesis Framework. * * WARNING: This file is part of the core Genesis Framework. DO NOT edit this file under any circumstances. * Please do all modifications in the form of a child theme. * * @package Genesis\Templates * @author StudioPress * @license GPL-2.0-or-later * @link https://my.studiopress.com/themes/genesis/ */ // Remove default loop. remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'genesis_404' ); /** * This function outputs a 404 "Not Found" error message. * * @since 1.6 */ function genesis_404() { genesis_markup( array( 'open' => '<article class="entry">', 'context' => 'entry-404', ) ); genesis_markup( array( 'open' => '<h1 %s>', 'close' => '</h1>', 'content' => apply_filters( 'genesis_404_entry_title', __( 'Not found, error 404', 'genesis' ) ), 'context' => 'entry-title', ) ); $genesis_404_content = sprintf( /* translators: %s: URL for current website. */ __( 'The page you are looking for no longer exists. Perhaps you can return back to the <a href="%s">homepage</a> and see if you can find what you are looking for. Or, you can try finding it by using the search form below.', 'genesis' ), esc_url( trailingslashit( home_url() ) ) ); $genesis_404_content = sprintf( '<p>%s</p>', $genesis_404_content ); /** * The 404 content (wrapped in paragraph tags). * * @since 2.2.0 * * @param string $genesis_404_content The content. */ $genesis_404_content = apply_filters( 'genesis_404_entry_content', $genesis_404_content ); genesis_markup( array( 'open' => '<div %s>', 'close' => '</div>', 'content' => $genesis_404_content . get_search_form( 0 ), 'context' => 'entry-content', ) ); genesis_markup( array( 'close' => '</article>', 'context' => 'entry-404', ) ); } genesis();
Furthurmore, and more importantly, is it at all possible to put the contents of 404.php into its own plugin?
It's important that I do this as I will need to create two versions of the same plugin, each in different languages, to be installed on separate sites in the WordPress multisite installation.
I've only one child theme folder, which means certain options are off the table in terms of creating new child theme code files to fork off their parents.
The plugin option is one I've been looking for online for some time but have had no luck.
I did something similar with some Genesis search box custom placeholder code that would normally go into functions.php
This code was put inside two plugins and it works wonderfully.
Here's what I did with that:
<?php /* Plugin Name: Genesis Search Placeholder (English) Plugin URI: Description: Adds custom search engine placeholder text in English Version: 0.1.0 Author: John D Author URI: https://example.com Text Domain: genesis-search-placeholder-en Domain Path /languages/ */ /* The previous section is what defines the plugin. The Plugin Name line defines the name. The Plugin URI line is a link to where the plugin details can be found online Since this plugin isn't released I'm just linking to root domain The Description line is the short description that will show in the WordPress dashboard. The Version line defines the version of the plugin file The Author line defines the author. This should match your WordPress user name if uploading to the WordPress repo The Author URI provides a link to your Web site The text domain line allow you to hint at the domain that is used for internationalizing your text strings The domain path defines where WordPress should look for translation files. */ /* Prevent direct access to the plugin */ if ( !defined( 'ABSPATH' ) ) { die( "Sorry, you are not allowed to access this page directly." ); } define( 'GENESIS_BOILERPLATE_LIB', dirname( __FILE__ ) . '/lib/' ); /** * Loads plugin text domain and required files. Uses genesis_init to ensure Genesis functions are available * * @since 0.1.0 * * @uses GENESIS_PLACEHOLDER_LIB * */ //* Customize search form input box text add_filter( 'genesis_search_text', 'sp_search_text' ); function sp_search_text( $text ) { return esc_attr( 'Search website' ); }
Thanks for reading! If anyone can point to me in the right direction, or tell me if my attempt to place the 404.php code inside a plugin is possible, I'd be very grateful.
This forum is a fantastic resource and I hope to one day be good enough with coding to give more answers more often than I ask questions!
Darren
http://localhostJuly 9, 2019 at 9:23 am #492226AnitaCKeymasterBill Erickson created the Genesis 404 Plugin if you'd like to use that instead of creating your own, or you can use it as a guide to creating your own.
Gary Jones also has a plugin boilerplate in Github that you can take a look at.
Need help with customization or troubleshooting? Reach out to me.
July 9, 2019 at 10:27 am #492228ilkwebParticipantHi Anita,
Thanks for the suggestion. I've already tried Bill's plugin but it does not change the meta titles of a 404 page. My site will be multi lingual and I want to the change document title from 'page not found' on the browser tab to a different language.
Other parts of the page show default text pulled from the Genesis 404.php file that Bill's plugin is not addressing.
I'm wanting to place the current 404.php text into a plugin the way you would with code that would normally go into functions.php. I just want to be able to make my customisations in the code directly. Bill's plugin is giving me a rich text editor which is not required.
The only other way I can think of for achieving what I have in mind is using a translation plugin, but it's really only a couple of things I want to change that are buried somewhere i nthe website code.
July 9, 2019 at 10:38 am #492229AnitaCKeymasterThis reply has been marked as private.July 9, 2019 at 10:57 am #492230ilkwebParticipantHi Anita,
Thanks for that, I'll take a look.
Meanwhile, I've found a function that works to replace 404 page meta titles:
//* Defines document title of 404 page function theme_slug_filter_wp_title( $title_parts ) { if ( is_404() ) { $title_parts['title'] = 'ADD 404 TITLE TEXT HERE'; } return $title_parts; } // Hook into document_title_parts add_filter( 'document_title_parts', 'theme_slug_filter_wp_title' );
I'll be adding this to a custom plugin.
My next task is to work out how to create a similar function to change 'Not found, error 404' using a plugin, as even with Bill's plugin, I was still getting the default text from 404.php on my page.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.