如何在没有 arg 输入的情况下修改函数中的一个条件


How to modify one condition inside a function without arg input?

我需要调用一个输出html的函数。在这个函数中,有一个条件是从 html 中排除一行,我需要这一行。此条件不是函数的参数。它是硬编码的。所以,我必须编写自己的代码,或者,有没有办法修改这个条件?

function get_media_item( $attachment_id, $args = null ) {
//....lots stuffs, then, arrive this part:
    $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) );
    $order = '';
    foreach ( $form_fields as $key => $val ) {
        if ( 'menu_order' == $key ) {
            if ( $gallery )
                $order = "<div class='menu_order'> <input class='menu_order_input' type='text' id='attachments[$attachment_id][menu_order]' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ). "' /></div>";
            else
                $order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
            unset( $form_fields['menu_order'] );
            break;
        }
    }
//... other stuffs
}

我需要在其他选项卡中调用此函数,而不是"图库"选项卡。所以,我无法获得$order输入框。

如果你不想将参数传递给函数(顺便说一句,建议这样做)来修改条件,你可以使用 globalize 变量(或会话)。

例:

function test(){
   global $instructions;
   if (isset($instructions['fetch'])){
      // new codes
   } else {
      // existing codes
   }
   return $result;
}
// implement:
$instructions = array();
$instructions['fetch'] = TRUE;
echo test();