Override metaboxes from parent theme in child theme in WordPress - php

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);
}

Related

Replace a function of wordpress parent theme inside functions.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/

I Want to Assign Multiple Roles to a single user

I've found below two lines to be inserted somewhere in our theme to get the job done:
$addMemberToGroup = new WP_User($required_id);
$addMemberToGroup->add_role( $role );
But I dont know where to place these lines.
Does anybody know where exactly to put those?
On which action you want to assign user role, generally custom code need to put in theme's function.php file (if child theme then put in child theme's function.php file) but it all depends on your requirement.
$addMemberToGroup = new WP_User($required_id);
$addMemberToGroup->add_role( $role );
Put these lines to your theme functions.php
Create a function:
function add_new_role_to_user( $role ) {
$addMemberToGroup = new WP_User($required_id);
$addMemberToGroup->add_role( $role );
}
After that you need to hook this function
add_action( 'admin_init', 'add_new_role_to_user' );

Wordpress Overwrite add_menu_page from Child theme?

How to Overwrite the add_menu_page function from Child Theme. my code is below
add_action('admin_menu', 'goal_settings_page');
function goal_settings_page() {
add_menu_page(
__('my menu'),
__('my menu'),
'manage_options', 'settings',
'sake_settings_page');
}
function sake_settings_page() {
// parent theme code here
}
I want to Overwrite sake_settings_page function from Child theme How to overwrite that function please help.
Thanks
First, Add/update you function to your parent theme like below
if ( ! function_exists ( 'sake_settings_page' ) ) {
function sake_settings_page() {
// Contents of your function here.
}
}
Then put the same function(which you want to override) to your child theme.
Note : if you enclose the functions in your parent theme in a conditional tag like this, WordPress will check if there is a function with the same name in your child theme that's already been run, and if that's the case it won't run the function from the parent theme.
WordPress will run the function in the child theme first, and when it comes to the one in the parent theme, it'll check if it already exists and because it does, it won't run it.

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.

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