1

Good evening,

I had some modifications in functions.php file to complete on my current wordpress website development.

I have completed new code within the file and uploaded my edited function file to child theme. The code simply replaces the metadata word "new" with "new with tags" on an ad in a classified ad website.

After doing so, the changes i have made are not taking placement of parent functions.php file.

Is there something specifically i must do in my child functions file to overwrite the parent?

Please see following code. (Child theme -> functions.php)

<?php
    add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles', 12 );
    function theme_enqueue_styles() {
        wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    }

    function adifier_custom_meta() {
        $meta_boxes = array();

        $advert_details = array(
            array(
                'id'                => 'advert_cond',
                'name'              => esc_html__( 'Condition', 'adifier' ),
                'type'              => 'select',
                'options'           => array(
                '0' => esc_html__( '-Select-', 'adifier' ),
                '1' => esc_html__( 'New with tags', 'adifier' ),
                '2' => esc_html__( 'New with tags', 'adifier' ),
                '3' => esc_html__( 'Used with tags', 'adifier' ),
                '4' => esc_html__( 'For Parts Or Not Working', 'adifier' ),
            ),
            'values_callback'   => 'adifier_get_advert_meta',
            'save_callback'     => 'adifier_save_advert_meta',          
        ),
    );
?>
  • The file name is functions.php, not function.php (plural, not singular). – Fayaz Aug 11 '18 at 22:59
  • Error typing here. Does not resolve my problem. Thank you. – James Aug 11 '18 at 23:24
  • What is your code exactly? Is it a filter? Is there a priority? The child theme functions.php file loads before the parent. – Milo Aug 11 '18 at 23:38
  • @Milo please see edited question including code from functions.php file in child theme. – James Aug 11 '18 at 23:51
  • @James is this your full code ? if so you are not hooking or calling your new function – kashalo Aug 12 '18 at 0:25
0

so James in order to achieve your goal you need to remove the meta-box and re-add it with the new callback that contain the new value;

so lets assume originally you have the meta-box added as below :

function adifier_custom_meta()
{
    $screens = ['page', 'post'];
    foreach ($screens as $screen) {
        add_meta_box(
            'advert_cond', // Unique ID
            esc_html__('Condition', 'adifier'), // Box title
            'adifier_get_advert_meta', // Content callback, must be of type callable
            $screen, // Post type,
            'side'
        );
    }
}
add_action('add_meta_boxes', 'adifier_custom_meta');

and the original call-back output as following :

function adifier_get_advert_meta($post)
{
    $value = get_post_meta($post->ID, '_advert_cond_meta_key', true);
    ?>
    <label for="advert_cond">Description for this field</label>
    <select name="advert_cond" id="advert_cond" class="postbox">
        <option value="0"><?php echo esc_html__('-Select-', 'adifier') ?></option>
        <option value="1" <?php selected($value, '1');?>><?php echo esc_html__('New with tags', 'adifier') ?></option>
        <option value="1" <?php selected($value, '2');?>><?php echo esc_html__('New with tags', 'adifier') ?></option>
        <option value="3" <?php selected($value, '3');?>><?php echo esc_html__('Used with tags', 'adifier') ?></option>
        <option value="4" <?php selected($value, '4');?>><?php echo esc_html__('For Parts Or Not Working', 'adifier') ?></option>

    </select>
    <?php
}

in order to change above we need to add the following functions:

add_action('add_meta_boxes', 'adifier_custom_meta2');
function adifier_custom_meta2()
{
    $screens = ['page', 'post'];
    remove_meta_box('advert_cond', $screens, 'side');
    add_meta_box(
        'advert_cond', // Unique ID
        esc_html__('Condition', 'adifier'), // Box title
        'adifier_get_advert_meta2', // Content callback, must be of type callable
        $screens, // Post type,
        'side'
    );
}

and now we will add our new call-back output with our new values :

function adifier_get_advert_meta2($post)
{
    $value = get_post_meta($post->ID, '_advert_cond_meta_key', true);
    ?>
    <label for="advert_cond">Description for this field</label>
    <select name="advert_cond" id="advert_cond" class="postbox">
        <option value="0"><?php echo esc_html__('-Select-', 'adifier') ?></option>
        <option value="1" <?php selected($value, '1');?>><?php echo esc_html__('New', 'adifier') ?></option>
        <option value="2" <?php selected($value, '3');?>><?php echo esc_html__('Used', 'adifier') ?></option>
        <option value="3" <?php selected($value, '4');?>><?php echo esc_html__('For Parts Or Not Working', 'adifier') ?></option>

    </select>
    <?php
}

for more information you can check WordPress Doc from the following links:

Custom Meta Boxes

Add Meta Box

  • This explanation is fantastic. I almost understand it. May i have some clarification on the addition of function above the new call-back. I understand the call back but the function is a little confusing. Must i find where the function is being called in the parent, copy it, make adjustments to suit new call-back array and place that in my child functions.php also? If the parent is an array of meta and is all being called by function addified_function_meta(), would this also be called in the function.php file? – James Aug 12 '18 at 16:35
  • @James if you copy the function and just put it in your child theme with same name of previous one you will have an error that you can't declare same function twice so that's why we have to remove that specific meta box and add it again in order to have different function name ( callback) – kashalo Aug 12 '18 at 16:41
  • Now i understand. Thank you for taking the time. Very much appreciated. – James Aug 12 '18 at 21:06
  • @James you are most welcome – kashalo Aug 12 '18 at 21:20

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.