Php Wordpress#创建产品链接

Php Wordpress#创建产品链接,php,wordpress,woocommerce,hook-woocommerce,wordpress-hook,Php,Wordpress,Woocommerce,Hook Woocommerce,Wordpress Hook,我正在使用wordpress重做一个客户网站。他们以前的开发人员是从头开始编写网站的,他们制作了一个产品sku/产品id,在sku/产品id号前面加了一个哈希/磅符号“#”,在“管理”部分对产品进行文本编辑将创建一个指向现有产品的链接。我想知道什么样的语言和代码可能是什么样的,这样我就可以成功地完成基本相同的事情。我已经在WooCommerce中创建了一个使用SKU/产品ID的所有产品的统一短链接 例如:#7512D将创建如下链接 <a href="bioquip.com/search/d

我正在使用wordpress重做一个客户网站。他们以前的开发人员是从头开始编写网站的,他们制作了一个产品sku/产品id,在sku/产品id号前面加了一个哈希/磅符号“#”,在“管理”部分对产品进行文本编辑将创建一个指向现有产品的链接。我想知道什么样的语言和代码可能是什么样的,这样我就可以成功地完成基本相同的事情。我已经在WooCommerce中创建了一个使用SKU/产品ID的所有产品的统一短链接

例如:#7512D将创建如下链接

<a href="bioquip.com/search/dispproduct.asp?pid=7512D">7512D</a>

这应该作为一个插件来完成,以保持其主题独立,它只需要过滤帖子(或页面)的“内容”。下面是一个使用WooCommerce的工作示例。它使用了与您在帖子中提到的相同的设计(#XXXXXX),但我建议您在比赛开始时使用“#”之外的其他符号。这将匹配
格式。虽然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();

这里有很多问题:为什么你想知道该使用哪种语言?我相信您已经了解到,WordPress使用PHP,从它的声音来看,您已经习惯了它。您共享的代码被截断,但URL中有一个
asp
扩展名。不知道该怎么办!该网站是一个非常旧的网站,我们最终更新。我们决定使用Wordpress,以简化自定义和功能。明白了-您希望复制Wordpress中的旧功能?是的,这是正确的。我们对使用这一旧功能的产品有大量的描述,不必对每个人都进行检查,也不必对链接进行编码(这些链接经常更改),这样就避免了在我们所有产品的顶部停留,避免了断开的链接。你介意在有用的问题上给出这个问题吗?我正试图获得足够的声誉点,开始为网站贡献更多。谢谢你的反馈!我发现它打破了单一产品的页面。像@这样的另一个符号会比#更好吗?如果是这样,我会在上面php中出现的地方替换“#”吗?如果它破坏了页面,那么它很可能与其他插件冲突。您可以尝试关闭其他插件,直到您看到它工作,以查看冲突是什么。您可能需要调整
add_filter()
调用的时间。我已经停用了所有插件,如果在描述文本中有一个#来创建链接,则所有描述都不会加载,并且管理wp横幅为空。