Wordpress#创建产品链接
问题描述:
我正在使用wordpress重做客户端网站。他们以前的开发人员从零开始编写网站,在管理部分中对产品进行文本编辑时,在sku /产品ID号前加上了带有散列/英镑符号“#”的产品sku/product id,以创建链接到现有产品。我想知道什么语言和代码可能是什么样子,所以我可以成功地做基本相同的事情。我已经在WooCommerce中创建了使用SKU /产品ID的所有产品的统一短链接。Wordpress#创建产品链接
例如:#7512D将创建一个链接如下;
<a href="bioquip.com/search/dispproduct.asp?pid=7512D">7512D</a>
答
这应该作为一个插件,以保持它的主题独立,它只需要过滤帖子(或页面)的“内容”。这是一个使用WooCommerce的示例。它使用与您在文章(#XXXXXX)中提到的相同的设计,但我建议您找到除“#”以外的其他内容作为比赛的开始。这将匹配格式为’
格式的所有HTML编码字符。虽然SKU查找可以确保您不会有错误的匹配,但这意味着会有比查询需要更多的查询。
<?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();
这里有很多问题:你为什么想知道使用哪种语言?您已经在使用PHP的声音中解决了WordPress的问题,因为我相信您知道。代码缩减了你分享的内容,但在URL中有一个'asp'扩展名。不知道该怎么做! –
该网站是我们最终更新的一个非常旧的网站。我们决定使用Wordpress进行更简单的自定义和功能。 – bworkman
明白了 - 你正在寻找复制WordPress内的旧功能? –