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?
You can't really "override" a function. If a function is defined, you can't redefine or change it. Your best option is to create a copy of the plugin and change the function directly. Of course you will have to repeat this everytime the plugin is updated.
In WordPress you can't owerride the whole core. You have two ways to modify the WordPress Core :
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With