How to override a function when isn't at functions.php
-
13-12-2020 - |
题
I am trying to override a function for a theme in the child-theme but I can't get it to work. At functions.php
on the theme I can see this require_once
line:
require_once("inc/alterna-functions.php");
And I want to override a function inside that file. I just copied the original code from alterna/functions.php
to alterna-child/functions.php
and made my changes as follow:
if (!function_exists('alterna_get_social_list')) :
function alterna_get_social_list($extra_name = '', $topbar = false, $data = null, $target = '_blank')
{
global $alterna_options;
$str = "";
$social_list = array(
array('twitter', 'Twitter'),
array('twitter_1', 'Twitter #1'), // this is new
array('twitter_2', 'Twitter #2'), // this is new
array('twitter_3', 'Twitter #3'), // this is new
array('twitter_4', 'Twitter #4'), // this is new
array('facebook', 'Facebook'),
array('facebook_1', 'Facebook #1'), // this is new
array('facebook_2', 'Facebook #2'), // this is new
array('facebook_3', 'Facebook #3'), // this is new
array('facebook_4', 'Facebook #4'), // this is new
array('google', 'Google Plus', 'google-plus'),
array('google_1', 'Google Plus #1', 'google-plus_1'), // this is new
array('google_2', 'Google Plus #2', 'google-plus_2'), // this is new
array('google_3', 'Google Plus #3', 'google-plus_3'), // this is new
array('google_4', 'Google Plus #4', 'google-plus_4'), // this is new
array('youtube', 'Youtube'),
array('linkedin', 'Linkedin'),
array('instagram', 'instagram'),
array('whatsapp', 'Whatsapp'),
array('email', 'Email', 'envelope'),
array('rss', 'Rss')
);
if ($data != null) {
foreach ($social_list as $social_item) {
if (isset($data['type']) && $data['type'] == $social_item[0]) {
if (!isset($data['url'])) {
$data['url'] = '#';
}
if (!isset($data['target'])) {
$data['target'] = '_blank';
}
$str .= '<li class="social"><a href="' . esc_attr($data['url']) . '" target="' . esc_attr($data['target']) . '"';
if (isset($data['tooltip']) && $data['tooltip'] == "yes") {
$str .= ' title="' . esc_attr($social_item[1]) . '" class="show-tooltip"';
if (isset($data['placement']) && $data['placement'] != "") {
$str .= ' data-placement="' . esc_attr($data['placement']) . '"';
}
}
$str .= '><span class="alterna-icon-' . esc_attr($social_item[0]) . '"';
if ($data['bg_color'] != "" || $data['color'] != "") {
$str .= ' style="';
if ($data['bg_color'] != "") {
$str .= 'background:' . esc_attr($data['bg_color']) . ';';
}
if ($data['color'] != "") {
$str .= 'color:' . esc_attr($data['color']) . ';';
}
$str .= '"';
}
$str .= '><i class="fa fa-' . (isset($social_item[2]) ? esc_attr($social_item[2]) : esc_attr($social_item[0])) . '"></i></span></a></li>';
}
}
} else {
foreach ($social_list as $social_item) {
if (penguin_get_options_key('social-' . $social_item[0]) != '') {
if (!$topbar) {
$str .= '<li class="social"><a title="' . esc_attr($social_item[1]) . '" href="' . esc_attr(penguin_get_options_key('social-' . $social_item[0])) . '" target="' . esc_attr($target) . '" ><span class="alterna-icon-' . esc_attr($social_item[0]) . '"><i class="fa fa-' . (isset($social_item[2]) ? esc_attr($social_item[2]) : esc_attr($social_item[0])) . '"></i></span></a></li>';
} else {
$str .= '<li class="social"><a href="' . esc_attr(penguin_get_options_key('social-' . $social_item[0])) . '" target="' . esc_attr($target) . '" ><i class="fa fa-' . (isset($social_item[2]) ? esc_attr($social_item[2]) : esc_attr($social_item[0])) . '"></i></a></li>';
}
}
}
}
return $str;
}
endif;
But it's not working since I can't see the changes on the theme. What I am doing wrong? Which is the right way to achieve this?
解决方案
The new function in your child theme's functions.php cannot override a function from the parent's functions.php file unless that function is written to be pluggable, i.e. declared with if (!function_exists('alterna_get_social_list'))
Per the documentation on Child Themes and how they inherit from child functions.php files:
Using functions.php 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.)
[https://codex.wordpress.org/Child_Themes#Using_functions.php][1]
Also, and this is super important:
Do not copy the full content of functions.php of the parent theme into functions.php in the child theme.
The only way to completely re-define a function that isn't pluggable is if it happens to be added via an action of some sort - then you can first remove the action calling the parent theme's function and add a new action that calls your new function with a different name.