0

How can I replace a function from a plugin with a custom version of it.

The original function of the plugin is:

function sep_get_the_event_end_date($post = NULL) {
    $post = get_post($post);
    if ($post->post_type !== 'event_listing') {
        return;
    }
    $event_xml = get_post_meta(get_the_ID(), 'event_meta', TRUE);
    $xml_object = new SimpleXMLElement($event_xml);
    $end_date = isset($xml_object->end_date) ? $xml_object->end_date : '';
    $event_date = isset($xml_object->event_date) ? $xml_object->event_date : '';
    if (isset($end_date) and ''  $end_date and 'checked'  $event_date) {
        $end_date = isset($xml_object->end_date) ? $xml_object->end_date : '';
        $end_date = new DateTime($end_date);
        $end_date = date_format($end_date, 'F d, Y');
    } else {

        $end_date = '';
    }
    return apply_filters('sep_the_event_end_date', $end_date, $post);
}

I want to change the date format of $end_date. Something like 'jS M Y'.

I tried this:

Created a new file: custom-date-planner.php and changed the date format.


    function custom_sep_get_the_event_end_date($post = NULL) {
        $post = get_post($post);
        if ($post->post_type !== 'event_listing') {
            return;
        }
        $event_xml = get_post_meta(get_the_ID(), 'event_meta', TRUE);
        $xml_object = new SimpleXMLElement($event_xml);
        $end_date = isset($xml_object->end_date) ? $xml_object->end_date : '';
        $event_date = isset($xml_object->event_date) ? $xml_object->event_date : '';
        if (isset($end_date) and ''  $end_date and 'checked'  $event_date) {
            $end_date = isset($xml_object->end_date) ? $xml_object->end_date : '';
            $end_date = new DateTime($end_date);
            $end_date = date_format($end_date, 'd M, Y');
        } else {

            $end_date = '';
        }
        return apply_filters('custom_sep_the_event_end_date', $end_date, $post);
    }

Next in my functions.php I did this:

// Add file
require_once(get_stylesheet_directory() . '/public/partials/custom-date-planner.php');

add_action( 'after_setup_theme', 'child_theme_setup', 100 );
function child_theme_setup() {

    remove_action( 'sep_the_event_end_date', 'sep_get_the_event_end_date' );

    add_action( 'sep_the_event_end_date', 'custom_sep_get_the_event_end_date' );
}

With this running, I get the notice: Notice: Trying to get property of non-object in /private/var/www/fashion/data/web/public/wp-content/themes/furnde-child/public/partials/custom-date-planner.php on line 4

How can I get this to work?

2

For your question how can I replace this function . You don't need to replace it. The function has a filter on the returns value, not an action. You don't need to remove it, just filter the result.

So you can create a new function and call it with add_filter('sep_the_event_end_date', 'yourfunction'); Put this in your functions.php file without the add_action plugin _loaded.

0

In the function that you are using as a replacement. You don't get the post data, or did you just left that out?

post = get_post($post);
  • Hi Bas, my bad, I didn't paste the complete function. I have edited the code. Including the post data. Does it make more sense now? – Pierre Nov 5 '16 at 17:13
0

It seems the $post parameter isn't always correct.

Maybe a double check.

Change this:
if ($post->post_type !== 'event_listing') {
Into this:
if ( ! isset($post->post_type) || $post->post_type !== 'event_listing' ) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.