Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override a function with child themes functions.php

As the title reads I'm trying to modify a function called by a parent theme in my child, I know that the child theme is set to be loaded beforehand so I'm curious if this is even possible?

My parent theme has a function called ajax_search_box() that I'd like to modify a query in, and I'd rather not modify the parent theme files in case I need to update it down the road.. what would be the best way to do this?

Also, for bonus points, how would I go about doing this with a widget as well? Thanks in advance!

function SearchFilter($query) {
    // If 's' request variable is set but empty
    if (isset($_GET['s']) && empty($_GET['s']) && $query->is_main_query()){
        $query->is_search = true;
        $query->is_home = false;
    }
    return $query;
}

add_filter('pre_get_posts','SearchFilter');

function ajax_search_box() {
    if (isset($_GET["q"])) 
    {   
        $q = $_GET["q"];        
        global $wpdb;           
        $q = mysql_real_escape_string($q);
        $q = $wpdb->escape($q);

        $query = array(
            'post_status' => 'publish',
            'order' => 'DESC',
            's' => $q
        );

        $get_posts = new WP_Query;
        $posts = $get_posts->query( $query );

        // Check if any posts were found.
        if ( ! $get_posts->post_count )
            die();

        //Create an array with the results
        foreach ( $posts as $post )
            echo $post->post_title . "|" . $post->ID . "\n";
    }   
    die();
}
// creating Ajax call for WordPress
add_action( 'wp_ajax_nopriv_ajax_search_box', 'ajax_search_box' ); 
add_action( 'wp_ajax_ajax_search_box', 'ajax_search_box' ); 
like image 384
Klikster Avatar asked Sep 26 '13 22:09

Klikster


People also ask

How do I override the parent theme function in a child theme?

Or, you can override an entire function defined in parent theme if it is assigned as a callback to a hook. You remove this filter hook in the child theme functions. php as: add_action( 'init', function() { remove_filter( 'the_content', 'append_thank_you' ); } );

How do I override a functions php in WordPress?

You can override a pluggable function in your parent theme by writing a function with the same name in your child theme. You can unhook a function from the parent theme using a function with remove_action() or remove_filter() in your child theme and then writing a new function.


2 Answers

the parent theme needs to check if(function_exists('ajax_search_box')) and if it doesn't exist then it will declare it.

If the parent theme checks to see if the function exists, then you can declare it first and have it do what you want.

If the parent theme does not check, get in touch with the theme author to see if they will throw that change in for the next update....and code it yourself too. That way when the theme updates then you will still be good to go.

like image 100
Adam Erstelle Avatar answered Sep 28 '22 08:09

Adam Erstelle


Break free of functions.php, write your own plugin.

<?php
/** 
 * Plugin Name: Manipulate the Parent 
 * Requires: PHP5.3+
 */

add_action( 'after_setup_theme', function()
{
    remove_filter( 'pre_get_posts','SearchFilter' );    
    // now add your own filter
    add_filter( 'pre_get_posts', 'your_callback_for_your_filter' ); 
});

function your_callback_for_your_filter()
{
     // do stuff
}
like image 43
brasofilo Avatar answered Sep 28 '22 10:09

brasofilo