Is there any hook to define new wp-config constant? - php

I want to add new constant to wp-config.php file. But I don't want to add it manually, I just want to add it from my theme's function.php file.
Is there any action or filter that define new constant for wp-config.php file?
UPD-01:
The constans I want to add are:
define('JWT_AUTH_SECRET_KEY', 'salt');
define('JWT_AUTH_CORS_ENABLE', true);

In a word, no. wp-config.php gets fired way before the theme functions.php file.
You can define a constant in your theme functions file easily enough with define() which can be used to overwrite a constant with the same name. The only problem is that WordPress defines and uses many of these constants before functions.php is called.

Related

Use defined constants from parent theme in child theme in Wordpress

I'm creating a theme and was wondering if the following is possible. I want to define some constants in parent theme to be usable in a child theme. But, since Wordpress loads child themes functions.php file before parents functions.php, my constants always return the literal string. For example:
In parents theme:
define('THEME_VERSION', 1.0);
In child theme:
var_dump(THEME_VERSION);
returns the string 'THEME_VERSION' (with a warning, that a constant is not defined) and not the number.
Is there anything I can do here without defining constants in my child theme?
WordPress loads functions.php file of child-theme just before the functions.php file of parent-theme. So that you are getting the warning while using constants in functions.php file of child-theme from parent-theme.
Here is the explanation of child theme
The constants from child themes will be used instead of parents one. Though, there will be a warning, but that shouldn't create any problem if you disable warning.
You should add constants like this if you don't want any warning.
if ( !defined('CONSTANT') )
define('CONSTANT', 'constant_value');
For more information you can see here.
https://wordpress.stackexchange.com/questions/157505/override-constants-in-child-theme?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

Wordpress child theme - modifying file of includes folder

