简体   繁体   中英

Disable a theme function in from a child theme functions.php in wordpress

I was having a problem with my theme in wordpress, that was showing its own og:meta descriptions on my theme, so it gets duplicated due to all in one seo plugin.

I wanted to disable the ones from the theme, but i did not know how, so i manage to find the function on the php file which is triggering this to show on the website, but i dont know how to disable it from the functions.php or my child theme, so it does not get overriten when update. The function in question is the following

// Open Graph Meta
function aurum_wp_head_open_graph_meta() {
 global $post;

 // Only show if open graph meta is allowed
 if ( ! apply_filters( 'aurum_open_graph_meta', true ) ) {
  return;
 }

 // Do not show open graph meta on single posts
 if ( ! is_singular() ) {
  return;
 }

 $image = '';

 if ( has_post_thumbnail( $post->ID ) ) {
  $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'original' );
  $image = esc_attr( $featured_image[0] );
 }

 ?>

 <meta property="og:type" content="article"/>
 <meta property="og:title" content="<?php echo esc_attr( get_the_title() ); ?>"/>
 <meta property="og:url" content="<?php echo esc_url( get_permalink() ); ?>"/>
 <meta property="og:site_name" content="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>"/>
 <meta property="og:description" content="<?php echo esc_attr( get_the_excerpt() ); ?>"/>

 <?php if ( '' != $image ) : ?>
 <meta property="og:image" content="<?php echo $image; ?>"/>
 <?php endif;
}

add_action( 'wp_head', 'aurum_wp_head_open_graph_meta', 5 );

Thanks a lot in advance.

This function actually has an inbuilt way of to short-circuit and return early. If the value of false is passed to the filter aurum_open_graph_meta if will return before any output is created.

add_filter( 'aurum_open_graph_meta',  '__return_false' );

You can read about the special __return_false() function here: https://codex.wordpress.org/Function_Reference/_return_false

If this function did not have a early return flag then an alternative method to stop it executing would be removing the action that the function creates. This would be a more universal method that could be applied to most actions registered anywhere in WordPress.

Add your own action that runs after the action you want removed has been added but before it is executed .

In this case you can use the init hook to make that happen. Inside your action function make a call to remove_action() with the details or the hook you want removed.

add_action( 'init', 'remove_my_action' );
function remove_my_action(){
      remove_action( 'wp_head', 'aurum_wp_head_open_graph_meta', 5 );
}

Note that the action needs to be removed on the same $priority as it was added (in this case '5'). Try adding the above code to your child theme's functions.php file and see if it removes the action.

If you are only supporting php>5.3 then you could clean up that code by using an anonymous function :

add_action( 'init', function() { 
    remove_action( 'wp_head', 'aurum_wp_head_open_graph_meta', 5 );
}

Some additional reading regarding adding/removing actions in WordPress: https://codex.wordpress.org/Function_Reference/remove_action

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM