如何覆盖 WordPress 核心功能? [英] How do I overwrite WordPress core functions?

查看:45
本文介绍了如何覆盖 WordPress 核心功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次接触WordPress,我的任务是将定制的核心功能提取出来放到一个非核心文件中,以便我们升级时更易于维护.以下是 wp-admin/includes/template.php 中的一种方法的示例:

This is my first time touching WordPress, and I have the task of extracting the core functions that have been customized and putting them in a non-core file so that it's more maintainable when we upgrade. Here's an example of one of the methods in wp-admin/includes/template.php :

原始代码:

    function meta_form() {
        global $wpdb;
        $limit = (int) apply_filters( 'postmeta_form_limit', 30 );
        $keys = $wpdb->get_col( "
            SELECT meta_key
            FROM $wpdb->postmeta
            GROUP BY meta_key
            HAVING meta_key NOT LIKE '\_%'
            ORDER BY meta_key
            LIMIT $limit" );
        if ( $keys )
            natcasesort($keys);
    ?>

定制版:

function meta_form() {
    global $wpdb;

  if ( isset($_GET['post']) )
    $post_id = (int) $_GET['post'];
  elseif ( isset($_POST['post_ID']) )
    $post_id = (int) $_POST['post_ID'];
  else
    $post_id = 0;

  if ( $post_id ) {
    $post_ = get_post($post_id);

  }

  if ($post_->post_type == 'video_photo' ){
    $limit = (int) apply_filters( 'postmeta_form_limit', 30 );
    $keys = $wpdb->get_col( "
        SELECT meta_key
        FROM $wpdb->postmeta
    where meta_key like 'tqmcf_%'
        GROUP BY meta_key
        HAVING meta_key NOT LIKE '\_%'
        ORDER BY meta_key
        LIMIT $limit" );
  }else{
    $limit = (int) apply_filters( 'postmeta_form_limit', 30 );
    $keys = $wpdb->get_col( "
        SELECT meta_key
        FROM $wpdb->postmeta

        GROUP BY meta_key
        HAVING meta_key NOT LIKE '\_%'
        ORDER BY meta_key
        LIMIT $limit" );
  }

    if ( $keys )
        natcasesort($keys);
?>

我应该在哪里定义我的 meta_form() 函数以确保它覆盖核心方法?

Where exactly would I define my meta_form() function to make sure it overwrites the core method?

推荐答案

在 WordPress 中,您无法覆盖整个核心.您有两种方法可以修改 WordPress 核心:

In WordPress you can't owerride the whole core. You have two ways to modify the WordPress Core :

有些功能可以重新定义.这可以在主题的 functions.php 文件或插件文件中完成.您可以查看 Pluggable Functions 的列表.

Some functions can be redefined. This can be done in theme's functions.php file or in a plugin file. You can check the list of the Pluggable Functions.

  • 过滤钩子可用于修改核心内部的变量WordPress 流程.
  • Action hooks 可用于触发自定义对某些事件起作用.
  • Filter hooks can be used to modify vars inside the core WordPress process.
  • Action hooks can be used to fire custom function on some events.

注意 1:您可以在主题或插件中创建自己的过滤器和动作挂钩.

Notice 1 : you can create your own filter and action hooks in your theme or plugin.

注意 2:如果找不到方便的动作钩子,可以使用过滤器钩子来触发函数.

Notice 2 : you can use filter hooks to fire a function if you don't find a convenient action hook.

如果您遵循 WordPress 编码标准,您应该能够深入修改 WordPress 的行为.

If you follow the WordPress coding standards, you should be able to deeply modify the behaviour of WordPress.

对于您展示的功能,您应该寻找 post_where 过滤器钩子,以做这样的事情:

For the function your showing, you should look for the post_where filter hook, to do something like this :

add_filter( 'posts_where' , 'my_posts_where' );

function my_posts_where( $where ) {

    global $post;

    if ($post->post_type == 'video_photo' ){
        $where .= " AND meta_key like 'tqmcf_%'";
    }

    return $where;
}

编辑 1:即使此特定查询难以定位,以下内容也可能更合适.

Edit 1: The following may be more suitable even if this specific query is hard to target.

add_filter( 'query' , 'my_meta_form_query' );

function my_meta_form_query( $query ) {

    $pattern = "/SELECT(?:\W*)meta_key(?:\W*)FROM (.*)(?:\W*.*?)*LIMIT(?:\W*)([0-9]*)/g";

    if( preg_match($pattern, $query, $vars) ) {
        $postmeta = $vars[1];
        $limit = $vars[2];

        $query = "SELECT meta_key
                FROM $postmeta
                WHERE meta_key like 'tqmcf_%'
                GROUP BY meta_key
                HAVING meta_key NOT LIKE '\_%'
                ORDER BY meta_key
                LIMIT $limit";
    }

    return $query;
}

您必须深入研究代码以找到合适的钩子以及如何最好地过滤变量.在此示例中,我们检查查询是否与 meta_form() 函数中的查询匹配.我们提取现有的查询变量并构建一个包含 WHERE 条件的新查询.我没有测试这段代码,可能存在一些错误,但它可以让您了解我们如何更改核心代码.

You have to digg into the code to find suitable hooks and how you can best filter variables. In this example, we check if the query is matching the one in the meta_form() function. We extract the existing query vars and we build a new query including the WHERE condition. I didn't test this piece of code and there may be some bugs but it can give you an idea of how we alter the core code.

这篇关于如何覆盖 WordPress 核心功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