Archived topic
Adding PHP in funtions file of Child Theme
29 replies · Started by Matias on October 16, 2019
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
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.
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
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
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.
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.
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 :)
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.
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
What's the specific error? It should give you some sort of hint at what's going on.
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.
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.
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)
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.
