Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In my theme, I used this:

<div id="brickwall" data-brickinvert="<?php echo brickAlign(); ?>">

I put the code into functions.php:

function brickAlign() {
    $a = "true";
    return $a;
}

Now in the child theme, I would like to change $a to false.

Then in child theme my html would look like:

<div id="brickwall" data-brickinvert="false">

I want to keep the function name the same, so that I won't have to copy all the files to change one variable. But of course I cannot put function with same name in child.

I tried using global, but it caused errors. I also tried to do filters but my bleeping computer was having none of that either. The reason is so I can pass a variable into my Masonry jQuery script using the html data attribute, for aligning my bricks to top or to bottom, depending on which theme I'm in, parent or child. Thanks for any advice!

share|improve this question

2 Answers 2

up vote 0 down vote accepted

You can make your function filterable:

function brickAlign() {
    $a = "true";
    return apply_filters( 'brick_align', $a );
}

Then, in your child Theme, add a filter callback:

function child_theme_filter_brick_align( $a ) {
    return 'false';
}
add_filter( 'brick_align', 'child_theme_filter_brick_align' );

Alternately, you could pass a parameter to your function:

function brickAlign( $value = 'true' ) {
    // Note: you should add some error-checking here
    $a = $value;
    return $a;
}

Then, in your Child Theme, call the function like so:

<?php brickAlign( 'false' ); ?>
share|improve this answer
    
Thanks, Chip, your first solution worked! So does the other one posted below. –  Jennifer Michelle Jan 23 '13 at 15:50

You should make the function in your main theme pluggable like this:

if ( !function_exists('brickAlign') ) {
    function brickAlign() {
        $a = "true";
        return $a;
    }
}

That way you can make a custom brickAlign() function in your child theme that overrides the parent theme function:

function brickAlign() {
    $a = "false";
    return $a;
}
share|improve this answer
    
This is enlightening. I should probably do this to all of my functions? I think I will! :) –  Jennifer Michelle Jan 23 '13 at 15:52
    
I prefer filters to pluggable functions, as they are easier to maintain. With Pluggable functions, if you make changes/tweaks to the original functions, you have to propagate those changes manually to the Child Theme - when all you want to do in the Child Theme is change one, minor value (like 'true' to 'false'). –  Chip Bennett Jan 23 '13 at 15:57
    
That makes sense. I never would have figured out to put the filter inside the function though. The 'if not' answer is very intuitive & easier to remember later, but I see your point about maintainability. –  Jennifer Michelle Jan 23 '13 at 16:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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