首页 文章

从wordpress中的子主题functions.php禁用主题功能

提问于
浏览
3

我在wordpress中遇到了我的主题问题,它显示了自己的og:我的主题的元描述,所以它由于所有的一个seo插件而被复制 .

我想禁用主题中的那些,但我不知道如何,所以我设法在php文件上找到触发此功能在网站上显示的功能,但我不知道如何从功能中禁用它 . PHP或我的孩子主题,所以它在更新时不会过分严重 . 有问题的功能如下

// 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 );

非常感谢提前 .

1 回答

  • 3

    这个功能实际上有一种内置的短路和早期返回方式 . 如果 false 的值传递给过滤器 aurum_open_graph_meta ,则在创建任何输出之前将返回 .

    add_filter( 'aurum_open_graph_meta',  '__return_false' );
    

    你可以在这里阅读特殊的 __return_false() 功能:https://codex.wordpress.org/Function_Reference/_return_false

    如果此函数没有早期返回标志,则停止执行的另一种方法是删除该函数创建的操作 . 这将是一种更通用的方法,可以应用于WordPress中任何位置注册的大多数操作 .

    添加自己的运行 after the action you want removed has been added but before it is executed 的操作 .

    在这种情况下,您可以使用 init 钩子来实现这一点 . 在您的动作函数内部调用 remove_action() ,其中包含您要删除的详细信息或挂钩 .

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

    请注意,需要在添加的行为中删除相同的动作(在本例中为'5') . 尝试将上面的代码添加到您的子主题的functions.php文件中,看看它是否删除了该操作 .

    如果您只支持php> 5.3,那么您可以使用anonymous function清理该代码:

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

    关于在WordPress中添加/删除操作的一些额外阅读:https://codex.wordpress.org/Function_Reference/remove_action

相关问题