• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Placing custom 404.php code into a plugin?

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › Design Tips and Tricks › Placing custom 404.php code into a plugin?

This topic is: not resolved

Tagged: Custom 404, function, genesis, plugin

  • This topic has 4 replies, 2 voices, and was last updated 3 years, 6 months ago by ilkweb.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • July 9, 2019 at 6:29 am #492217
    ilkweb
    Member

    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://localhost
    July 9, 2019 at 9:23 am #492226
    Anita
    Keymaster

    Bill 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.


    Love coffee, chocolate and my Bella!

    July 9, 2019 at 10:27 am #492228
    ilkweb
    Member

    Hi 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 #492229
    Anita
    Keymaster
    This reply has been marked as private.
    July 9, 2019 at 10:57 am #492230
    ilkweb
    Member

    Hi 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.

  • Author
    Posts
Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘Design Tips and Tricks’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2023 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble