Reputation: 2012
Hi I know this is probably an obvious one, but Im just wondering how do I use my own function that I would create in functions.php instead of the default one.
To explain what Ive done, I went into wp-includes/general-templates.php and changed alot of the code around that was in get_calendar
.
But upon reading more online I realized that I shouldnt have done this as as soon as the user updates to a new wordpress these lines may be overwritten.
I kept a copy of the original general-templates.php file. So im wondering how do I implement my new updated function instead of the one in general-templates.php?
Thank you
Upvotes: 0
Views: 1336
Reputation: 393
WordPress provides two different means to override default function output: pluggable functions and filters.
A pluggable function (all of which live in pluggable.php
), takes the following form:
if ( ! function_exists( 'some_function' ) ) {
function some_function() {
// function code goes here
}
}
To override a pluggable function, simply define it in your own Plugin (or in your Theme's functions.php
file, as applicable):
function some_function() {
// Your custom code goes here
}
A filter takes the following form:
function some_function() {
// function code goes here
return apply_filters( 'some_function', $output );
}
To override a filter, define a callback and add it to the filter:
function mytheme_filter_some_function( $output ) {
// Define your custom output here
return $output;
}
add_filter( 'some_function', 'mytheme_filter_some_function' );
get_calendar()
If you look in source, you will see that the output for get_calendar()
is passed through the get_calendar
filter:
return apply_filters( 'get_calendar', $calendar_output );
So you would simply write your own callback to modify $calendar_output
, and hook it into get_calendar
.
function mytheme_filter_get_calendar( $calendar_output ) {
// Define your custom calendar output here
$calendar_output = '';
// Now return it
return $calendar_output;
}
add_filter( 'get_calendar', 'mytheme_filter_get_calendar' );
Upvotes: 4