Replace a function of wordpress parent theme inside functions.php - php

There is not a specific answer for this exact issue so i am going to try this.
So in the parent theme of a wordpress website there is a php file named helpers-icons.php. The exact path for this file is /wp-content/themes/parent/inc/helpers/helpers-icons.php, and the content of that file is
function get_flatsome_icon($name, $size = null) {
if($size) $size = 'style="font-size:'.$size.';"';
return '<i class="'.$name.'" '.$size.'></i>';
}
This file is then inlcuded in functions.php of that parent theme.
Now i want to override a function inside and being more specific, just this line of code
return '<i class="'.$name.'" '.$size.'></i>'; to return '<span class="'.$name.'" '.$size.'></span>';
how could i do that on child theme without messing with the php files of parent?
Thanks in advance
EDIT
EDIT 2

EDIT - Different Approach
You can copy the file /inc/helpers/helpers-icons.php into your child theme (keep the folder structure - e.g. /child-theme/inc/helpers/helpers-icons.php). That way it will be called instead of the original file and you can change the function there.
(Original Answer) Removing Functions From Hooks:
You’ll need to de-hook the parent function and hook your child theme function instead by using remove_action() and remove_filter().
The one you use will depend on whether the function is attached to an action hook or filter hook in the parent theme.
To remove a function, use:
remove_action( 'init', 'parent_function' );
However, this won’t work on its own, you will need to attach this function to a hook which will fire after the hook which the parent theme function is attached to. This is because you can’t remove the action before it’s been fired.
function child_remove_parent_function() {
remove_action( 'init', 'parent_function', [priority level] );
}
add_action( 'wp_loaded', 'child_remove_parent_function' );
NOTE: If the parent_function() was loaded using a priority level, you have to unloaded by stating the same level as well. That’s why [priority level] is in square brackets.
As you may imagine, wp-loaded is a hook that comes after init, so remove_action() can really remove the parent_function() that was loaded to init.
Now you’re free to write whatever new function you’d like.
function child_function() {
// your function
}
add_action( 'init', 'child_function' );
Source and further reading: https://obsessive-coffee-disorder.com/how-to-override-parent-theme-functions-in-wordpress/

Related

Wordpress remove action declared in parent theme in hook function

My parent theme prints inline styles in header. One I want to remove.
Inside controls.php in public function hook():
add_action( 'wp_head', array($this, 'display_customization') );
This function looks like this:
public function display_customization()
{
do_action('zoom_customizer_display_customization_css', $this->get_css_rules());
$css = zoom_customizer_get_css()->build();
if (!empty($css)) {
echo "\n<!-- Begin Theme Custom CSS -->\n<style type=\"text/css\" id=\"" . WPZOOM::$theme_raw_name . "-custom-css\">\n";
echo $css;
echo "\n</style>\n<!-- End Theme Custom CSS -->\n";
}
}
It adds inline style with id="foodica-pro-custom-css"
I tried
remove_action( 'wp_head','display_customization', 120);
and it didn't work.
The main issue above is that the action is added using the class method callback syntax
array($this, 'display_customization') so you can't remove it using directly the function name
https://gist.github.com/tripflex/c6518efc1753cf2392559866b4bd1a53
Here you can find an implementation of remove_class_action and remove_class_filter that allow you to remove actions and filters that have been created using the class syntax.
Once you have the above code in your project using the following syntax:
remove_class_action('wp_head', 'NameOfClassThatThisRefersToInControlsFile', 'display_customization');
Some other things to note:
remove_action and remove_class_action from above need to have the same signature as add action, so please keep the priority the same. If the add call has no priority as above (so it has the default priority of 10), please keep the remove call the same.
Another issue that you could encounter here is to make sure the remove_action or remove_class_action calls are in fact called after the add_action.
Depending on where the add_action is called, you might have to put your remove call inside a specific hook, to make sure the remove call is executed after the add call.

How do I unhook WordPress action hook in plugin file?

I am trying to unhook and modify an action from within my child themes functions.php file.
The WordPress plugin Sensei, adds this action in line 86 of this document.
https://github.com/Automattic/sensei/blob/master/includes/class-sensei-modules.php#L86
The action references a function further down the page that is responsible for outputting a dynamic header element.
/**
* Show the title modules on the single course template.
*
* Function is hooked into sensei_single_course_modules_before.
*
* #since 1.8.0
* #return void
*/
public function course_modules_title( ) {
if( sensei_module_has_lessons() ){
echo '<header><h2>' . __('Modules', 'woothemes-sensei') . '</h2></header>';
}
}
My goal here is to change the html currently output as 'Modules' to something else.
I have tried to the following in my child themes functions.php file but neither seem to be working.
remove_action( 'sensei_single_course_modules_before', array( 'Sensei_Core_Modules', 'course_modules_title' ), 20);
remove_action( 'sensei_single_course_modules_before', array( 'Sensei()->Sensei_Core_Modules', 'course_modules_title' ), 20);
The issue is, I do not know how to determine which initial parameter, to add to the array to call the correct class. Because I am accessing it externally I cannot use $this like it is being used in the core file.
In order to remove the action you have to find the instance of the class. This is an assumption since I don't have access to the source code of Sensei but big chance there is one since most WordPress plug-ins use this method.
When you find the instance name you can load it using global $senseiInstance - replace this with the actual name of the variable.
Then you can remove the action using for example this code:
remove_action( 'sensei_single_course_modules_before', array( $senseiInstance, 'course_modules_title' ), 20);
More information can be found in for example this article: https://www.sitepoint.com/digging-deeper-wordpress-hooks-filters.
Hope this helps you out!

