我想从子函数更改插件类内方法的输出。php而不更改插件的文件。我想更改的输出是,它使用模板按钮数组,而不是常见的a href。问题是,您得到的方法的输出是一个短代码(下面的这个短代码可以插入到您想要的任何模板中)。我不知道如何从子函数更改此方法。php。我修改了这个插件文件,它可以工作,但这意味着如果不松开新按钮,我就无法更新插件。
echo do_shortcode( \'[bewpi-download-invoice title="Download (PDF) Invoice {formatted_invoice_number}" order_id="\' . $order->id . \'"]\' );
以下是be woocommerce pdf发票中的方法。php类
public function download_invoice_shortcode( $atts ) {
if ( ! isset( $atts[\'order_id\'] ) || 0 === intval( $atts[\'order_id\'] ) ) {
return;
}
// by default order status should be Processing or Completed.
$order = wc_get_order( $atts[\'order_id\'] );
if ( ! $order->is_paid() ) {
return;
}
if ( ! BEWPI_Invoice::exists( $order->id ) ) {
return;
}
$url = add_query_arg( array(
\'bewpi_action\' => \'view\',
\'post\' => $order->id,
\'nonce\' => wp_create_nonce( \'view\' ),
) );
$invoice = new BEWPI_Invoice( $order->id );
$tags = array(
\'{formatted_invoice_number}\' => $invoice->get_formatted_number(),
\'{order_number}\' => $order->id,
\'{formatted_invoice_date}\' => $invoice->get_formatted_invoice_date(),
\'{formatted_order_date}\' => $invoice->get_formatted_order_date(),
);
// find and replace placeholders.
$title = str_replace( array_keys( $tags ), array_values( $tags ), $atts[\'title\'] );
// MOD OF THE PLUGIN
thegem_button(array(
\'tag\' => \'a\',
\'href\' => esc_attr( $url ),
\'text\' => esc_html__($title, \'thegem\' ),
\'style\' => \'outline\',
\'size\' => \'medium\',
\'extra_class\' => \'checkout-exit\',
\'attributes\' => array(
\'value\' => esc_attr__( $title, \'thegem\' ),
)
), true);
// ORIGINAL OUTPUT
// printf( \'<a href="%1$s">%2$s</a>\', esc_attr( $url ), esc_html( $title ) );
}
插件的MOD是我添加的新代码。我把它放在评论中的原始输出。当然欢迎任何建议。提前感谢
最合适的回答,由SO网友:Harry 整理而成
在你的情况下,我认为这是直接的功能附加到短代码。您可以创建自己的快捷码并使用它来显示所需内容。你可以试试这个黑客,希望它能帮助你,
add_shortcode( \'custom-bewpi-download-invoice\', \'print_invoice_func\');
function print_invoice_func( $atts ) {
if ( ! isset( $atts[\'order_id\'] ) || 0 === intval( $atts[\'order_id\'] ) ) {
return;
}
// by default order status should be Processing or Completed.
$order = wc_get_order( $atts[\'order_id\'] );
if ( ! $order->is_paid() ) {
return;
}
if ( ! BEWPI_Invoice::exists( $order->id ) ) {
return;
}
$url = add_query_arg( array(
\'bewpi_action\' => \'view\',
\'post\' => $order->id,
\'nonce\' => wp_create_nonce( \'view\' ),
) );
$invoice = new BEWPI_Invoice( $order->id );
$tags = array(
\'{formatted_invoice_number}\' => $invoice->get_formatted_number(),
\'{order_number}\' => $order->id,
\'{formatted_invoice_date}\' => $invoice->get_formatted_invoice_date(),
\'{formatted_order_date}\' => $invoice->get_formatted_order_date(),
);
// find and replace placeholders.
$title = str_replace( array_keys( $tags ), array_values( $tags ), $atts[\'title\'] );
// MOD OF THE PLUGIN
thegem_button(array(
\'tag\' => \'a\',
\'href\' => esc_attr( $url ),
\'text\' => esc_html__($title, \'thegem\' ),
\'style\' => \'outline\',
\'size\' => \'medium\',
\'extra_class\' => \'checkout-exit\',
\'attributes\' => array(
\'value\' => esc_attr__( $title, \'thegem\' ),
)
), true);
// ORIGINAL OUTPUT
// printf( \'<a href="%1$s">%2$s</a>\', esc_attr( $url ), esc_html( $title ) );
}
将上述代码粘贴到函数中。php,现在使用此自定义短代码
echo do_shortcode( \'[custom-bewpi-download-invoice title="Download (PDF) Invoice {formatted_invoice_number}" order_id="\' . $order->id . \'"]\' );
让我知道它给你的输出是什么。