• 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

How do I turn this snippet into a function?

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 › How do I turn this snippet into a function?

This topic is: resolved

Tagged: 404, error, function, plugin

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

    I've a rather specific site setup (multisite) and I need to create a function to add to a custom plugin.

    The function I want to create uses code taken from 404.php in the Genesis parent theme. I cannot create a fork off of this file in the usual manner.

    It absolutely has to be a plugin. (I've used many of the already available plugins in the WP repository and none of them are affecting and therefore changing genesis_404_entry_title.

    See my screenshot here and you'll see I used the browser element inspector to discover that the CSS class h1 class entry-title is what I need to affect.

    The code I want to turn into a hookable function is:

    'content' => apply_filters( 'genesis_404_entry_title', __( 'Not found, error 404', 'genesis' ) ),

    And here's the entire 404.php code for reference:

    <?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();
    

    Unfortunately I am useless when it comes to creating functions. I can reverse engineer them but I wouldn't know where to begin with this.

    https://previews.dropbox.com/p/thumb/AAdn2Zuow5DIzlIGmh7HGDyQ4gWTORwZ1am_g440zq8ux9BEgvVOvu5T3vhqMlJU9jY1uBfQdemiUgCQTaAcyZ6HTr16HdGThpv13ntrdFCmhj_zsN7Vu8EQ3JjSNCbcC4ajjfDkery9-VbW2GZThObP4BVkJYg7qM67LfuJlUvPs8BM9HDveY33Zt4a3P7le1WWLuijfdkp6aSOgME7clDUUQzNRnNPHGcKIk6_A5pFTJhhHhJqJ7NoLbUpD1Fbi8t2QsBPOMxYgzXqRfXQU0FUSTWn1gBd0gM32hnVU4McK2JUyizWDtAEa13IASTojAXvg3HnVJinhRtYYRJA9SWz/p.jpeg?fv_content=true&size_mode=5
    July 11, 2019 at 7:33 am #492287
    Victor Font
    Moderator

    What's your end goal? What are you trying to accomplish?

    If you're trying to change the title of a 404 page based on some criteria, you wouldn't use apply_filters in a stand alone plugin. You would need to use add_filter.

    If you look at the 404 page code, the apply_filters runs inside the WordPress loop. The WordPress loop displays the content on the page. To use the same apply_filters in a stand alone plugin, you'd have to write your own loop.

    I'm just trying to understand what your "rather specific site" requirement is.


    Regards,

    Victor
    https://victorfont.com/
    Call us toll free: 844-VIC-FONT (842-3668)
    Have you requested your free website audit yet?

    July 12, 2019 at 3:18 am #492325
    ilkweb
    Member

    Hello Victor, I'm wanting to change the text that reads "Not found, error 404".

    Here's a screenshot.

    My specific site setup is a multisite. I will be using one child theme for both sites, and will be using plugins on both sites to change the aforementioned 404 text. The text will be different on each site (different languages).

    July 12, 2019 at 3:31 am #492326
    ilkweb
    Member

    Hello again, I've been experimenting and have come up with this snippet, and it seems to be working.

     //* Customize 404 page entry title
    add_filter( 'genesis_404_entry_title', 'not_found_change_entry_title_text', 10, 2 );
    function not_found_change_entry_title_text( $text ) {
    	$text  = __( 'Sorry, nothing was found');
    	return $text;
    }

    Can you see any issues with it?

    July 12, 2019 at 4:08 am #492328
    ilkweb
    Member

    Bill Erickson has improved my code:

    //* Customize 404 page entry title*
    *add_filter( 'genesis_404_entry_title', 'not_found_change_entry_title_text', 10, 2 );*
    *function not_found_change_entry_title_text( $text ) {*
    *    $text  = __( 'Sorry, nothing was found - English');*
    *    return $text;*
    *}

    It looks I was almost there.

    July 12, 2019 at 6:49 am #492332
    andytc
    Participant

    I think you were there anyway , the only thing different in the last 'improved' version is the actual text it will print out ?

    July 12, 2019 at 7:13 am #492333
    ilkweb
    Member

    My mistake in my last post. I meant to post this:

    (Bill's improvement)

     //* Customize 404 page entry title
    add_filter( 'genesis_404_entry_title', 'be_404_entry_title' );
    function be_404_entry_title( $title ) {
        return 'Sorry, nothing found - English';
    }
  • Author
    Posts
Viewing 7 posts - 1 through 7 (of 7 total)
  • The topic ‘How do I turn this snippet into a function?’ is closed to new 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