WordPress child template – functions.php: redeclareing functions

I have a site that was already created (based on: wordpress, biznex template), now I have to recreate it using a child template. The template doens’t had a child template, and I created it, and now I have to migrate all the custom stuff into the child template.

The issue I have is that there are some modified functions in the base templates functions.php file that I need to put in the child tempalte’s functions.php, but it gives me the next error:

Read More

Fatal error: Cannot redeclare biznex_contact_ajax() (previously
declared in
C:xampphtdocsenetix-clean-tempwp-contentthemesbiznex-childfunctions.php:16)
in
C:xampphtdocsenetix-clean-tempwp-contentthemesbiznexfunctions.php
on line 883

How can I make the child-themes functions.php to override the base templates function without deleting them from the base templates functions.php.

Here is one of the functions for to use as a concret exmaple:

function biznex_contact_ajax(){

    $receiver_mail = get_bloginfo('admin_email'); 
    if(!empty($receiver_mail))
    {   
        $mail_title_prefix = _go('email_prefix');
        $mail_prefix_cont = sprintf( __('This e-mail was sent from a contact form on %1$s %2$s.'),(get_bloginfo('name')),('( '.get_site_url().')'));
        $mail_prefix = '</br></br>' .$mail_prefix_cont;
        if(empty($mail_title_prefix))
            $mail_title_prefix = '';
        if( !empty($_POST['biznex-name']) && !empty($_POST['biznex-email']) && !empty($_POST['biznex-subject']) && !empty($_POST['biznex-message']) ){
            $subject = $mail_title_prefix.$_POST['biznex-subject'];
            $reply_to = is_email($_POST['biznex-email']);
            if(false!==$reply_to){
                $reply_to = $_POST['biznex-name'] . '<' . $reply_to . '>';
                $headers = '';
                $headers .= 'MIME-Version: 1.0' . "rn";
                $headers .= 'Content-type: text/html; charset=utf-8' . "rn";
                $headers .= 'From:'.$_POST['biznex-name'].'<'.$_POST['biznex-email']."> rn";
                $headers .= 'Reply-to: ' . $reply_to . "rn";
                if ( wp_mail($receiver_mail, $subject, $_POST['biznex-message'] .$mail_prefix , $headers) )
                    $result = __("Message successfully sent. Thank you!");
                else
                    $result = __("Operation could not be completed.");
            }else{
                $result = __("You have provided an invalid e-mail address.");
            }
        }else{
            $result = __("Please fill in all the required fields.");
        }
    }else{
        $result = __('Error! There is no e-mail configured to receive the messages.');
    }
    echo $result;
    die;

}
add_action( "wp_ajax_biznex_contact", "biznex_contact_ajax" );
add_action( "wp_ajax_nopriv_biznex_contact", "biznex_contact_ajax" );

Related posts

1 comment

  1. Unfortunately you cannot have 2 functions with the same name in php.

    But wordpress is written to allow developers to hook into actions / filters. If you study the load process you will see wp loads some functions, then active theme (child theme first + parent if relevant). This allows developers to create code and run later in the process without modifying core code. Ajax hooks are an example of these.

    So you can of course remove these hooked actions if you know the hook used and function name. But if you are in a child theme, you need someway to remove the action after the parent theme has loaded (be careful some themes may also use hooks to define functions later in the process when more wp functions are available) and in this / most instances, init is a good hook to use.

    Hence, hook a function onto init to remove the functions, and add your custom function.

     function change_ajax_funct(){
        remove_action( "wp_ajax_biznex_contact", "biznex_contact_ajax" );
        remove_action( "wp_ajax_nopriv_biznex_contact", "biznex_contact_ajax" );
        // note leave action hook the same, just change the function call this means that you don't have to change the js code to use a new hook.
        add_action( "wp_ajax_biznex_contact", "biznex_contact_ajaxb" );
        add_action( "wp_ajax_nopriv_biznex_contact", "biznex_contact_ajaxb" );
    
     }
    
     add_action('init', 'change_ajax_funct');
    

Comments are closed.