Override function (with hook) in wordpress

I have this situation:
parent theme:
functions.php
require_once(dirname(__FILE__) . "/includes/theme-functions.php");
require_once(dirname(__FILE__) . "/includes/theme-hooks.php");
theme-function.php
function name_of_this_function() {
// DO SOMETHING
}
add_action ('hook','name_of_this_function');
theme-hooks.php
function hook() {
do_action('hook');
}
How can I override that action in my child functions.php given that mine is called before? I tried:
remove_action ('hook','name_of_this_function');
but of course this return false
I came to an answer after many attempts (miserably failed) and a lot of reading of documentation online.
The solution resides in the "after_setup_theme" hook.
Before I was adding my actions "on top" of the parent's, meaning that i had two headers, two footers and so on. Of course the problem was that my functions.php was not able to remove something that was not added yet.
This was caused by wordpress behavior that runs functions.php of the child before the one of the parent theme.
The solution to this problem is to add a function after the setup of theme with a priority that force it to runs after the one of the parent. In this way ALL functions of the parent would be initialized (the after setup of parent is called as last function) and I would be able to remove some actions.
This is the syntax:
add_action('after_setup_theme', 'my_setup', 3);
function my_setup(){
/* CUSTOM FOOTER */
remove_action('footer_hook','parent_site_info',99);
add_action('footer_hook','site_info',99);
}
That solved the problem.

Override metaboxes from parent theme in child theme in WordPress

I have a parent theme in WordPress that adds many functions and features into WordPress, including several metaboxes.
In my child theme, I have added some new custom post types into WordPress, so I am trying to modify the meta boxes to include them in the new custom post types.
Here is the parent theme functions.php file: http://pastebin.com/G1CVM6U2
My thought was that I would simply need to add code into my child theme that would remove the action from the parent theme that adds in the metaboxes, and then copy the metabox files into my child theme, modify them as needed, and then use add_action to pull in a new function that calls those metabox files.
However, I am having a hell of a time even being able to remove the action from the parent theme.
I have tried several variations of the following in my child theme functions.php:
remove_action('init', 'add_metaboxes');
remove_action('add_meta_box', 'add_metaboxes');
It does not remove the metaboxes though, as when I check the page/post editor, they are still visible.
Without running the remove_action, I cannot then do a new "add_action" to call in the modified metabox files. When I try to do so, I get a white screen of death due what I guess is the same function being redeclared.
I suspect that because the action in the parent theme is being called as part of a class, the process of over-riding this in the child theme might be differet?
Would appreciate some help on this one.
Sample 1: Remove a normal named add action
add_action('after_setup_theme', 'remove_meta_boxes');
function remove_meta_boxes(){
remove_action('add_meta_box', 'add_metaboxes', 10);
}
Sample 2: remove a function passed by reference
the add_action looks like this:
class ab{
function __construct(){
add_action('init', array( &$this, 'add_metaboxes' ));
}
}
$x=new ab;
The problem here is that wordpress will prepend a random identification string to the function name e.g. (a3898orj34930add_metaboxes), this is random every time so what we need to do is use strpos to find the name in the key. I have created a custom function for this.
function remove_anon_filters( $name, $functionname, $priority){
global $wp_filter;
foreach ( $wp_filter[$name][$priority] as $key => $data) {
if( stripos($key, $functionname ) ){
remove_action( $name, $key, $priority );
}
}
}
We than then hook into after theme set up to remove the parent theme action.
add_action('after_setup_theme', 'remove_parent_meta_boxes');
function remove_parent_meta_boxes(){
remove_anon_filters ( 'init', 'add_metaboxes', 10);
}

Wordpress filter not being added

I have a plugin that uses apply_filters like this:
$additional_fields = apply_filters('attachment_meta_add_fields', $additional_fields);
In my theme's functions.php, I do:
function addAttachmentMeta($additionalFields) {
return $addtionalFields;
}
add_filter( 'attachment_meta_add_fields', 'addAttachmentMeta', 1, 1 );
But the function addAttachmentMeta never runs.
How can I alter my apply or add filter statements to make it so that addAttachmentMeta gets called?
Edit:
This is a custom plugin that I wrote based off tutorials on how to add additional attachment meta fields. The whole source is here: http://pastebin.com/7NcjDsK5. As I mentioned in the comments, I know this is running and working because I can add additional fields in this plugin file, but not by using the filters because the filter doesn't get added.
I can see var_dumps before and after the apply_filters statement, but the function I've pointed to with add_filter never gets called.
According to the order WordPress' core loads, function.php gets called after all plugins are loaded and executed.
You need to make sure the apply_filters() in your plugin runs AFTER your add_filter() is called. Otherwise at the point where your filters are 'applied', add_filter() simply hasn't been called yet.
What you could do is use a hook to make that part of your plugin run after functions.php has loaded. You could use the add_action('after_setup_theme', 'function_name') hook.
Wrap the last three lines of your plugin file inside a function and execute it after functions.php runs.
function addAttachmentMeta() {
$additional_fields = array();
$additional_fields = apply_filters('attachment_meta_add_fields', $additional_fields);
$am = new Attachment_Meta( $additional_fields );
}
add_action('after_setup_theme', 'addAttachmentMeta');

Categories