Переопределение родительской темы функции в дочерней теме

#wordpress #function #wordpress-theming #themes #parent-child

#wordpress #функция #wordpress-тематизация #темы #родитель-потомок

Вопрос:

У меня есть некоторые функции для переопределения в моей дочерней теме, но я продолжаю получать Cannot redeclare langs_content() .

Я использовал довольно много ресурсов для устранения неполадок, и что-то, что должно быть прямым, я не могу решить

 <?php
function enqueue_parent_theme_style() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_parent_theme_style' );
function enqueue_child_theme_style() {
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri().'/style.css' );
}
//require_once( get_stylesheet_directory() . '/functions.php');
add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_style', 100 );


if ( ! function_exists( 'langs_content' ) ) {
/*
 * My Awesome function is awesome
 *
 */
        function langs_content( $atts, $content = null ) // shortcode lang
        {     
            $lang = the_lang();     
            extract( shortcode_atts( array(), $atts ) );     
            $langs_content = '';
            if(in_array($lang, $atts))
            { 
                $langs_content .= do_shortcode($content);
            }    
            return $langs_content;
        } 
        add_shortcode('lang', 'langs_content');
}


?>
  

Комментарии:

1. Это можно сделать, только если они также делают это в исходном месте, где находится функция

2. но вы можете воссоздать свою собственную function custom_langs_content(){//Whatever }add_shortcode('custom_lang', 'custom_langs_content'); и обернуть ее, if(function_exists('langs_content')){} чтобы быть уверенным, что the_lang() она все еще работает

Ответ №1:

Действительно просто! Удалите короткий init hook код, а затем добавьте свой новый 😉

Вот так :

 /******
* Inside parent functions.php (don't touch this!)
******/
function test_shortcode() {
    return "Hello World from parent theme";
}

add_shortcode('test_shortcode', 'test_shortcode');

/******
* Inside child functions.php
******/
function child_test_shortcode()
{
    return "Hello World from child theme";
}

function remove_parent_theme_shortcodes()
{
    remove_shortcode('test_shortcode');

    // Add our new function with the same shortcode
    add_shortcode('test_shortcode', 'child_test_shortcode');
}

// Remove the shortcode with init hook
add_action('init', 'remove_parent_theme_shortcodes');