0

Is there a way to load a file (copyright.php) functions & variable's value into child theme's functions.php

I'm trying to pass the value in order to change the footer credit. I can do it if I included the code directly within functions.php, but I'm trying to exclude the code to a different file so that my client can change the variables value easily in the future without touching functions.php. Below are my codes but it's not working:

copyright.php (within Child Theme folder)

function display_company_url() {    
global $company_url;
$company_url = 'www.mycompany.com';
return $company_url;
}

function display_company_name() {   
global $company_name;
$company_name = 'MY COMPANY';
return $company_name;
}

functions.php (Child Theme)

add_action( 'wp_enqueue_scripts', 'my_assets' );)
function my_assets() {
wp_register_script( 'copyright', get_stylesheet_directory_uri() . '/copyright.php' );
wp_enqueue_script( 'copyright' );   
}


// REMOVE AND MODIFIED PARENT'S FOOTER
add_action( 'init', 'remove_parent_footer', 10);
function remove_parent_footer() {
remove_action('parent_footer', 'parent_footer_credits', 8);


add_action('parent_footer', 'child_footer_credits', 8); 
function child_footer_credits() {
    ?>
        <div class="site-info col-md-6">
            <a href="<?php echo esc_url( __( '/login', 'childtheme' ) ); ?>" rel="nofollow"> <?php echo "Copyright" ?></a> &copy; <?php echo date("Y"); ?>
            <?php echo get_bloginfo( 'name' ) . ". All rights reserved."; ?> <br> <?php echo "Powered by"; ?>
            <a href="<?php echo esc_url( __( display_company_url(), 'childtheme' ) ); ?>" rel="nofollow"><?php echo display_company_name(); ?></a>
        </div><!-- .site-info -->
    <?php
}

}

The footer credit should be showing:

Copyright © 2017 BLOGNAME. All rights reserved.
Powered by MY COMPANY

  • If you want your client can change copyright content in future easily then best option would be placing these contents in dashboard -> Add pages and call that page in footer.php. This way client can change contents most easily. – Rishabh May 5 '17 at 6:58
2

I would suggest to use the WordPress Customize API. There you can set as many fields as you want and all will be nicely shown in the theme's Customize menu.

<?php
/* New Section in Customize */
add_action( 'customize_register','new_customize_option');
function new_customize_option( $wp_customize ) {

    /* --------------REGISTERING NEW SECTION --------------- */


    $wp_customize->add_section( 'rewp_logo' , array(
    'title'      => __( 'Copywrite fields', 'textdomain' ),
    'priority'   => 260,
    ) );

    /* -------------- S E T T I N G S --------------- */


    /* Setting: Set a text field   */
    $wp_customize->add_setting( 'rewp-text' , array(
    'default'     => 'Copywrite 2017',
    'capability'  => 'edit_theme_options',
    ) );

    /* -------------- C O N T R O L S --------------- */

    /* Control: Upload Background */
    /* Control: Set a link */
    $wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'rewp-text', array(
    'label'     => __( 'Copywrite text', 'textdomain' ),
    'section'   => 'rewp_logo',
    'settings'  => 'rewp-text',
    'type'      => 'text',
    'priority'  => 1
    ) ) );

} ?>

Then you can use it like:

$mods = get_theme_mods();
echo $mods['rewp-text'];

The suggestion of @Rishabh will also work but mind that if this page gets deleted by accident then you will need to create a new one and edit the code again.

  • 1
    Thanks! It's working. Your suggestion solved my problem. – Caracos May 5 '17 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.