链接到另一个数据库的 Wordpress 简码
Wordpress shortcode linked to another DB
首先,这是我第一次尝试简码。
值得一提的是,我也在我的网站上使用了 woocommerce。
开始吧:
我知道要在 wordpress 中添加短代码,您需要在 functions.php 文件中写入类似于以下代码的内容:(这只是一个示例)
function myshortcode() {
return "This is a shortcode example!";
}
add_shortcode( 'mycode', 'myshortcode' );
如果我将 [mycode]
添加到 wordpress 页面编辑器中,预览会正确显示我的文本。
但是如果我需要在短代码中使用变量(在我的例子中是 woocommerce 订单号)怎么办?
假设我需要比较 woocommerce_order_number 和 my_custom_uid(插入另一个数据库,外部 wordpress).
我通常使用像下面这样的数据库请求,通常它工作正常(和以前一样,这只是一个例子):
select 'my_custom_uid' from 'my_custom_database' where 'woocommerce_order_number' = '1234'
问题是我不知道 woocommerce_order_number(它每次都在变!),因为这个简码需要放在 html 电子邮件正文中,我需要在之后发送给客户他们下了订单。
我怎样才能获得客户的 woocommerce 订单(变量每次都在变化),这样我就可以将它用于我的短代码 link 它到我的 custom_uid?
如果问题不够清楚,欢迎提问!
非常感谢
我没有理由使用简码。如果您想向电子邮件添加内容,您应该使用电子邮件中的一个挂钩。例如:
function kia_display_email_order_meta( $order, $sent_to_admin, $plain_text ) {
$some_field = get_post_meta( $order->id, '_some_field', true ),
$another_field = get_post_meta( $order->id, '_another_field', true ),
if( $plain_text ){
echo 'The value for some field is ' . $some_field . ' while the value of another field is ' . $another_field;
} else {
echo '<p>The value for <strong>some field</strong> is ' . $some_field. ' while the value of <strong>another field</strong> is ' . $another_field . '</p>';
}
}
add_action('woocommerce_email_customer_details', 'kia_display_email_order_meta', 30, 3 );
请注意,$order
对象是 kia_display_email_order_meta
函数可用的第一个参数。因此您可以通过 $order->id
获取 ID。这应该在客户地址详细信息之后添加数据,但如果 woocommerce_email_customer_details
不合适,还有其他挂钩可用。
我终于设法解决了这个问题,如果有人感兴趣的话,这是我所做的:
<?PHP
add_action('fue_before_variable_replacements', 'register_variable_replacements', 11, 4);
add_action('fue_email_variables_list', 'email_variables_list');
/**
* This gets called to replace the variable in the email with an actual value
* @param $var - Modify this: array key is the variable name, value is the replacement
*/
function register_variable_replacements($var, $email_data, $queue_item, $email){
global $wpdb;
// Look up UID from order number
$orderNumber = $var->get_variables()['order_number'];
$sql = " //Your sql statement here...//";
$results = $wpdb->get_results($sql);
$UID = $results[0]->meta_value;
$variables = array(
'uid' => $UID
);
$var->register($variables);
}
function email_variables_list($email)
{
global $woocommerce;
?>
<li class="var hideable var_subscriptions">
<strong>{uid}</strong>
<img class="help_tip" title="<?php _e('Order UID', 'follow_up_emails'); ?>" src="<?php echo $woocommerce->plugin_url(); ?>/assets/images/help.png" width="16" height="16"/>
</li>
<?php
}
现在,在您的跟进电子邮件插件中,您可以使用 {uid}
作为变量,这将在每封电子邮件中被替换为正确的值。
我虽然最好的方法是使用短代码,但后来我发现了这个过滤器,我认为这是处理这个问题的最好方法。此代码也非常灵活,您可以根据需要添加任意数量的变量。
希望这对某人有所帮助。
首先,这是我第一次尝试简码。 值得一提的是,我也在我的网站上使用了 woocommerce。
开始吧:
我知道要在 wordpress 中添加短代码,您需要在 functions.php 文件中写入类似于以下代码的内容:(这只是一个示例)
function myshortcode() {
return "This is a shortcode example!";
}
add_shortcode( 'mycode', 'myshortcode' );
如果我将 [mycode]
添加到 wordpress 页面编辑器中,预览会正确显示我的文本。
但是如果我需要在短代码中使用变量(在我的例子中是 woocommerce 订单号)怎么办?
假设我需要比较 woocommerce_order_number 和 my_custom_uid(插入另一个数据库,外部 wordpress).
我通常使用像下面这样的数据库请求,通常它工作正常(和以前一样,这只是一个例子):
select 'my_custom_uid' from 'my_custom_database' where 'woocommerce_order_number' = '1234'
问题是我不知道 woocommerce_order_number(它每次都在变!),因为这个简码需要放在 html 电子邮件正文中,我需要在之后发送给客户他们下了订单。
我怎样才能获得客户的 woocommerce 订单(变量每次都在变化),这样我就可以将它用于我的短代码 link 它到我的 custom_uid?
如果问题不够清楚,欢迎提问! 非常感谢
我没有理由使用简码。如果您想向电子邮件添加内容,您应该使用电子邮件中的一个挂钩。例如:
function kia_display_email_order_meta( $order, $sent_to_admin, $plain_text ) {
$some_field = get_post_meta( $order->id, '_some_field', true ),
$another_field = get_post_meta( $order->id, '_another_field', true ),
if( $plain_text ){
echo 'The value for some field is ' . $some_field . ' while the value of another field is ' . $another_field;
} else {
echo '<p>The value for <strong>some field</strong> is ' . $some_field. ' while the value of <strong>another field</strong> is ' . $another_field . '</p>';
}
}
add_action('woocommerce_email_customer_details', 'kia_display_email_order_meta', 30, 3 );
请注意,$order
对象是 kia_display_email_order_meta
函数可用的第一个参数。因此您可以通过 $order->id
获取 ID。这应该在客户地址详细信息之后添加数据,但如果 woocommerce_email_customer_details
不合适,还有其他挂钩可用。
我终于设法解决了这个问题,如果有人感兴趣的话,这是我所做的:
<?PHP
add_action('fue_before_variable_replacements', 'register_variable_replacements', 11, 4);
add_action('fue_email_variables_list', 'email_variables_list');
/**
* This gets called to replace the variable in the email with an actual value
* @param $var - Modify this: array key is the variable name, value is the replacement
*/
function register_variable_replacements($var, $email_data, $queue_item, $email){
global $wpdb;
// Look up UID from order number
$orderNumber = $var->get_variables()['order_number'];
$sql = " //Your sql statement here...//";
$results = $wpdb->get_results($sql);
$UID = $results[0]->meta_value;
$variables = array(
'uid' => $UID
);
$var->register($variables);
}
function email_variables_list($email)
{
global $woocommerce;
?>
<li class="var hideable var_subscriptions">
<strong>{uid}</strong>
<img class="help_tip" title="<?php _e('Order UID', 'follow_up_emails'); ?>" src="<?php echo $woocommerce->plugin_url(); ?>/assets/images/help.png" width="16" height="16"/>
</li>
<?php
}
现在,在您的跟进电子邮件插件中,您可以使用 {uid}
作为变量,这将在每封电子邮件中被替换为正确的值。
我虽然最好的方法是使用短代码,但后来我发现了这个过滤器,我认为这是处理这个问题的最好方法。此代码也非常灵活,您可以根据需要添加任意数量的变量。
希望这对某人有所帮助。