2

The Problem

I am trying to remove an JSON+LD block being added by a WordPress plugin called The Events Calendar.

I believe it is being added by the function called: Tribe__Events__Template__Single_Event within which the function named google_data_markup is responsible for the actual markup which I am trying to stop/prevent from being added to the HTML.

The Code

From my limited understanding of this, I need to remove this action:

add_action( 'wp_head', array( $this, 'google_data_markup' ) );.

I believe it is being called here, in Single_Event.php:

if ( ! class_exists( 'Tribe__Events__Template__Single_Event' ) ) {
    /**
     * Single event template class
     */
    class Tribe__Events__Template__Single_Event extends Tribe__Events__Template_Factory {

        protected $body_class = 'events-single';

        public function hooks() {
            parent::hooks();

            // google data markup
            add_action( 'wp_head', array( $this, 'google_data_markup' ) );

        }

        public function google_data_markup() {
            $event_markup = new Tribe__Events__Google_Data_Markup__Event();
            $html = apply_filters( 'tribe_google_data_markup_json', $event_markup->script_block() );
            echo $html;
        }

        /**
        ...

What I've tried

Initial Attempts

After finding this on StackOverflow, and this on WordPress Dev Exchange, I tried:

remove_action( 'wp_head', array( 'Tribe__Events__Template__Single_Event', 'google_data_markup' ) );

and

global $Tribe__Events__Template__Single_Event;
remove_action( 'wp_head', array( $Tribe__Events__Template__Single_Event, 'google_data_markup' ) );

But it did not work.

Subsequent Attempts

After getting even more desperate and seeing this slim thread (the example in that thread seems very similar to mine but no answer provided that I understand), and this, I've also tried this, with the & before the variable name (why?):

remove_action( 'wp_head', array( &$Tribe__Events__Template__Single_Event, 'google_data_markup' ) );

I've also tried this:

function remove_the_events_calendar_jsonld() {
    remove_action( 'wp_head', array( $Tribe__Events__Template__Single_Event, 'google_data_markup' ) );
}
add_action( 'plugins_loaded', 'remove_the_events_calendar_jsonld', 1 );

as well as:

remove_action( 'Tribe__Events__Template__Single_Event', 'google_data_markup' );

Please help. At this point I have no idea if I am making a simple syntax or logic error of sorts, or if I am completely confused and oblivious to how functions, classes and actions work.

UPDATE 1

As suggested by Andrew Khan I've tried:

global $Tribe__Events__Template__Single_Event;
remove_action( 'wp_head', array( $Tribe__Events__Template__Single_Event, 'google_data_markup' ), 20 );

and

remove_action( 'wp_head', array( 'Tribe__Events__Template__Single_Event', 'google_data_markup' ), 20 );

But it still adds the ld+json block.

UPDATE 2 -- Solution

This works:

add_filter( 'tribe_google_data_markup_json', '__return_empty_string', 20 );

Thank you to Andrew Khan for the answer. His explanation is here, below.

Community
  • 1
  • 1
Andre Bulatov
  • 1,090
  • 3
  • 16
  • 47
  • 1
    The `&` means you are then passing by reference, which means the remove_action function will then access the existing variable, rather than a cloned version of it. http://php.net/manual/en/language.references.php – Andrew Khan Feb 16 '16 at 11:09

1 Answers1

1

The above does not seem to work, but here's a solution that does.

add_filter( 'tribe_google_data_markup_json', '__return_empty_string', 20 );

For all those wondering, look at the function that the action was calling - it simply echos a HTML string which has a filter applied. That particular filter is also not encapsulated in a class, so it can be accessed easily anywhere. I imagine the one you were using was not designed to be changed by anything but the plugin.

Andrew Khan
  • 198
  • 9