• Resolved nicoter

    (@nicoter)


    Hi,

    There’s a pluggable function like this in the parent theme:

    <?php
    if ( ! function_exists ( 'my_function' ) ) {
        function my_function() {
            // Contents of the function
        }
    }
    ?>

    I’m trying to redeclare this function with a custom code snippet as follows:

    <?php
    function my_function() {
        // Contents of the function
    }
    ?>

    Unfortunately I get:

    The code snippet you are trying to save produced a fatal error on line 89:
    Cannot redeclare my_function() (previously declared in /redacted/wp-content/themes/parenttheme/inc/blog/blog-config.php:100)

    However when I add the function to the functions.php of the child theme, it works (as intended).

    Why is it not possible to redeclare functions with this plugin? It defeats the whole purpose of the plugin when I have to start adding code to the functions.php

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • It works in this case according to generic principles. Using child theme, WordPress works differently because it read function from the child theme first. Apparently WordPress unset original function.

    Redeclaring function with Code Snippet just cause error because WordPress doesn’t handle code snippets as a child theme. Because it doesn’t hanlde Code Snippet like a child theme, redeclaring the same function of course cause error.

    Indeed Code Snippet does not always check if function exists correctly. It might not find your own function in another snippet. It might accept usage of a function in a situation, where some function is not available.

    Code Snippet is fine plugin, but it does have some bug and it can’t completely replace usage of a child them.

    1. Redeclaring the same function, you must use a child theme.
    2. Changing templates you must use child theme, if you want to avoid overwriting your changed templates, if some plugin is updated.

    In fact I would need a child theme for the latter reason. I have never needed a child theme for the first reason. I have used available hooks (add_filter and add_action) and shortcodes togethere with my own functions. Try to use available hooks or create shortcodes, which do the desired result.

    Have you tried these functions:

    https://www.php.net/manual/en/function.runkit-function-remove.php

    or

    https://www.php.net/manual/en/function.runkit-function-redefine.php

    Maybe WordPress use these when you use a child theme. Because Code Snippet is not a way to create a child theme, you must do that by yourself.

    • This reply was modified 6 years, 7 months ago by tapiohuuhaa.
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Cannot redeclare pluggable function of parent theme’ is closed to new replies.