在wp-admin / includes / template.php中自定义meta_form()

时间:2013-06-21 16:16:23

标签: wordpress

大家好我是一个WordPress菜鸟,所以请耐心等待。所以在template.php中我需要meta_form()函数的功能来根据一些条件进行更改。这是它的样子:

原始代码:

    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功能的最佳方法是什么?我是否必须创建一个插件或什么?我似乎无法找到一个干净的答案。 (p.s.我没有写这个代码,我只是修复它的那个。)

1 个答案:

答案 0 :(得分:1)

meta_form函数不可插入,并且没有可用的钩子,所以,由于不真正推荐核心,你需要另一种方法。以下是jQuery解决方案。将代码放在主题的functions.php文件中,或插入插件:

add_action( 'admin_footer', 'so17239871_mod_metakeyselect' );
function so17239871_mod_metakeyselect()
{
    global $pagenow, $post_type;

    if( ! ( 'post.php' == $pagenow && 'video_photo' == $post_type ) )
        return;
?>
<script type="text/javascript">
jQuery(document).ready(function(){
    jQuery("#metakeyselect > option").hide();
    jQuery("#metakeyselect > option[value^='tqmcf_']").show();
});
</script>
<?php
}