Wordpress # to create product link

2019-08-03 20:48发布

问题:

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.

Ex: #7512D would create a link as following;

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

回答1:

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 &#8217; 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();