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?
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
WordPress process.
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 :
Edit 1: The following may be more suitable even if this specific query is hard to target.
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 theWHERE
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.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
anddo_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 thewpdb
or thepostmeta
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.
If you know the name of the WP function you want to replace, put something like this in your theme’s functions.php file:
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.]
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…)