How do I overwrite WordPress core functions?

This is my first time touching WordPress, and I have the task of extracting the core functions that have been customized and putting them in a non-core file so that it’s more maintainable when we upgrade. Here’s an example of one of the methods in wp-admin/includes/template.php :

Original code:

Read More
    function meta_form() {
        global $wpdb;
        $limit = (int) apply_filters( 'postmeta_form_limit', 30 );
        $keys = $wpdb->get_col( "
            SELECT meta_key
            FROM $wpdb->postmeta
            GROUP BY meta_key
            HAVING meta_key NOT LIKE '_%'
            ORDER BY meta_key
            LIMIT $limit" );
        if ( $keys )
            natcasesort($keys);
    ?>

Customized version:

function meta_form() {
    global $wpdb;

  if ( isset($_GET['post']) )
    $post_id = (int) $_GET['post'];
  elseif ( isset($_POST['post_ID']) )
    $post_id = (int) $_POST['post_ID'];
  else
    $post_id = 0;

  if ( $post_id ) {
    $post_ = get_post($post_id);

  }

  if ($post_->post_type == 'video_photo' ){
    $limit = (int) apply_filters( 'postmeta_form_limit', 30 );
    $keys = $wpdb->get_col( "
        SELECT meta_key
        FROM $wpdb->postmeta
    where meta_key like 'tqmcf_%'
        GROUP BY meta_key
        HAVING meta_key NOT LIKE '_%'
        ORDER BY meta_key
        LIMIT $limit" );
  }else{
    $limit = (int) apply_filters( 'postmeta_form_limit', 30 );
    $keys = $wpdb->get_col( "
        SELECT meta_key
        FROM $wpdb->postmeta

        GROUP BY meta_key
        HAVING meta_key NOT LIKE '_%'
        ORDER BY meta_key
        LIMIT $limit" );
  }

    if ( $keys )
        natcasesort($keys);
?>

Where exactly would I define my meta_form() function to make sure it overwrites the core method?

Related posts

Leave a Reply

4 comments

  1. In WordPress you can’t owerride the whole core. You have two ways to modify the WordPress Core :

    First option : Pluggable functions

    Some functions can be redefined. This can be done in theme’s functions.php file or in a plugin file. You can check the list of the Pluggable Functions.

    Second option : Filter and Action hooks

    • Filter hooks can be used to modify vars inside the core
      WordPress process.
    • Action hooks can be used to fire custom
      function on some events.

    Notice 1 : you can create your own filter and action hooks in your theme or plugin.

    Notice 2 : you can use filter hooks to fire a function if you don’t find a convenient action hook.

    If you follow the WordPress coding standards, you should be able to deeply modify the behaviour of WordPress.

    For the function your showing, you should look for the post_where filter hook, to do something like this :

    add_filter( 'posts_where' , 'my_posts_where' );
    
    function my_posts_where( $where ) {
    
        global $post;
    
        if ($post->post_type == 'video_photo' ){
            $where .= " AND meta_key like 'tqmcf_%'";
        }
    
        return $where;
    }
    

    Edit 1: The following may be more suitable even if this specific query is hard to target.

    add_filter( 'query' , 'my_meta_form_query' );
    
    function my_meta_form_query( $query ) {
    
        $pattern = "/SELECT(?:W*)meta_key(?:W*)FROM (.*)(?:W*.*?)*LIMIT(?:W*)([0-9]*)/g";
    
        if( preg_match($pattern, $query, $vars) ) {
            $postmeta = $vars[1];
            $limit = $vars[2];
    
            $query = "SELECT meta_key
                    FROM $postmeta
                    WHERE meta_key like 'tqmcf_%'
                    GROUP BY meta_key
                    HAVING meta_key NOT LIKE '_%'
                    ORDER BY meta_key
                    LIMIT $limit";
        }
    
        return $query;
    }
    

    You have to digg into the code to find suitable hooks and how you can best filter variables. In this example, we check if the query is matching the one in the meta_form() function. We extract the existing query vars and we build a new query including the WHERE condition. I didn’t test this piece of code and there may be some bugs but it can give you an idea of how we alter the core code.

  2. There’s no replacing core functions, unless they’re defined in wp-includes/pluggable.php.

    You can modify values and processes using hooks where available (you’ll find them mainly by apply_filters and do_action functions in core files). IMO, there are more specialists and knowledge base at WordPress Answers, but I suggest focusing on the end result because you could hook on the wpdb or the postmeta queries, for example.
    In you sample code, it’s not clear what you’re trying to achieve, but the only filter hook available doesn’t seem adequate, hence the need of other hook and clear objective.

    In the end of the day, the proper way of modifying WordPress behavior is through the Plugin API.

  3. If you know the name of the WP function you want to replace, put something like this in your theme’s functions.php file:

    function my_new_function () {
    /* Your replacement code.... */
    }
    
    remove_filter (the_content, old_wp_function);
    add_filter (the_content, my_new_function);
    

    Where ‘the_content’ is the post’s entire content – so you could use this, for example, to replace WordPress’s wptexturize() function (which changes, among other things, ordinary quotes to ‘smart’ quotes) with your own.  [Or, if you simply don’t want to use the WP function at all, just use the remove_filter statement above.]

  4. Why not just edit the core file and just add if(!function_exists…)
    then copy and edit the functions into you funcitons.php file.

    Then when you upgrade you will receive errors of all the functions that are declared twice but will be easy enough to go and ad if(!function_exists…)