How do I overwrite WordPress core functions?

wordpress override plugin function in child theme
override storefront template functions
wordpress pluggable functions
wordpress child theme override function
php override function
how to override theme in wordpress
wordpress actions
wordpress hook order

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:

    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?

How do I overwrite WordPress core functions?, Is there a way to override woocommerce core functions in functions.php ? for example file location in: wp-content/plugins/woocommerce/includes/class-wc- order-  There are two well-documented WordPress functions, one called wp_register_style() and the other wp_enqueue_style() that allows us to do this and it can be added either to parent or child theme functions.php file in order tell WordPress to look for it and load it in the site header.

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.

Woocommerce: How to override core functions in functions.php , Overriding the function could cause un-expected issues when updating. like to know if I can override functions from outside the core, in an external plugin. In this article, we’ll show how you can overwrite an existing site using a package created with Duplicator Pro. Note that packages created with Duplicator Lite do not support overwriting of sites and would require some manual steps not shown here. Companion video showing how to overwrite a site using the Duplicator Pro.

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...)

How to override plugin functions?, <span></span> <nav id=”site-navigation” class=”main-navigation” role=” navigation”> I want to override that function into my child theme. I have made a php file  I only want to overwrite one or two functions from these classes. For example and in particular I want to overwite some functions of the admin order create class: class Mage_Adminhtml_Model_Sales_Order_Create extends Varien_Object implements Mage_Checkout_Model_Cart_Interface . From my limited knowledge in this area, I think I have to do

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.]

How to override function in parent theme?, If you've had any experience working with parent and child themes in WordPress, you'll know that the template files in your child theme  Pluggable functions are no longer being added to WordPress core. All new functions instead use filters on their output to allow for similar overriding of their functionality. Note: A function can only be reassigned this way once, so you can’t install two plugins that plug the same function for different reasons.

A Guide to Overriding Parent Theme Functions in Your Child Theme, Copy (in full) the function you want to override from the parent theme. in /home/ idahoo5/public_html/wp-content/themes/twentyeleven/functions.php on line  1. Taking advantage of pluggable functions. Let's start with pluggable functions. What does the word pluggable actually mean? Pluggable means that the parent theme function is wrapped in a function_exists() if-condition to check whether the function already exists – if so, the function will not be executed, otherwise WordPress will run the function.

Overriding WordPress Functions, In this WordPress tutorial you will learn three practical methods of overriding parent theme functions within a custom child theme.

How to override parent theme functions in a child theme, This way the parent theme function does not override one the child theme, As you may imagine, wp-loaded is a hook that comes after init , so 

Comments
  • If you simply want to hide some meta keys in certain cases, you might be able to hook a filter onto is_protected_meta. This answer may also be relevant.
  • That method was just an example of a ton of methods that were changed.
  • hey strategio, how would I test this code? I'm not sure where I can actually see the effects of the original source code. I want to test before and after to see if it functions the same
  • Maintenance nightmare
  • we can't edit the core file because when we update wordpress your changes will be loss.so we have to only override in function.php file using add_filter Hooks.
  • I understand your guys concern but in certain cases where you cannot use filters and hooks and the core function is not pluggable you may have to write your own code and use your own functions however depending on the clients budget and other concerns my answer above does work. So if rewriting the desired functionality is more work than the client can afford, rarely it may make sense to copy the core function into your theme, make the changes you want, and then edit the core file and simply wrap the function with if(!function_exists...). On theme updates yes you will receive an error.
  • I support Allon here. Unless someone explains why WP makes lots of essential function non-overridable. See wp-includes/theme.php. Why don't those functions have if(!function_exists...) in the first place? Hooks are often just overkill/way too complicated for simple small changes.
  • also, someone looking to actually edit the core file, wrapping core function in function_exists won't work as it is loaded before theme functions. You would have to comment out the core function. Then simply comment out the function again after every update. It sounds like a nightmare but it's pretty simple.