[Support request] Adding PHP in funtions file of Child Theme

Home Forums Support [Support request] Adding PHP in funtions file of Child Theme

Home Forums Support Adding PHP in funtions file of Child Theme

Viewing 15 posts - 1 through 15 (of 30 total)
  • Author
    Posts
  • #1036495
    Matias

    Hi, Im tryig to add this code to make a custom 404 page in funtions.php file of a GP child Theme:

    /* CUSTOM 404 PAGE */
    add_filter( ‘generate_404_title’,’generate_custom_404_title’ );
    function generate_custom_404_title()
    {
    return ‘Custom 404 title’;
    }
    add_filter( ‘generate_404_text’,’generate_custom_404_text’ );
    function generate_custom_404_text()
    {
    return ‘Custom 404 text’;
    }

    But I always get a WordPress Error when I try to load the web
    How can add code un funtions.php file without problems?
    thanks

    #1036536
    David
    Staff
    Customer Support

    Hi there,

    can’t see a problem with that code.
    What is the error ?
    And can you try using the Code Snippets plugin to test, if that errors then there is something conflicting.

    #1036544
    Matias

    Thanks for your answer.
    I rather not use more plugins in my wordpress site.
    I just add the code in funtions file and when I try to load the website I get a WordPress Error.
    Blank Page with that error

    #1036555
    David
    Staff
    Customer Support

    You don’t need to keep the plugin installed – it would be good if you can test the code using the plugin – if the error doesn’t happen then there is something else causing the issue. Just trying to narrow down where the issue could be

    #1036985
    Matias

    Hi. I have used Code Snippets Pluging to add de PHP code to Functions file without problems but I dont see any change in 404 error page.

    Always that I add some functions code to the file of my GP child theme I get the same error
    error

    #1037087
    David
    Staff
    Customer Support

    Do you have any other functions in your child theme?
    If so can you try removing the others to see if there is a conflict.

    #1037352
    Matias

    I have the generatepress folder with all files and a generatepress_child folder with 3 files:
    functions.php, screeshot.jpg and style.css

    The functions.php file content is:

    <?php
    /**
    * GeneratePress child theme functions and definitions.
    *
    * Add your custom PHP in this file. 
    * Only edit this file if you have direct access to it on your server (to fix errors if they happen).
    */
    function generatepress_child_enqueue_scripts() {
    if ( is_rtl() ) {
    wp_enqueue_style( 'generatepress-rtl', trailingslashit( get_template_directory_uri() ) . 'rtl.css' );
    	}
    }
    add_action( 'wp_enqueue_scripts', 'generatepress_child_enqueue_scripts', 100 );

    All works fine but if I add some php code after the last line the error appears.

    #1037724
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Can you try this as your functions.php file?:

    <?php
    /**
    * GeneratePress child theme functions and definitions.
    *
    * Add your custom PHP in this file. 
    * Only edit this file if you have direct access to it on your server (to fix errors if they happen).
    */
    
    add_action( 'wp_enqueue_scripts', 'generatepress_child_enqueue_scripts', 100 );
    function generatepress_child_enqueue_scripts() {
        if ( is_rtl() ) {
            wp_enqueue_style( 'generatepress-rtl', trailingslashit( get_template_directory_uri() ) . 'rtl.css' );
        }
    }
    
    add_filter( 'generate_404_title', function() {
        return 'Custom 404 title';
    } );
    
    add_filter( 'generate_404_text', function() {
        return 'Custom 404 text';
    } );

    Let me know 🙂

    #1037883
    Matias

    Hi, I have added this code to my functions.php file in GP Child Theme with the same result

    Loading a webpage or opening the WP admin Panel the error reappears.

    #1038285
    Matias

    Please any help?

    I have this functions file in child theme:

    <?php
    /**
    * GeneratePress child theme functions and definitions.
    *
    * Add your custom PHP in this file. 
    
     * Only edit this file if you have direct access to it on your server (to fix errors if they happen).
    */
    function generatepress_child_enqueue_scripts() {
    
    	if ( is_rtl() ) {
    
    		wp_enqueue_style( 'generatepress-rtl', trailingslashit( get_template_directory_uri() ) . 'rtl.css' );
    
    	}
    
    }
    
    add_action( 'wp_enqueue_scripts', 'generatepress_child_enqueue_scripts', 100 );

    I add some php code like this:
    `<?php

    /**

    * GeneratePress child theme functions and definitions.

    *

    * Add your custom PHP in this file.

    * Only edit this file if you have direct access to it on your server (to fix errors if they happen).

    */

    function generatepress_child_enqueue_scripts() {

    if ( is_rtl() ) {

    wp_enqueue_style( ‘generatepress-rtl’, trailingslashit( get_template_directory_uri() ) . ‘rtl.css’ );

    }

    }

    add_action( ‘wp_enqueue_scripts’, ‘generatepress_child_enqueue_scripts’, 100 );

    add_filter( ‘generate_google_font_display’, function() {
    return ‘swap’;
    } );`

    And I get a WordPress tecnichal problem adviser. Check you mail to test…

    Always the same error

    #1038332
    Tom
    Lead Developer
    Lead Developer

    What’s the specific error? It should give you some sort of hint at what’s going on.

    #1038346
    Matias

    You can see a imagen of the problem in this post:
    Post

    I open the functions.php of the child theme
    I add after the last line a php code like this:

    add_filter( ‘generate_google_font_display’, function() {
    return ‘swap’;
    } );

    And when I reload my web site I get a WordPress > Error

    The site is experiencing technical difficulties.

    #1038558
    Tom
    Lead Developer
    Lead Developer

    That’s not the specific error, though. You should receive an email with more details, which should have the specific error.

    If not, you can check the error_log file on your server, which should have a specific error. It’s hard to debug without knowing what we’re looking for.

    Alternatively, you can try the functions.php file in our child theme: https://docs.generatepress.com/article/using-child-theme/

    Then you can add your functions one by one until it breaks.

    #1038714
    Matias

    The error email says:

    An error of type E_PARSE occurred on line 1 of the file /home/tecnoped/public_html/tecnopeda.com/wp-content/themes/generatepress_child/functions.php. Error message: syntax error, unexpected ‘function’ (T_FUNCTION)

    #1039082
    Tom
    Lead Developer
    Lead Developer

    Can you try our child theme I linked to? Then just add your functions to it one by one – I’m thinking there might be some sort of funky character in yours.

Viewing 15 posts - 1 through 15 (of 30 total)
  • You must be logged in to reply to this topic.