Wordpress#创建产品链接 [英] Wordpress # to create product link
问题描述
我正在使用wordpress重做客户网站.他们的前一位开发人员从头开始编写网站,然后在admin部分中对产品进行文字编辑时,在sku/产品ID编号前加上了井号/磅符号'#'来制作产品sku/产品ID,将创建一个链接到现有产品.我想知道什么样的语言和代码是什么样的,所以我可以成功地完成基本上相同的事情.我已经在WooCommerce中使用SKU/产品ID为所有产品创建了统一的短链接.
I'm working on redoing a clients website using wordpress. Their previous developer who wrote the website from scratch, made a products sku/product id with a hash/pound sign '#' in front of the sku/product id number in the text editting of the product in the admin section would create a link to the existing product. I was wondering what language and what the code might look like so I could successfully do essentially the same thing. I've already created in WooCommerce a uniform shortlink to all products using the SKU/Product ID.
例如:#7512D 将创建如下链接;
Ex: #7512D would create a link as following;
<a href="bioquip.com/search/dispproduct.asp?pid=7512D">7512D</a>
推荐答案
此操作应作为插件执行,以使其主题独立,并且只需要过滤帖子(或页面)的内容"即可.这是一个使用WooCommerce的工作示例.它使用的设计与您在帖子(#XXXXXX)中提到的设计相同,但是我建议您找到除#"之外的其他内容以用作比赛的开始.这将匹配所有’
格式的HTML编码字符.虽然SKU查找确保您不会有错误的匹配,但这意味着查询的数量将超过所需的数量.
This should be done as a plugin to keep it theme independent and it simply needs to filter the "content" of the post (or page). Here's a working example using WooCommerce. It uses the same design as you mentioned in your post (#XXXXXX), but I recommend you find something other than the "#" to use as the beginning of the match. This will match all of the HTML encoded characters that are in the ’
format. While the SKU lookup makes sure you won't have an errant match, it means that there will be a lot more queries than there need to be.
<?php
/*
Plugin Name: Replace SKU with Link
Description: Plugin to replace a formatted SKU (#XXXXXX) with the link to that product
Version: 1.0
*/
defined( 'ABSPATH' ) or die( 'No direct access!' );
class SkuReplace {
/*
* On __construct, we will initialize the filter for the content
*/
function __construct()
{
add_filter( 'the_content', array( $this, 'replace_sku_with_link' ) );
}
/**
* The filter hook get processed here
*
* @return string the filtered content
*/
function replace_sku_with_link( $content )
{
// Check if we're inside the main loop in a single post page.
if ( ( is_single() || is_page() ) && in_the_loop() && is_main_query() )
{
// Use the callback to check to see if this product exists in the DB
$content = preg_replace_callback(
'/\#[^\s\<]*/',
array( $this, 'get_product_url' ),
$content
);
}
return $content;
}
/**
* The match is checked against the product entries to verify that it exists
* If it does, it will create the hyperlink and return it, else, it returns
* the original string so as not to break the content
*
* @return string URL or original string
*/
function get_product_url( $in )
{
global $wpdb;
$sku = ltrim( rtrim( $in[0], " \t\r\n" ), '#');
$product_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1",
$sku
)
);
if( $product_id )
{
$product = new WC_Product( $product_id );
$url = get_permalink( $product_id ) ;
return '<a href="'. $url .'">'. $product->get_name() .'</a>';
}
return $in[0];
}
} // end class
$plugin_name = new SkuReplace();
这篇关于Wordpress#创建产品链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!