I know there are many others who are trying to deal with that problem, but I didn't find an answer.
I'd like to modify a file which is located in the includes folder of a theme.
I need to change the following line:
$html .= '<span class="profile-widget-info-item"><i class="fa fa-star"></i> '.__('Channel','mars').'</span>';
of this file: http://pastebin.com/Qm5r9NeW
How can I do this in my child theme?
Thank you all in advance,
Jake
Copy the file you have pasted to your child theme.
At the very top you will see an if statement that is checking if a function already exists. By placing this file in your child theme, the file will run first and prevent the parent theme from adding the function. You will need to change the name of the widget class so you don't cause a conflict. I've changed the class name from Mars_LoginForm_Widget_Class to Jake_LoginForm_Widget_Class.
if( !defined('ABSPATH') ) exit;
if( !function_exists('Mars_LoginForm_Widget') ){
function Mars_LoginForm_Widget() {
register_widget('Jake_LoginForm_Widget_Class');
}
add_action('widgets_init', 'Mars_LoginForm_Widget');
}
Now you also need to update the class name in the class definition to match the name you've given the class above.
class Jake_LoginForm_Widget_Class extends WP_Widget{
After that just make sure you are including this file in your child theme and then you can freely edit the widget class.

How to override a parent file in a child Wordpress theme when parent file is being required in functions.php

I'm rather new to designing and coding Wordpress themes and have recently started a child theme based on the Sydney theme that's offered free in Wordpress. I have hit a snag I can't work out.
There's a PHP file being called through the parent's functions.php that I need to customize and need to be untouched when I update the parent theme. This file is called template-tags.php and is located inside an "inc" folder. Now, supposedly, according to the Wordpress codex (which I have consulted before asking here), any duplicate files on the child theme (except the functions.php) will automatically override those in the parent theme. I don't know if this works as well for files inside folders but I think the fact that this particular file is being called in the parent's functions.php makes it so that it is the parent's file that is being loaded. I am assuming this because the same goes for the style CSS and JS script files that I had to enqueue in the child theme in order to get my own customized versions in.
However, there is nothing in the Wordpress codex that explains how to override a file that is being already called in the parent's functions.php. And I can't find anything anywhere else (not even in these questions: WordPress child theme override a parent theme include, Overiding parent theme functions with a Child theme in WordPress, How to override a not plugable parent theme function from a non function.php file?, Wordpress child theme - functions.php copy - cannot "redeclare") that helps me specifically.
So, to sum it all up:
The parent theme has an "inc" folder with the file template-tags.php in it.
My child theme also has an "inc" folder with the file template-tags.php in it.
The parent theme's functions.php has the following code that calls its file:
require get_template_directory() . '/inc/template-tags.php';
Anything I try to add in my child's functions.php file that calls its template-tags.php file will result in an error because obviously WP can't require the same thing twice.
Short of commenting out the call in the parent's functions.php, which does't seem to me a practical solution but more of a work-around, I have no idea what else to do.
I would appreciate any input at this time. Thanks in advance!
Hello #QuestionerNo27,
It's been a long time since you asked this but I've also been looking for an answer for 2 days as I was facing the same issue. There is the answer, simple like "Hi":
You dont need the inc folder in your child theme nor the template-tags.php file.
You just have to copy the function(s) you want to override from your parent template-tags.php and paste it into your child theme functions.php
In my case, I wanted to override function mytheme_posted_on() from my parent template-tags.php
And it's now working. Thanks to https://wordpress.org/support/topic/inc-folder-in-child-theme (Stephencottontail answer)
I had a similar problem where I wanted to make changes to the layout of the header in my child theme within the template-tags.php.
The solution that worked for me was to use the following line in my functions.php file in my child theme:
require get_stylesheet_directory() . '/inc/template-tags.php';
I was using the original line from the the Parent them in functions.php:
require get_template_directory() . '/inc/template-tags.php';
Using the function get_template_directory() was not working because this function will always return the parent theme directory.
Using the function get_stylesheet_directory() worked because it retrieves the Child Theme directory.
Yes I know the name of the function "get_stylesheet_directory()" is not very intuitive but it it actually will return the Child Theme directory location.
It took a while to work this out by searching online. I found this reference online that helped me in my search: How to override a parent file in a child Wordpress theme when parent file is being required in functions.php
you should use
get_stylesheet_directory() that refers to active theme directory
instead of get_template_directory() that refers to parent theme directory
https://www.csestack.org/overwrite-wordpress-php-file-subfolder-child-theme/

Wordpress child theme - functions.php copy - cannot "redeclare"

I'm using Starkers with a child theme (starkers-child).
I need to edit the functions.php to declare a menu, so I copied down the functions.php, JS folder and external folder (which were all declared somewhere in the functions file)
However I still have one error:
Cannot redeclare starkers_script_enqueuer()
(previously declared in C:\wamp\www\redlaw\wp-content\themes\starkers-child\functions.php:65) in
C:\wamp\www\redlaw\wp-content\themes\starkers-master\functions.php
I believe it is caused by this line:
function starkers_script_enqueuer() {
I can see why as it already calls the function in the master, so it cannot call it again in the child.
But if I remove this line from the master then doesn't that defeat the purpose of keeping a clean master and having a child theme?
Full functions.php is here (unedited, as it appears in the master theme)
http://jsfiddle.net/8KGcK/
Forgive me, I don't have enough rep to just comment.
A child theme's functions.php is loaded in addition to the parent theme. So you don't need to actually copy and paste the code. What everyone else says about wrapping it in an if(function_exists(function)) is optional, but a best practice because it's less error prone.
Anyways!
This is from the Codex:
Unlike style.css, the functions.php of a child theme does not override its counterpart from the parent. Instead, it is loaded in addition to the parent’s functions.php. (Specifically, it is loaded right before the parent’s file.)
Read more here

prestashop 1.5 core file overwrite

I want to overwrite prestashop 1.5 core file FrontController.php to overwrite from override\classes\controller folder but it didn't load my overwrite folder file.
<?php
class FrontController extends FrontControllerCore
{
/* Display a specific page if the user country is not allowed */
protected function displayRestrictedCountryPage() {
}
}
Any body have idea how to over write core prestashop file in 1.5
I also follow this document but no way to solve.
Prestashop 1.5.x automatically merge default override file and you custom file on module::install() function. You just need to create a correct hierarchy in your module folder: modules/your_package/override/classes/controller/FrontController.php with your custom functional.
If you don't have an extension and want to add override manually - you need to change the file override/classes/controller/FrontController.php
Just remove the class_index.php under /cache folder.
It carries Presta's Class & Controller file details. Once you delete and reload the page, prestashop will automatically generate the file with latest changes.
The problem may be, that one of your functions which contains parent::nameOfFunction, You should replace parent in the function by the inheritance of the parent function you want to override.
The function you're trying to override is called in the core function init(). what you should do is to override both init() and displayRestrictedCountryPage(). Then inside the init function use Controller::init() instead of parent::init()
After override whatever you want don"t forget to delete the cache folder contents and recreate them because that lost my time for a while till found the solution here

Categories